home

Deploying Workers Projects From SourceHut Builds

Here I will walk through how to deploy a Cloudflare Workers project from the builds system on SourceHut. Doing so will allow you to automatically publish your workers project when you push to your SourceHut repository or however else you’d like to trigger the build.

Secrets

In order to publish our workers project we will need to store our Cloudflare API key in SourceHut. We can then provide our API key when publishing our project to authenticate with Cloudflare. You can find documentation about secrets on SourceHut here.

To create your secret first follow this guide to get your Cloudflare API key. You will likely want to use the “Edit Cloudflare Workers” template so that your key has appropriate permissions.

Once you have your API key, create a file secret here. Call it CF_API_TOKEN and when asked to specify where it should be located place it at ~/.CF_API_TOKEN. Using the 644 permissions will work fine.

Build Manafest

On SourceHut you create builds with build manafests. In order to automatically deploy our workers project we will create a build manafest. To test our manafest we can use SourceHut’s Submit Manafest tool.

Our Manafest will look like this:

image: alpine/edge
packages:
- nodejs
- npm
secrets:
- <UUID OF YOUR SECRET>
sources:
- <REPOSITORY YOU WANT TO DEPLOY>
tasks:
- deploy: |
    cd <REPOSITORY YOU WANT TO DEPLOY>
    wget https://github.com/cloudflare/wrangler/releases/download/v1.19.0/wrangler-v1.19.0-x86_64-unknown-linux-musl.tar.gz
    tar -xf wrangler-v1.19.0-x86_64-unknown-linux-musl.tar.gz
    cp dist/wrangler .
    set +x
    CF_API_TOKEN=$(sudo cat ~/.CF_API_TOKEN) ./wrangler publish
    set -x

Going through this, we first specify that we would like to use the alpine/edge Linux image. We then specify that we need the nodejs and npm packages which are requirements for using wrangler.

In order to use our secret we specify its UUID in the secrets: section and we also specify what repository we’d like to deploy in the sources: section.

Our deploy script is reasonably simple. It just moves into our repository, downloads wrangler and then using our secret from earlier triggers a deploy.

I’d reccomend testing this managest with SourceHut’s Submit Manafest tool before you add it to your repository. Once you’re satisified it is working you can add it to a .build.yml file in the root of your repository and SourceHut will automatically run it whenever you push to your repository.

You can find an example of this setup here.

Thanks for reading.