wrangler-action

repository·main·Indexed 23 days ago

https://github.com/cloudflare/wrangler-action

GitHub Action to automate the deployment of Cloudflare Workers and Pages using Wrangler. It supports custom Wrangler versions, environment variable management via --var, secret uploads, and execution of pre/post commands. The action provides output variables for deployment URLs and command logs, and supports multiple package managers including npm, yarn, pnpm, and bun.

Tokens
4.4K
Snippets
14
Records
20
Agent score
80%

What's inside wrangler-action

  1. Access Wrangler command output and error logs

    main

    You can capture the output of the Wrangler command to use in subsequent GitHub Action steps using the following output variables:

    • command-output: The standard output (stdout) of the Wrangler command.
    • command-stderr: The standard error (stderr) of the Wrangler command.

    To use them, ensure you assign an id to the wrangler-action step.

    - name: Deploy
      id: deploy
      uses: cloudflare/wrangler-action@v4
      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
  2. Quickstart: Deploy a Worker on git push

    main

    To deploy a Cloudflare Worker automatically whenever code is pushed to your main branch, add the cloudflare/wrangler-action@v4 to your GitHub Actions workflow. You must first add your Cloudflare API token to your repository's Secrets (e.g., as CLOUDFLARE_API_TOKEN).

    name: Deploy
    
    on:
      push:
        branches:
          - main
    
    jobs:
      deploy:
        runs-on: ubuntu-latest
        name: Deploy
        steps:
          - uses: actions/checkout@v6
          - name: Deploy
            uses: cloudflare/wrangler-action@v4
            with:
              apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
  3. Authenticate the Wrangler Action

    main

    The action requires a Cloudflare API token to interact with your account.

    1. Go to your GitHub repository settings: Settings -> Secrets and variables -> Actions.
    2. Add a new repository secret named CLOUDFLARE_API_TOKEN (or any name you prefer).
    3. Pass this secret to the action using the apiToken input in your workflow file.
    jobs:
      deploy:
        name: Deploy
        steps:
          uses: cloudflare/wrangler-action@v4
          with:
            apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
  4. Deploy Cloudflare Pages (Production & Preview)

    main

    To deploy a Cloudflare Pages project using this action, use the command input with pages deploy. If the workflow is triggered by a push to a non-production branch, Wrangler will automatically create a preview deployment. You must provide the accountId and can optionally provide gitHubToken to enable GitHub Deployments integration.

    on: [push]
    
    jobs:
      deploy:
        runs-on: ubuntu-latest
        name: Deploy
        permissions:
          contents: read
          deployments: write
        steps:
          - uses: actions/checkout@v6
          - name: Deploy
            uses: cloudflare/wrangler-action@v4
            with:
              apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
              accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
              command: pages deploy YOUR_DIST_FOLDER --project-name=example
              gitHubToken: ${{ secrets.GITHUB_TOKEN }}
  5. Set Worker secrets for a specific environment

    main

    To set a secret for a specific Wrangler environment, use the environment input to specify the target environment and the secrets input to provide a list of secret names. The actual values for these secrets must be passed as environment variables in the env block of the step.

    - uses: cloudflare/wrangler-action@v4
      with:
        apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
        command: deploy --env production
        secrets: |
          SUPER_SECRET
        environment: production
      env:
        SUPER_SECRET: ${{ secrets.SUPER_SECRET }}
  6. Upload a Worker Version

    main

    To create a new version of your Worker without deploying it immediately, use the versions upload command. This requires Wrangler v3.40.0 or above. The uploaded version can then be deployed later via the Cloudflare dashboard or the wrangler versions deploy command.

    jobs:
      upload:
        runs-on: ubuntu-latest
        name: Deploy
        steps:
          - uses: actions/checkout@v6
          - name: Upload Worker Version
            uses: cloudflare/wrangler-action@v4
            with:
              apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
              accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
              command: versions upload
  7. Specify a custom package manager

    main

    The action automatically detects the package manager by looking for lock files (package-lock.json, yarn.lock, pnpm-lock.yaml, or bun.lockb/bun.lock). To override this detection, use the packageManager input. Supported values are npm, yarn, pnpm, or bun.

    jobs:
      deploy:
        steps:
          uses: cloudflare/wrangler-action@v4
          with:
            apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
            packageManager: pnpm
  8. Configure workingDirectory

    main

    If your Wrangler configuration files (like wrangler.toml) are located in a subdirectory of your repository, use the workingDirectory input to specify that path.

    jobs:
      deploy:
        steps:
          uses: cloudflare/wrangler-action@v4
          with:
            apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
            workingDirectory: "subfoldername"
  9. Upload secrets to Cloudflare

    main

    The action can automatically upload secrets to your Cloudflare account. You provide a list of secret names in the secrets input. The action retrieves the values from the GitHub Actions environment and uses wrangler secret bulk (or wrangler secret:bulk for Wrangler versions < 3.60.0) to upload them.

    If an ENVIRONMENT is specified, the --env flag is automatically included in the command. For Wrangler versions older than 3.4.0, the action falls back to individual wrangler secret put commands.

  10. Manage Worker Secrets and Environments

    main

    You can pass Worker secrets to the action using the secrets input. Each secret name must be provided as a string of names separated by newlines. To make these values available to the action, you must also define them in the env block of the step, mapping the secret names to your GitHub repository secrets.

    jobs:
      deploy:
        steps:
          uses: cloudflare/wrangler-action@v4
          with:
            apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
            environment: production
            secrets: |
              SECRET1
              SECRET2
          env:
            SECRET1: ${{ secrets.SECRET1 }}
            SECRET2: ${{ secrets.SECRET2 }}
  11. Run preCommands and postCommands

    main

    Use preCommands to run shell commands or additional wrangler commands before the main deployment, and postCommands to run them after. This is useful for tasks like setting KV keys or running diagnostic commands.

    jobs:
      deploy:
        steps:
          uses: cloudflare/wrangler-action@v4
          with:
            apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
            preCommands: echo "*** pre command ***"
            postCommands: |
              echo "*** post commands ***"
              wrangler kv:key put --binding=MY_KV key2 value2
              echo "******"
  12. Configure wranglerVersion

    main

    By default, the action uses Wrangler v4. You can pin a specific version of Wrangler using the wranglerVersion input. This accepts any NPM-compatible version format, including exact versions, major versions, ranges, or latest.

    jobs:
      deploy:
        steps:
          uses: cloudflare/wrangler-action@v4
          with:
            apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
            wranglerVersion: "4"