-
Notifications
You must be signed in to change notification settings - Fork 48
251 lines (220 loc) · 8.79 KB
/
Copy pathdeploy-workers.yml
File metadata and controls
251 lines (220 loc) · 8.79 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
name: Deploy Cloudflare Workers
on:
workflow_dispatch:
inputs:
worker:
description: 'Worker folder to deploy (e.g. services/app-builder)'
required: true
type: string
target_environment:
description: 'Worker environment to deploy to'
required: false
default: production
type: choice
options:
- production
- staging
workflow_call:
inputs:
worker:
description: 'Worker folder to deploy for single-worker calls'
required: false
default: ''
type: string
base_sha:
description: 'Base SHA to diff against when detecting changed workers'
required: false
default: ''
type: string
target_environment:
description: 'Worker environment to deploy to'
required: false
default: production
type: string
permissions:
contents: read
concurrency:
group: deploy-workers
cancel-in-progress: false
jobs:
deploy-manual:
if: inputs.worker != ''
runs-on: ${{ vars.RUNNER_DEFAULT_LABEL || 'ubuntu-latest' }}
timeout-minutes: 15
name: Deploy ${{ inputs.worker }}
environment: ${{ inputs.target_environment }}
steps:
- name: Checkout code
uses: useblacksmith/checkout@41cdeedae8edb2e684ba22896a5fd2a3cb85db6b # v1
- name: Setup pnpm
uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v4.4.0
- name: Setup Node.js
uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6.3.0
with:
node-version-file: '.nvmrc'
cache: 'pnpm'
- name: Install dependencies
working-directory: ${{ inputs.worker }}
run: pnpm install --frozen-lockfile
- name: Deploy to Cloudflare Workers
uses: cloudflare/wrangler-action@da0e0dfe58b7a431659754fdf3f186c529afbe65 # v3.14.1
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
workingDirectory: ${{ inputs.worker }}
# Workers that define a `predeploy` script (e.g. D1 migrations) run it
# right before deploy; all other workers are unaffected.
preCommands: |
if [ "$(jq -r '.scripts.predeploy // empty' package.json)" != "" ]; then pnpm run predeploy; fi
command: ${{ inputs.target_environment == 'production' && 'deploy' || format('deploy --env {0}', inputs.target_environment) }}
detect-changes:
if: inputs.worker == '' && (inputs.base_sha != '' || inputs.target_environment != 'production')
runs-on: ${{ vars.RUNNER_DEFAULT_LABEL || 'ubuntu-latest' }}
timeout-minutes: 5
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- name: Checkout code
uses: useblacksmith/checkout@41cdeedae8edb2e684ba22896a5fd2a3cb85db6b # v1
with:
fetch-depth: 0
- name: Find changed workers
id: set-matrix
run: |
# Auto-discover all deployable workers (folders containing wrangler.jsonc)
# and exclude workers that have their own deploy pipelines.
#
# Production diffs against the SHA before the push so that multi-commit
# pushes don't miss workers touched in earlier commits. Staging deploys
# only workers that explicitly define the named Wrangler environment.
BASE_SHA="${{ inputs.base_sha }}"
TARGET_ENVIRONMENT="${{ inputs.target_environment }}"
has_named_environment() {
node - "$1" "$TARGET_ENVIRONMENT" <<'NODE'
const fs = require('node:fs');
const [file, targetEnvironment] = process.argv.slice(2);
function stripJsonComments(value) {
let output = '';
let inString = false;
let escaped = false;
for (let index = 0; index < value.length; index += 1) {
const current = value[index];
const next = value[index + 1];
if (inString) {
output += current;
if (escaped) escaped = false;
else if (current === '\\') escaped = true;
else if (current === '"') inString = false;
continue;
}
if (current === '"') {
inString = true;
output += current;
continue;
}
if (current === '/' && next === '/') {
while (index < value.length && value[index] !== '\n') index += 1;
output += '\n';
continue;
}
if (current === '/' && next === '*') {
index += 2;
while (index < value.length && !(value[index] === '*' && value[index + 1] === '/')) {
if (value[index] === '\n') output += '\n';
index += 1;
}
index += 1;
continue;
}
output += current;
}
return output;
}
const rawConfig = fs.readFileSync(file, 'utf8');
const config = JSON.parse(stripJsonComments(rawConfig).replace(/,\s*([}\]])/g, '$1'));
process.exit(config.env && Object.hasOwn(config.env, targetEnvironment) ? 0 : 1);
NODE
}
# Workers excluded from this workflow (they have custom deploy pipelines):
EXCLUDED=(
services/kiloclaw # Docker-based deploy in deploy-production.yml
services/gastown # Deployed separately
services/wasteland # Deployed separately
services/deploy-infra/builder-docker-container/container-files # Build artifact template, not a deployable worker
)
# Discover all workers with a wrangler.jsonc
WORKERS=()
while IFS= read -r cfg; do
dir=$(dirname "$cfg")
WORKERS+=("$dir")
done < <(find services -name "wrangler.jsonc" -not -path "*/node_modules/*" | sort)
# Filter out excluded workers
DEPLOYABLE=()
for dir in "${WORKERS[@]}"; do
skip=false
for excluded in "${EXCLUDED[@]}"; do
if [[ "$dir" == "$excluded" ]]; then
skip=true
break
fi
done
if [[ "$skip" == false ]]; then
DEPLOYABLE+=("$dir")
fi
done
if [ "$TARGET_ENVIRONMENT" = "production" ]; then
CHANGED=()
for dir in "${DEPLOYABLE[@]}"; do
if git diff --name-only "$BASE_SHA" HEAD -- "$dir/" | grep -q .; then
CHANGED+=("$dir")
fi
done
else
CHANGED=()
for dir in "${DEPLOYABLE[@]}"; do
if has_named_environment "$dir/wrangler.jsonc"; then
CHANGED+=("$dir")
else
echo "Skipping $dir: wrangler.jsonc does not define env.$TARGET_ENVIRONMENT"
fi
done
fi
if [ ${#CHANGED[@]} -eq 0 ]; then
echo "matrix=[]" >> "$GITHUB_OUTPUT"
else
MATRIX=$(printf '%s\n' "${CHANGED[@]}" | jq -R . | jq -sc .)
echo "matrix=$MATRIX" >> "$GITHUB_OUTPUT"
fi
deploy-changed:
needs: detect-changes
if: needs.detect-changes.outputs.matrix != '[]' && needs.detect-changes.outputs.matrix != ''
runs-on: ${{ vars.RUNNER_DEFAULT_LABEL || 'ubuntu-latest' }}
timeout-minutes: 15
environment: ${{ inputs.target_environment }}
strategy:
fail-fast: false
matrix:
worker: ${{ fromJson(needs.detect-changes.outputs.matrix) }}
name: Deploy ${{ matrix.worker }}
steps:
- name: Checkout code
uses: useblacksmith/checkout@41cdeedae8edb2e684ba22896a5fd2a3cb85db6b # v1
- name: Setup pnpm
uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v4.4.0
- name: Setup Node.js
uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6.3.0
with:
node-version-file: '.nvmrc'
cache: 'pnpm'
- name: Install dependencies
working-directory: ${{ matrix.worker }}
run: pnpm install --frozen-lockfile
- name: Deploy to Cloudflare Workers
uses: cloudflare/wrangler-action@da0e0dfe58b7a431659754fdf3f186c529afbe65 # v3.14.1
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
workingDirectory: ${{ matrix.worker }}
# Workers that define a `predeploy` script (e.g. D1 migrations) run it
# right before deploy; all other workers are unaffected.
preCommands: |
if [ "$(jq -r '.scripts.predeploy // empty' package.json)" != "" ]; then pnpm run predeploy; fi
command: ${{ inputs.target_environment == 'production' && 'deploy' || format('deploy --env {0}', inputs.target_environment) }}