💸Save up to $132K/month in CI costs!👉 Try Free
Skip to main content
← Back to workflows

How to Use Wrangler GitHub Action for Easy Cloudflare Workers Deployment

cloudflare-wrangler-action -
GitHub Action
v3.5.0
1,181
Contributors
Contributor - JacobMGEvansContributor - kristianfreemanContributor - 1000hz
Categories

Optimize Your CI/CD Pipeline

Get instant insights into your CI/CD performance and costs. Reduce build times by up to 45% and save on infrastructure costs.

45% Faster Builds
60% Cost Reduction
Usage
name: 'Usage of wrangler-action GitHub Action'
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Deploy
uses: cloudflare/wrangler-action@v3
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}

wrangler-action logo

Wrangler Action

Easily deploy cloudflare workers applications using wrangler and github actions


What is Wrangler GitHub Action?

Efficient and reliable deployment of Cloudflare Workers requires a streamlined process that integrates well with our CI/CD pipelines. The Wrangler GitHub Action provides a user-friendly and efficient way to automate the deployment of Cloudflare Workers directly from our GitHub repositories. Below, I’ll guide you through setting up this GitHub Action to make deploying Workers a breeze.

How to Authenticate with Cloudflare Using Wrangler GitHub Action

To securely authenticate with Cloudflare’s API, you need to store your API token as a GitHub secret. This token allows Wrangler to interact with your Cloudflare account under the security measures provided by GitHub. Here’s how to set this up:

- uses: cloudflare/wrangler-action@v3
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}

How to Customize Wrangler Configuration in GitHub Actions

Installing a Specific Version of Wrangler

If your project requires a specific version of Wrangler, you can specify this in the workflow.

- uses: cloudflare/wrangler-action@v3
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
wranglerVersion: "2.20.0"

Specifying a Working Directory

If you need to run Wrangler commands from a specific subdirectory within your repository, you can use the workingDirectory parameter.

- uses: cloudflare/wrangler-action@v3
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
workingDirectory: "subfoldername"

How to Inject Worker Secrets Using Wrangler GitHub Action

To securely pass environment-specific secrets to your Worker, use GitHub Secrets to store the values and inject them directly into your Worker's environment via the Wrangler action.

- uses: cloudflare/wrangler-action@v3
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
secrets: |
SECRET1
SECRET2
env:
SECRET1: ${{ secrets.SECRET1 }}
SECRET2: ${{ secrets.SECRET2 }}

Deploying Cloudflare Pages (Production & Preview)

For those managing Cloudflare Pages projects and preferring GitHub Actions over built-in CI, here’s how to configure it for both production and preview environments.

- name: Deploy
uses: cloudflare/wrangler-action@v3
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
command: pages deploy YOUR_DIST_FOLDER --project-name=example

Scheduling Regular Deployments

To deploy your Workers application on a recurring basis, use the schedule trigger in your GitHub Actions workflow.

on:
schedule:
- cron: "0 * * * *"

jobs:
deploy:
runs-on: ubuntu-latest
name: Deploy App Regularly
steps:
- uses: actions/checkout@v4
- name: Deploy Worker
uses: cloudflare/wrangler-action@v3
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}

Manually Triggering Deployments

If you need to deploy your application at-will, configure your workflow to trigger on the workflow_dispatch event.

on:
workflow_dispatch:
inputs:
environment:
description: "Choose an environment to deploy to: <dev|staging|prod>"
required: true
default: "dev"

jobs:
deploy:
runs-on: ubuntu-latest
name: Manual Deploy
steps:
- uses: actions/checkout@v4
- name: Deploy App
uses: cloudflare/wrangler-action@v3
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
command: deploy --env ${{ github.event.inputs.environment }}

Using Wrangler Command Output in Subsequent Steps

When deploying with Wrangler, you might want to capture and log the output or use it for further conditional logic in later steps. Here’s how you can capture and use the command output from a Wrangler deployment:

- name: Deploy
id: deploy
uses: cloudflare/wrangler-action@v3
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
command: pages deploy --project-name=example

- name: Print Wrangler Command Output
env:
CMD_OUTPUT: ${{ steps.deploy.outputs.command-output }}
run: echo $CMD_OUTPUT