Repository navigation
120 lines (103 loc) · 4.98 KB
/
Copy pathdeploy.yml
File metadata and controls
120 lines (103 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
name: Deploy to Cloudflare Workers
on:
push:
branches: [main]
workflow_dispatch:
# Only the latest run per branch should deploy.
concurrency:
group: deploy-${{ github.ref }}
cancel-in-progress: true
# This workflow only checks out code and talks to the Cloudflare API.
# Deployment uses CLOUDFLARE_API_TOKEN, not the GITHUB_TOKEN.
permissions:
contents: read
jobs:
deploy:
name: Build and deploy worker
runs-on: ubuntu-latest
timeout-minutes: 25
steps:
- name: Checkout
uses: actions/checkout@v4
# Fail fast BEFORE the expensive build. A referenced-but-unset secret
# expands to an empty string, so `-z` detects it. GitHub does not allow
# secrets in `if:` conditionals, so this step runs unconditionally and
# exits non-zero on a missing value. The token value is never printed —
# only its name.
- name: Verify required secrets
shell: bash
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
AUTH_SECRET: ${{ secrets.AUTH_SECRET }}
run: |
set -euo pipefail
fail=0
# "$2" is the value; "${2// /}" treats a whitespace-only value as blank.
# Only the name ($1, a hardcoded literal) is ever printed.
require() {
if [[ -z "${2// /}" ]]; then
echo "::error::Required secret '$1' is missing or empty. Add it under Settings → Secrets and variables → Actions."
fail=1
fi
}
require CLOUDFLARE_API_TOKEN "${CLOUDFLARE_API_TOKEN:-}"
require CLOUDFLARE_ACCOUNT_ID "${CLOUDFLARE_ACCOUNT_ID:-}"
require AUTH_SECRET "${AUTH_SECRET:-}"
# Wrangler validates CLOUDFLARE_ACCOUNT_ID against ^[A-Za-z0-9_-]+$
# and aborts the deploy on any other character. A value copied from a
# terminal or an .env file often carries a trailing newline, surrounding
# quotes or a stray "VAR=" prefix, all of which trip that check while
# the ID itself is correct. Strip them, then validate, so a fixable
# value is caught here in seconds rather than after the full build.
account_id="${CLOUDFLARE_ACCOUNT_ID:-}"
account_id="${account_id#"${account_id%%[![:space:]]*}"}"
account_id="${account_id%"${account_id##*[![:space:]]}"}"
account_id="${account_id#CLOUDFLARE_ACCOUNT_ID=}"
account_id="${account_id#\"}"; account_id="${account_id%\"}"
account_id="${account_id#\'}"; account_id="${account_id%\'}"
account_id="${account_id#"${account_id%%[![:space:]]*}"}"
account_id="${account_id%"${account_id##*[![:space:]]}"}"
if [[ -n "$account_id" && ! "$account_id" =~ ^[A-Za-z0-9_-]+$ ]]; then
echo "::error::CLOUDFLARE_ACCOUNT_ID contains characters other than alphanumerics, '-' and '_'. Copy the 32-character hex ID from the Cloudflare dashboard (any account → Overview → Account ID), not the account name or the whole 'CLOUDFLARE_ACCOUNT_ID=...' line."
fail=1
fi
if [[ "$fail" -ne 0 ]]; then
echo "::error::Aborting before build: one or more required secrets are missing."
exit 1
fi
# Hand the normalized ID to the deploy step via $GITHUB_ENV. Writing
# a masked secret's trimmed form here is safe: it never reaches the
# log, and the deploy step reads it back verbatim.
echo "CLOUDFLARE_ACCOUNT_ID=$account_id" >> "$GITHUB_ENV"
echo "All required secrets are present."
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- name: Install dependencies
run: npm ci
# OpenNext bakes runtime env vars into .open-next/cloudflare/next-env.mjs at
# BUILD time, and its extractProjectEnvVars() reads ONLY .env* files (never
# process.env). .env.local is gitignored, so without materializing an env
# file here the CI build emits an empty env map and process.env.AUTH_SECRET
# is undefined at runtime -> next-auth throws MissingSecret + UntrustedHost.
- name: Write env file for build
shell: bash
env:
AUTH_SECRET: ${{ secrets.AUTH_SECRET }}
run: |
set -euo pipefail
printf 'AUTH_SECRET=%s\n' "$AUTH_SECRET" > .env.production.local
printf 'AUTH_TRUST_HOST=true\n' >> .env.production.local
echo ".env.production.local written (gitignored)"
- name: Build OpenNext worker
run: npm run build:worker
- name: Deploy to Cloudflare Workers
run: npx wrangler deploy
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
# Normalized by the "Verify required secrets" step above, so wrangler
# never sees a trailing newline or quotes around the raw secret.
CLOUDFLARE_ACCOUNT_ID: ${{ env.CLOUDFLARE_ACCOUNT_ID }}