UptimeFlare/.github/workflows/deploy.yml

162 lines
7.8 KiB
YAML

name: Deploy to Cloudflare
on:
push:
branches: ['main']
workflow_dispatch:
# Least privilege, pinned in the workflow instead of inherited from the repo's
# `default_workflow_permissions` setting. This job carries CLOUDFLARE_API_TOKEN
# and runs `terraform apply`; if that setting is ever flipped to
# read-and-write — or a new repo is derived from this one — the ambient
# GITHUB_TOKEN would silently gain write access to the repository inside
# exactly that run. `contents: read` is all `actions/checkout` needs; nothing
# in this workflow or in deploy/*.py touches the GitHub API.
# Top-level, not per-job, so a job added later inherits the restriction.
permissions:
contents: read
jobs:
build-and-deploy:
runs-on: ubuntu-latest
# `workflow_dispatch` above can be pointed at ANY ref, and the ruleset
# `main-protection` only governs pushes to main — it does not govern
# workflow runs. Without this binding, a dispatch from any side branch
# runs `terraform apply` + `wrangler pages deploy` against production
# with CLOUDFLARE_API_TOKEN, unreviewed and without any status check.
# The `production` environment carries a deployment_branch_policy that
# allows `main` only; a job bound to it is refused BEFORE its first
# step, so the secrets are never bound.
# Measured 2026-07-31 in this repo (run 30597988048, branch
# probe/env-gate): 'Branch "probe/env-gate" is not allowed to deploy to
# production due to environment protection rules.' — 0 steps executed.
# LIMIT: workflow_dispatch executes the file from the CHOSEN ref, so
# this line only protects refs that carry it. Side branches with an
# older copy of deploy.yml stay dispatchable. The ref-independent
# closure is to move CLOUDFLARE_API_TOKEN/ACCOUNT_ID/PUSHOVER_* from
# repository scope to `production` environment scope — then an ungated
# run receives an empty value (measured: SECRET_LEN=0 vs 20).
environment: production
steps:
- uses: actions/checkout@a37ce9120846195fa4ece8f58b268e6043cb2f26 # v3.7.0
- name: Setup Terraform
uses: hashicorp/setup-terraform@633666f66e0061ca3b725c73b2ec20cd13a8fdd1 # v2.0.3
with:
terraform_version: 1.6.4
- name: Use Node.js 22.x
uses: actions/setup-node@3235b876344d2a9aa001b8d1453c930bba69e610 # v3.9.1
with:
node-version: 22.x
cache: 'npm'
# Automatically get an account id via the API Token
# if secrets.CLOUDFLARE_ACCOUNT_ID is not set.
- name: Fetch Account ID
id: fetch_account_id
run: |
if [[ -n "${{ secrets.CLOUDFLARE_ACCOUNT_ID }}" ]]; then
ACCOUNT_ID="${{ secrets.CLOUDFLARE_ACCOUNT_ID }}"
echo "Using provided CLOUDFLARE_ACCOUNT_ID from secrets."
else
ACCOUNT_ID=$(curl -X GET "https://api.cloudflare.com/client/v4/accounts" -H "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" -H "Content-Type:application/json" | jq ".result[0].id" -r)
if [[ "$ACCOUNT_ID" == "null" ]]; then
echo "Failed to get an account id, please make sure you have set up CLOUDFLARE_API_TOKEN correctly!"
exit 1
fi
fi
echo 'account_id='$ACCOUNT_ID >> $GITHUB_OUTPUT
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
- name: Install packages
run: |
npm install
cd worker
npm install
- name: Build worker
run: |
cd worker
npx wrangler deploy src/index.ts --outdir dist --dry-run
- name: Build page
run: |
npx @cloudflare/next-on-pages
# The "Upload pages" step at the end of this job REPLACES the live site
# with the contents of .vercel/output/static. The build above is
# fail-closed against its own crash — a non-zero exit stops the job — but
# not against exiting 0 with an empty or truncated directory, and until
# now nothing between the build and the upload looked inside it. Assert
# the output here, before `terraform apply`, so a broken build stops the
# run while Cloudflare is still untouched.
- name: Verify page build output
run: scripts/verify-pages-output.sh
- name: Create D1 database and tables
run: |
python3 deploy/init_d1.py # This sets D1_ID in GITHUB_ENV
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ steps.fetch_account_id.outputs.account_id }}
- name: Migrate state from KV (if needed)
run: |
python3 deploy/migrate_kv.py
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ steps.fetch_account_id.outputs.account_id }}
D1_ID: ${{ env.D1_ID }}
- name: Deploy using Terraform
# As we don't save terraform state somewhere, we need to import the existing resources
run: |
terraform init
# The DO migration (new_tag = "v1") may only be applied ONCE, at the
# worker's initial creation. If the worker already exists it is already
# at tag "v1", and re-sending the migration makes Cloudflare reject the
# apply with 412 / code 10079 ("Actor migration tag precondition failed,
# got tag '' when expected tag is 'v1'"). So gate the migration purely on
# worker-script existence, not on a (flaky) durable-object namespace query.
WORKER_HTTP=$(curl -s -o /dev/null -w "%{http_code}" \
"https://api.cloudflare.com/client/v4/accounts/$TF_VAR_CLOUDFLARE_ACCOUNT_ID/workers/scripts/uptimeflare_worker" \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN")
if [[ "$WORKER_HTTP" == "200" ]]; then
echo "Worker uptimeflare_worker already exists (HTTP $WORKER_HTTP); DO migration v1 already applied. No migration needed."
else
echo "Worker uptimeflare_worker not found (HTTP $WORKER_HTTP). Applying initial DO migration."
export TF_VAR_enable_do_migration=true
fi
echo "Try importing existing resources..."
terraform import cloudflare_d1_database.uptimeflare_d1 "$TF_VAR_CLOUDFLARE_ACCOUNT_ID/$D1_ID"
terraform import cloudflare_workers_script.uptimeflare_worker "$TF_VAR_CLOUDFLARE_ACCOUNT_ID/uptimeflare_worker" || echo "WARNING: Worker script import failed, continuing..."
terraform import cloudflare_workers_cron_trigger.uptimeflare_worker_cron "$TF_VAR_CLOUDFLARE_ACCOUNT_ID/uptimeflare_worker" || echo "WARNING: Cron trigger import failed, continuing..."
terraform import cloudflare_pages_project.uptimeflare "$TF_VAR_CLOUDFLARE_ACCOUNT_ID/uptimeflare" || echo "WARNING: Pages project import failed, continuing..."
terraform apply -auto-approve -input=false
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
TF_VAR_CLOUDFLARE_ACCOUNT_ID: ${{ steps.fetch_account_id.outputs.account_id }}
D1_ID: ${{ env.D1_ID }}
# Alert channel (worker/src/pushover.ts). If these repository secrets
# are unset the apply still succeeds, the bindings are simply omitted,
# and the worker logs "[ALERT-CHANNEL-BROKEN] … missing Worker
# secret(s) …" for every alert it could not deliver.
TF_VAR_PUSHOVER_TOKEN: ${{ secrets.PUSHOVER_TOKEN }}
TF_VAR_PUSHOVER_USER: ${{ secrets.PUSHOVER_USER }}
# Currently Terraform Cloudflare provider doesn't support direct upload, use wrangler to upload instead.
- name: Upload pages
run: |
npx wrangler pages deploy .vercel/output/static --project-name uptimeflare
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ steps.fetch_account_id.outputs.account_id }}