This is deprecated use the new https://developers.cloudflare.com/workers/static-assets/.
Setting Up Github Actions for Cloudflare Workers
The development workflow with Cloudflare Workers is quite poor, as the online editor for Workers is unusable, and there is no out of the box Github connection, like you would have with Cloudflare Pages. Therefore when developing in Cloudflare Workers, it is crucial to set up Github workflows so you can properly maintain the code and deployments. The following is the basic guide that works for us. The code for the workflow action is from Cloudflare Wrangler Action.
Important: Before setting anything up, make sure you wrangler.toml file is set up correctly. The action will pull that to do the builds.
Steps for Setting Up Github Workflow Action
1. Set up a new API Token in Cloudflare
-
Note: I believe it is best to set up a differnt API Token for each Github repo, though of course, you can share tokens between repos, if you want. It just seems like managing different tokens, in case you need to roll one of them, will be easier if there is a different token for each repo. Otherwise, if you roll a token, you will have to then update all the repos that use it. I assume Github has some way of setting a global secret, but I can't figure it out and it doesn't seem like Cloudflare's action supports this anyway.
-
To set up a token
- Log into Cloudflare -> Profile (top right) -> API Tokens (left) -> Create Token (middle) -> Continue to Summary and Get Token.
- Note: Use the Edit Cloudflare Template, and you can choose All Accounts and All Zones, if you want, or restrict. Remember to edit the Token Name at the top of the page, before saving it.
- Log into Cloudflare -> Profile (top right) -> API Tokens (left) -> Create Token (middle) -> Continue to Summary and Get Token.
2. Add Cloudflare API Token in Github Repo
- Go to the Github Repo -> Settings -> Secrets and Variables -> Choose Actions -> Click on New Repository Secret (not enviornment secret) -> Create a secret with the name CLOUDFLARE_API_TOKEN and the API Key you got in the first step.
- Note: Obviously, you can call the Secret whatever name you want, but we are using that name above in the code below.
3. Create Workflow Files
-
Click Actions on the repo you want to create the workflow for -> New Workflow (top left) -> Choose Set Up Workflow yourself - > This creates the yml file for you in the repo you choose (see note above) -> change the yml file name to whatever you want -> copy and paste the sample yml file below into the window and then commit changes -> the workflow should run after the commit -> If it is successful pull into all the branches to make sure all the branchs in the repo share the same yml files.
- Note: It seems easier to create the workflows directly on Github on the main branch and then pull these to other branches because Github doesn't seem to allow you to create the workflow to a different branch. However, if you look at the URL when you create the workflow, it is something like this: new/main?filename=.github%2Fworkflows%2Fmain.yml&workflow_template=blank, so I believe you can change the main, in the url, to the branch name and it will work.
4. Done
Now, whenever you commit to the branch specified in the yml file, Workers will deploy the new code. You can test it by pushing new code (never push code directly to main, by the way), and then log into Cloudflare -> Workers -> Click on Worker -> Deployment and you should see your deployment there. Also, clicking on Actions in the Github repo will show you all the latest runs and if there are errors, you can see them there.
5. Sample yml file
You can use the sample file below. Note that you can create separate files for each branch. Just change the branches name in the workflow below, to whatever the name of the branch is. In the file below, we are using "main". Also, you can change the command to build a specific environment. So if you have a production environment in the worker, for example, you can edit the command to this: command: deploy --env production
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
name: Deploy
steps:
- uses: actions/checkout@v4
- name: Deploy
uses: cloudflare/wrangler-action@v3
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
command: deploy