It is very easy to get automated deployments to Cloudflare Workers using builds.sr.ht.
First you need a Cloudflare Account API Token. Ensure it has edit access to the following scopes:
- Account: Workers Scripts (to create scripts)
- Zone: Workers Routes (to create custom domains)
Save the API Token in a new builds.sr.ht secret. This will give you a secret id that we will need in the following config:
image: alpine/latest
packages:
- nodejs
- npm
secrets:
- c0e267ac-a8a0-4f76-a55b-1ed51f9313ad # to access private ssh repos
- 0a5e47cf-85e0-4c4e-bbe4-4b56c0c1c457 # cloudflare deployer key
tasks:
- deploy-workers: |
# Only deploy on push to main
if [[ "$GIT_REF" = "refs/heads/main" ]]; then
set +x
# Required to help wrangler know which account to push to.
export CLOUDFLARE_ACCOUNT_ID=<your account id>
# The filename from the secret created before
export CLOUDFLARE_API_TOKEN=$(cat ~/.cloudflare)
set -x
cd repo
npx wrangler deploy
done
fi