Q. What is this Ci/CD nonsense?

A: CI/CD bridges the gaps between development and operation activities and teams by enforcing automation in building, testing and deployment of applications. https://en.wikipedia.org/wiki/CI/CD

Q. What is Cloudflare Workers?

A: Cloudflare Workers provides a serverless execution environment that allows you to create entirely new applications or augment existing ones without configuring or maintaining infrastructure. https://workers.cloudflare.com/

Prerequisites:

  • Cloudflare Workers
  • Gitlab
  • Cloudflare Workers Sites project
  • A static web site/application, even a hello world would work.

Goals:

  • Allow multiple environments. (Basically different versions of your application running in different modes or capacities ex. production, staging, testing etc.)
  • Not use any third party deployment clients. (Like https://www.serverless.com/)
  • Not leak any sensitive information. (Ex. API Tokens, Passwords etc.)
  • Free

Example Configs

wrangler.toml

Defines the structure and order of the cloudflare worker environments.

name = "8192-webapp"
type = "webpack"
account_id = "12345678902345678901234567890"
zone_id = "12345678902345678901234567890"

[site]
bucket = "." # The folder with your static assets
entry-point = "workers-site"

[env.staging]
name = "8192-staging"
route = "staging.8192.one/*"

[env.production]
name = "8192-production"
routes = [
  "8192.one/*"
]

.gitlab-ci.yml

Defines the structure and order of the pipelines.

image: node:latest
stages:
  - deploy

deploy_staging:
  stage: deploy
  script:
    - echo "Deploy to staging"
    - npm install -g @cloudflare/wrangler
    - CF_API_TOKEN=$CF_API_TOKEN wrangler publish --env staging
  environment:
    name: staging
    url: https://staging.8192.one

deploy_prod:
  stage: deploy
  script:
    - echo "Deploy to production"
    - npm install -g @cloudflare/wrangler
    - CF_API_TOKEN=$CF_API_TOKEN wrangler publish --env production
  environment:
    name: production
    url: https://8192.one
  when: manual
  only:
  - master

Alternative: If this doesn’t work for you. Try downloading and running the wrangler directly.

deploy_staging:
  stage: deploy
  script:
    - echo "Deploy to staging"
    - wget https://github.com/cloudflare/wrangler/releases/download/v1.10.3/wrangler-v1.10.3-x86_64-unknown-linux-musl.tar.gz
    - tar xvzf wrangler-v1.10.3-x86_64-unknown-linux-musl.tar.gz
    - CF_API_TOKEN=$CF_API_TOKEN ./dist/wrangler publish --env staging

Environment Varibles for API tokens

Congratulations! Your pipeline is ready, go run a few commits!