/wrangler-action-node

🧙‍♀️ zero-config cloudflare workers application deployment using wrangler and github actions

Primary LanguageTypeScriptApache License 2.0Apache-2.0

Wrangler GitHub Action (Node Edition)


Update Aug 7, 2023

The official @cloudflare/wrangler-action has now updated to v3.0.0 which is was also converted to TypeScript and no external deps (from Docker + Linux + Bash). As a result, this project will no longer be maintained and I recommend switching to the official one. This repo will be left up but archived.


This is a direct logic copy of @cloudflare/wrangler-action but written in pure Node.JS without any external dependencies

Easy-to-use GitHub Action to use Wrangler. Makes deploying Workers, Pages or modifying R2 easy to do.

Usage

Add wrangler-action to the workflow for your Workers/Pages application. The below example will publish a Worker on a git push to the main branch:

name: Deploy

on:
    push:
        branches:
            - main

jobs:
    deploy:
        runs-on: ubuntu-latest
        name: Deploy
        steps:
            - uses: actions/checkout@v2
            - name: Publish
              uses: demosjarco/wrangler-action-node@v1
              with:
                  apiToken: ${{ secrets.CF_API_TOKEN }}

Authentication

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 the Workers 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 the with block of your workflow. Below, I've set the secret name to CF_API_TOKEN:

jobs:
    deploy:
        name: Deploy
        steps:
            uses: demosjarco/wrangler-action-node@v1
            with:
                apiToken: ${{ secrets.CF_API_TOKEN }}

wrangler-action also supports using your global API key and email as an authentication method, although API tokens are preferred. Pass in apiKey and email to the GitHub Action to use this method:

jobs:
    deploy:
        name: Deploy
        steps:
            uses: demosjarco/wrangler-action-node@v1
            with:
                apiKey: ${{ secrets.CF_API_KEY }}
                email: ${{ secrets.CF_EMAIL }}

Configuration

If you need to install a specific version of Wrangler to use for deployment, you can also pass the input wranglerVersion to install a specific version of Wrangler from NPM. This should be a SemVer-style version number, such as 1.6.0:

jobs:
    deploy:
        steps:
            uses: demosjarco/wrangler-action-node@v1
            with:
                apiToken: ${{ secrets.CF_API_TOKEN }}
                wranglerVersion: '1.6.0'

Optionally, you can also pass a workingDirectory 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: demosjarco/wrangler-action-node@v1
            with:
                apiToken: ${{ secrets.CF_API_TOKEN }}
                workingDirectory: 'subfoldername'

Worker secrets can be optionally passed as a new line deliminated string of names in secrets. Each secret name must match an environment variable name specified in the env attribute. Creates or replaces the value for the Worker secret using the wrangler secret put command.

jobs:
    deploy:
        steps:
            uses: demosjarco/wrangler-action-node@v1
            with:
                apiToken: ${{ secrets.CF_API_TOKEN }}
                secrets: |
                    SECRET1
                    SECRET2
            env:
                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 to preCommands (before publish) or postCommands (after publish). These can include additional wrangler commands (that is, whoami, kv:key put) or any other commands available inside the wrangler-action context.

jobs:
    deploy:
        steps:
            uses: demosjarco/wrangler-action-node@v1
            with:
                apiToken: ${{ secrets.CF_API_TOKEN }}
                preCommands: echo "*** pre command ***"
                postCommands: |
                    echo "*** post commands ***"
                    wrangler kv:key put --binding=MY_KV key2 value2
                    echo "******"

You can use the command option to do specific actions such as running wrangler whoami against your project:

jobs:
    deploy:
        steps:
            uses: demosjarco/wrangler-action-node@v1
            with:
                apiToken: ${{ secrets.CF_API_TOKEN }}
                command: whoami

Sponsors

JamesChainFuse

More Info

See the wiki for more information