Notes regarding Gatsby deployment to a Cloudflare Worker using GitLab CI

April 17, 2021

Rough notes below for now

package.json

Added a scripts line to facilitate build and deploy: "publish": "npm run build && wrangler publish"

gitlab-ci.yml

image: node:latest

cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - node_modules/
    - .cache/
    - public/

before_script:
  - npm --version
  - node --version
  - npm install -g gatsby-cli
  - npm install -g @cloudflare/wrangler

build:
  script:
    # Use `npm ci` because it's faster than `npm install` and designed for this context.
    - npm ci
    # Env vars are CF_ACCOUNT_ID, CF_API_TOKEN, CF_ZONE_ID and set in GitLab CI settings. 
    # Note that these variables must *not* be protected.
    - npm run publish
  only:
    - main

GitLab CI configuration

Add Cloudflare-related environment variables to GitLab CI/CD settings for the project, under “Variables” Variables should not be protected, otherwise GitLab CI won’t see them properly (banged my head against this for a bit).

The variables are self-explanatory as you setup with your Cloudflare tokens:

  • CF_ACCOUNT_ID
  • CF_API_TOKEN
  • CF_ZONE_ID

References

  • [https://www.gatsbyjs.com/docs/deploying-to-gitlab-pages/]
  • [https://blog.vence.tech/posts/ci-cd-pipeline-on-gitlab/]

Profile picture

Written by Jeff Beeman Goodjer, Sun Devil, Acquian, Drupalist. He/him. Doogiemac on most things. You should follow them on Twitter