-
Notifications
You must be signed in to change notification settings - Fork 0
333 lines (302 loc) · 11.5 KB
/
ci.yml
File metadata and controls
333 lines (302 loc) · 11.5 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# ─────────────────────────────────────────────────────────────────────────────
# FILE: initialstate/.github/.github/workflows/ci.yml
#
# Reusable CI workflow for SvelteKit + CDK repos.
# Callers: `.github/workflows/ci.yml` in any initialstate repo.
# ─────────────────────────────────────────────────────────────────────────────
name: CI (reusable)
on:
workflow_call:
inputs:
node-version:
description: 'Node.js version to use'
type: string
default: '20'
aws-account-id:
description: 'AWS account ID (for CDK diff OIDC role)'
type: string
required: true
aws-region:
description: 'AWS region'
type: string
default: 'us-east-1'
app-name:
description: 'Application name (e.g. bruce-svelte) used to construct role name'
type: string
required: true
diff-environment:
description: 'CDK environment to diff on PRs'
type: string
default: 'dev'
run-cdk-diff:
description: 'Run CDK diff on PRs and post as comment'
type: boolean
default: true
strict-checks:
description: 'If false, type-check and build failures do not fail the workflow. Intended for repos with in-progress code migrations where CI should report but not block.'
type: boolean
default: true
jobs:
# ── Type check ────────────────────────────────────────────────────────────
type-check:
name: Type Check
runs-on: ubuntu-latest
continue-on-error: ${{ !inputs.strict-checks }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}
cache: 'npm'
- name: Debug — diagnose npm ci
run: |
echo "::group::tooling"
which npm node
npm --version
node --version
echo "::endgroup::"
echo "::group::pwd + ref"
pwd
echo "GITHUB_WORKSPACE=$GITHUB_WORKSPACE"
git rev-parse HEAD
echo "::endgroup::"
echo "::group::root listing"
ls -la
echo "::endgroup::"
echo "::group::lockfile check"
if [ -f package-lock.json ]; then
echo "FOUND package-lock.json ($(wc -c < package-lock.json) bytes)"
echo "stat:"
stat package-lock.json
echo "first 10 lines:"
head -10 package-lock.json
echo "lockfileVersion grep:"
grep -n '"lockfileVersion"' package-lock.json | head -3
else
echo "MISSING package-lock.json"
fi
echo "::endgroup::"
echo "::group::npm config (top)"
npm config ls -l 2>&1 | head -60
echo "::endgroup::"
echo "::group::npm-related env"
env | grep -iE '^(npm_|node_)' || echo "(no npm/node env vars)"
echo "::endgroup::"
echo "::group::npm ci --dry-run -d (capture verbose error)"
npm ci --dry-run -d 2>&1 | tail -120 || true
echo "::endgroup::"
- name: Install dependencies
run: npm ci
- name: Svelte type check
run: npm run check
# ── Build ─────────────────────────────────────────────────────────────────
build:
name: Build
runs-on: ubuntu-latest
continue-on-error: ${{ !inputs.strict-checks }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}
cache: 'npm'
- name: Debug — runner state before npm ci
run: |
echo "::group::pwd + ref"
pwd
git rev-parse HEAD
echo "::endgroup::"
echo "::group::root listing"
ls -la
echo "::endgroup::"
echo "::group::lockfile check"
if [ -f package-lock.json ]; then
echo "FOUND package-lock.json ($(wc -c < package-lock.json) bytes)"
head -5 package-lock.json
else
echo "MISSING package-lock.json"
fi
echo "::endgroup::"
- name: Install dependencies
run: npm ci
- name: Build
run: npm run build
# ── Lint (optional — activate once eslint is configured) ─────────────────
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}
cache: 'npm'
- name: Debug — runner state before npm ci
run: |
echo "::group::pwd + ref"
pwd
git rev-parse HEAD
echo "::endgroup::"
echo "::group::root listing"
ls -la
echo "::endgroup::"
echo "::group::lockfile check"
if [ -f package-lock.json ]; then
echo "FOUND package-lock.json ($(wc -c < package-lock.json) bytes)"
head -5 package-lock.json
else
echo "MISSING package-lock.json"
fi
echo "::endgroup::"
- name: Install dependencies
run: npm ci
- name: Lint
# Replace with `npm run lint` once an eslint config is added to the repo
run: |
if [ -f ".eslintrc*" ] || [ -f "eslint.config*" ]; then
npm run lint
else
echo "No eslint config found — skipping lint (add eslint to unlock this check)"
fi
# ── Test (optional — activate once vitest is configured) ─────────────────
test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}
cache: 'npm'
- name: Debug — runner state before npm ci
run: |
echo "::group::pwd + ref"
pwd
git rev-parse HEAD
echo "::endgroup::"
echo "::group::root listing"
ls -la
echo "::endgroup::"
echo "::group::lockfile check"
if [ -f package-lock.json ]; then
echo "FOUND package-lock.json ($(wc -c < package-lock.json) bytes)"
head -5 package-lock.json
else
echo "MISSING package-lock.json"
fi
echo "::endgroup::"
- name: Install dependencies
run: npm ci
- name: Run tests
# --if-present is a no-op exit-0 when the script doesn't exist.
# Add `npm run test` (vitest etc.) to unlock this check.
run: npm run test --if-present
# ── CDK Diff (PR only) ────────────────────────────────────────────────────
cdk-diff:
name: CDK Diff (${{ inputs.diff-environment }})
runs-on: ubuntu-latest
continue-on-error: ${{ !inputs.strict-checks }}
if: ${{ github.event_name == 'pull_request' && inputs.run-cdk-diff }}
permissions:
id-token: write # Required for OIDC
contents: read
pull-requests: write # Required to post PR comment
environment: ${{ inputs.diff-environment }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}
cache: 'npm'
- name: Configure AWS credentials (OIDC)
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::${{ inputs.aws-account-id }}:role/${{ inputs.app-name }}-${{ inputs.diff-environment }}-deployer
aws-region: ${{ inputs.aws-region }}
- name: Debug — runner state before npm ci
run: |
echo "::group::pwd + ref"
pwd
git rev-parse HEAD
echo "::endgroup::"
echo "::group::root listing"
ls -la
echo "::endgroup::"
echo "::group::infra listing"
ls -la infra || echo "(no infra dir)"
echo "::endgroup::"
echo "::group::lockfile check"
for f in package-lock.json infra/package-lock.json; do
if [ -f "$f" ]; then
echo "FOUND $f ($(wc -c < "$f") bytes)"
else
echo "MISSING $f"
fi
done
echo "::endgroup::"
- name: Install app dependencies
run: npm ci
- name: Install infra dependencies
working-directory: infra
run: npm ci
- name: CDK Diff
id: diff
working-directory: infra
# Capture output even if diff exits non-zero (it does when there are changes)
run: |
set +e
DIFF_OUTPUT=$(ENVIRONMENT=${{ inputs.diff-environment }} npm run app:diff 2>&1)
EXIT_CODE=$?
echo "diff_output<<EOF" >> "$GITHUB_OUTPUT"
echo "$DIFF_OUTPUT" >> "$GITHUB_OUTPUT"
echo "EOF" >> "$GITHUB_OUTPUT"
echo "has_changes=$( [ $EXIT_CODE -ne 0 ] && echo 'true' || echo 'false' )" >> "$GITHUB_OUTPUT"
- name: Post CDK diff comment
uses: actions/github-script@v7
# Pass data via env so script content is static — avoids JS parser
# errors when diff output contains `${...}` (CloudFormation tokens).
env:
DIFF_OUTPUT: ${{ steps.diff.outputs.diff_output }}
HAS_CHANGES: ${{ steps.diff.outputs.has_changes }}
DIFF_ENV: ${{ inputs.diff-environment }}
with:
script: |
const diff = process.env.DIFF_OUTPUT || '';
const hasChanges = process.env.HAS_CHANGES === 'true';
const env = process.env.DIFF_ENV;
const icon = hasChanges ? '⚠️' : '✅';
const body = [
`## ${icon} CDK Diff — \`${env}\``,
'',
'```',
diff.slice(0, 60000), // GitHub comment limit guard
'```',
'',
hasChanges
? '_Infrastructure changes detected. Review before merging._'
: '_No infrastructure changes._',
].join('\n');
// Update existing comment if present, otherwise create
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const existingComment = comments.find(c =>
c.user?.type === 'Bot' && c.body?.startsWith('## ') && c.body.includes('CDK Diff')
);
if (existingComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body,
});
}