- Notifications
You must be signed in to change notification settings - Fork197
🧙♀️ easily deploy cloudflare workers applications using wrangler and github actions
License
Apache-2.0, MIT licenses found
Licenses found
cloudflare/wrangler-action
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Easy-to-use GitHub Action to useWrangler. Makes deploying Workers a breeze.
- Wrangler v1 is no longer supported.
- Global API key & Email Auth no longer supported
- Action version syntax is newly supported. This means e.g.
uses: cloudflare/wrangler-action@v3,uses: cloudflare/wrangler-action@v3.x, anduses: cloudflare/wrangler-action@v3.x.xare all now valid syntax. Previously supported syntax e.g.uses: cloudflare/wrangler-action@3.x.xis no longer supported -- the prefixvis now necessary.
Refer to Changelog for more information.
Addwrangler-action to the workflow for your Workers/Pages application. The below example will deploy a Worker on agit push to themain branch:
name:Deployon:push:branches: -mainjobs:deploy:runs-on:ubuntu-latestname:Deploysteps: -uses:actions/checkout@v4 -name:Deployuses:cloudflare/wrangler-action@v3with:apiToken:${{ secrets.CLOUDFLARE_API_TOKEN }}
You'll need to configure Wrangler using GitHub's Secrets feature - go to "Settings -> Secrets" and add your Cloudflare API token (for help finding this, see theWorkers documentation). Your API token is encrypted by GitHub, and the action won't print it into logs, so it should be safe!
With your API token set as a secret for your repository, pass it to the action in thewith block of your workflow. Below, I've set the secret name toCLOUDFLARE_API_TOKEN:
jobs:deploy:name:Deploysteps:uses:cloudflare/wrangler-action@v3with:apiToken:${{ secrets.CLOUDFLARE_API_TOKEN }}
If you need to install a specific version of Wrangler to use for deployment, you can also pass the inputwranglerVersion to install a specific version of Wrangler from NPM. This should be aSemVer-style version number, such as2.20.0:
jobs:deploy:steps:uses:cloudflare/wrangler-action@v3with:apiToken:${{ secrets.CLOUDFLARE_API_TOKEN }}wranglerVersion:"2.20.0"
Optionally, you can also pass aworkingDirectory key to the action. This will allow you to specify a subdirectory of the repo to run the Wrangler command from.
jobs:deploy:steps:uses:cloudflare/wrangler-action@v3with:apiToken:${{ secrets.CLOUDFLARE_API_TOKEN }}workingDirectory:"subfoldername"
Worker secrets can optionally be passed in viasecrets as a string of names separated by newlines. Each secret name must match the name of an environment variable specified in theenv field. This creates or replaces the value for the Worker secret using thewrangler secret put command. It's also possible to specify worker environment using environment parameter.
jobs:deploy:steps:uses:cloudflare/wrangler-action@v3with:apiToken:${{ secrets.CLOUDFLARE_API_TOKEN }}environment:productionsecrets:| SECRET1 SECRET2env:SECRET1:${{ secrets.SECRET1 }}SECRET2:${{ secrets.SECRET2 }}
If you need to run additional shell commands before or after your command, you can specify them as input topreCommands (beforedeploy) orpostCommands (afterdeploy). These can include additionalwrangler commands (that is,whoami,kv:key put) or any other commands available inside thewrangler-action context.
jobs:deploy:steps:uses:cloudflare/wrangler-action@v3with:apiToken:${{ secrets.CLOUDFLARE_API_TOKEN }}preCommands:echo "*** pre command ***"postCommands:| echo "*** post commands ***" wrangler kv:key put --binding=MY_KV key2 value2 echo "******"
You can use thecommand option to do specific actions such as runningwrangler whoami against your project:
jobs:deploy:steps:uses:cloudflare/wrangler-action@v3with:apiToken:${{ secrets.CLOUDFLARE_API_TOKEN }}command:whoami
You can also add a command that spans multiple lines:
jobs:deploy:steps:uses:cloudflare/wrangler-action@v3with:apiToken:${{ secrets.CLOUDFLARE_API_TOKEN }}command:| pages project list pages deploy .vercel/output/static --project-name=demo-actions --branch=test
The above workflow examples have already shown how to runwrangler-action when new commits are merged to the main branch. For most developers, this workflow will easily replace manual deploys and be a great first integration step withwrangler-action:
on:push:branches: -mainjobs:deploy:runs-on:ubuntu-latestname:Deploysteps: -uses:actions/checkout@v4 -name:Deployuses:cloudflare/wrangler-action@v3with:apiToken:${{ secrets.CLOUDFLARE_API_TOKEN }}
Note that there are a number of possible events, likepush, that can be used to trigger a workflow. For more details on the events available, refer to theGitHub Actions documentation.
If you want to deploy your Pages project with GitHub Actions rather than the built-in continous integration (CI), then this is a great way to do it. Wrangler 2 will populate the commit message and branch for you. You only need to pass the project name. If a push to a non-production branch is done, it will deploy as a preview deployment:
on:[push]jobs:deploy:runs-on:ubuntu-latestname:Deploypermissions:contents:readdeployments:writesteps: -uses:actions/checkout@v4 -name:Deployuses:cloudflare/wrangler-action@v3with:apiToken:${{ secrets.CLOUDFLARE_API_TOKEN }}accountId:${{ secrets.CLOUDFLARE_ACCOUNT_ID }}command:pages deploy YOUR_DIST_FOLDER --project-name=example# Optional: Enable this if you want to have GitHub Deployments triggeredgitHubToken:${{ secrets.GITHUB_TOKEN }}
If you would like to deploy your Workers application on a recurring basis – for example, every hour, or daily – theschedule trigger allows you to use cron syntax to define a workflow schedule. The below example will deploy at the beginning of every hour:
on:schedule: -cron:"0 * * * *"jobs:deploy:runs-on:ubuntu-latestname:Deploysteps: -uses:actions/checkout@v4 -name:Deploy appuses:cloudflare/wrangler-action@v3with:apiToken:${{ secrets.CLOUDFLARE_API_TOKEN }}
If you need help defining the correct cron syntax, check outcrontab.guru, which provides a friendly user interface for validating your cron schedule.
If you need to trigger a workflow at-will, you can use GitHub'sworkflow_dispatchevent in your workflow file. By setting your workflow to trigger on that event, you will be able to deploy your application via the GitHub UI. The UI also accepts inputs that can be used to configure the action:
on:workflow_dispatch:inputs:environment:description:"Choose an environment to deploy to: <dev|staging|prod>"required:truedefault:"dev"jobs:deploy:runs-on:ubuntu-latestname:Deploysteps: -uses:actions/checkout@v4 -name:Deploy appuses:cloudflare/wrangler-action@v3with:apiToken:${{ secrets.CLOUDFLARE_API_TOKEN }}command:deploy --env ${{ github.event.inputs.environment }}
For more advanced usage or to programmatically trigger the workflow from scripts, refer tothe GitHub documentation for making API calls.
To create a new version of your Worker that is not deployed immediately, use thewrangler versions upload command. Worker versions created in this way can then be deployed all at once at a later time or gradually deployed using thewrangler versions deploy command or via the Cloudflare dashboard under the Deployments tab. Wrangler v3.40.0 or above is required to use this feature.
jobs:upload:runs-on:ubuntu-latestname:Deploysteps: -uses:actions/checkout@v4 -name:Upload Worker Versionuses:cloudflare/wrangler-action@v3with:apiToken:${{ secrets.CLOUDFLARE_API_TOKEN }}accountId:${{ secrets.CLOUDFLARE_ACCOUNT_ID }}command:versions upload
There is an environment parameter that can be set within the workflow to enable this. Example:
-uses:cloudflare/wrangler-action@v3with:apiToken:${{ secrets.CLOUDFLARE_API_TOKEN }}command:deploy --env productionsecrets:| SUPER_SECRETenvironment:productionenv:SUPER_SECRET:${{ secrets.SUPER_SECRET }}
More advanced workflows may need to parse the resulting output of Wrangler commands. To do this, you can use thecommand-output output variable in subsequent steps. For example, if you want to print the output of the Wrangler command, you can do the following:
-name:Deployid:deployuses:cloudflare/wrangler-action@v3with:apiToken:${{ secrets.CLOUDFLARE_API_TOKEN }}accountId:${{ secrets.CLOUDFLARE_ACCOUNT_ID }}command:pages deploy --project-name=example-name:print wrangler command outputenv:CMD_OUTPUT:${{ steps.deploy.outputs.command-output }}run:echo $CMD_OUTPUT
Now when you run your workflow, you will see the full output of the Wrangler command in your workflow logs. You can also use this output in subsequent workflow steps to parse the output for specific values.
Note: the
command-stderroutput variable is also available if you need to parse the standard error output of the Wrangler command.
If you are executing a Wrangler command that results in either a Workers or Pages deployment, you can utilize thedeployment-url output variable to get the URL of the deployment. For example, if you want to print the deployment URL after deploying your application, you can do the following:
-name:Deployid:deployuses:cloudflare/wrangler-action@v3with:apiToken:${{ secrets.CLOUDFLARE_API_TOKEN }}accountId:${{ secrets.CLOUDFLARE_ACCOUNT_ID }}command:pages deploy --project-name=example-name:print deployment-urlenv:DEPLOYMENT_URL:${{ steps.deploy.outputs.deployment-url }}run:echo $DEPLOYMENT_URL
The resulting output will look something like this:
https://<your_pages_site>.pages.devPages deployments will also provide their alias URL (since Wrangler v3.78.0). You can use thepages-deployment-alias-url output variable to get the URL of the deployment alias. This is useful for, for example, branch aliases for preview deployments.
If the sample action above was used to deploy a branch other than main, you could use the following to get the branch URL:
-name:print pages-deployment-alias-urlenv:DEPLOYMENT_ALIAS_URL:${{ steps.deploy.outputs.pages-deployment-alias-url }}run:echo $DEPLOYMENT_ALIAS_URL
Resulting in:
https://new-feature.<your_pages_site>.pages.devBy default, this action will detect which package manager to use, based on the presence of apackage-lock.json,yarn.lock,pnpm-lock.yaml, orbun.lockb/bun.lock file.
If you need to use a specific package manager for your application, you can set thepackageManager input tonpm,yarn,pnpm, orbun. You don't need to set this option unless you want to override the default behavior.
jobs:deploy:steps:uses:cloudflare/wrangler-action@v3with:apiToken:${{ secrets.CLOUDFLARE_API_TOKEN }}packageManager:pnpm
Refer to theQuick Start guide to get started. Once you have a Workers application, you may want to set it up to automatically deploy from GitHub whenever you change your project.
You will need to addaccount_id = "" in yourwrangler.toml file or setaccountId in this GitHub Action.
on:[push]jobs:deploy:runs-on:ubuntu-latestname:Deploysteps: -uses:actions/checkout@v4 -name:Deploy appuses:cloudflare/wrangler-action@v3with:apiToken:${{ secrets.CLOUDFLARE_API_TOKEN }}accountId:${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
About
🧙♀️ easily deploy cloudflare workers applications using wrangler and github actions
Topics
Resources
License
Apache-2.0, MIT licenses found
Licenses found
Code of conduct
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.