Repository navigation
711 lines (698 loc) · 31.5 KB
/
Copy pathci-cd.yml
File metadata and controls
711 lines (698 loc) · 31.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
name: CI/CD
on:
pull_request:
paths-ignore:
- '.claude/**'
- '.gemini/**'
- '.serena/**'
- 'Dockerfile'
- 'docker-compose.yml'
- 'CLAUDE.md'
- 'GEMINI.md'
- '.editorconfig'
- '.env.example'
- '.gitignore'
- '.mcp.json'
- 'entrypoint.sh'
- 'README.md'
- 'Release.md'
- 'TRADEMARK'
- 'infra/**'
- 'ruby/**'
- 'tools/**'
- 'docs/**'
- 'bin/**'
push:
branches: [main, master, develop, hotfix/*]
paths-ignore:
- '.claude/**'
- '.gemini/**'
- '.serena/**'
- 'Dockerfile'
- 'docker-compose.yml'
- 'CLAUDE.md'
- 'GEMINI.md'
- '.editorconfig'
- '.env.example'
- '.gitignore'
- '.mcp.json'
- 'entrypoint.sh'
- 'README.md'
- 'Release.md'
- 'TRADEMARK'
- 'infra/**'
- 'ruby/**'
- 'tools/**'
- 'docs/**'
- 'bin/**'
workflow_dispatch:
concurrency:
group: "${{ github.workflow }} @ ${{ github.head_ref || github.ref }}"
cancel-in-progress: true
permissions:
contents: write # publish a GitHub release
issues: write # comment on released issues
pull-requests: write # comment on released pull requests
jobs:
lint:
runs-on: ubuntu-26.04
if: |
(!(
github.ref == 'refs/heads/develop' ||
github.ref == 'refs/heads/master' ||
github.ref == 'refs/heads/main'
))
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
cache: 'npm'
node-version-file: '.nvmrc'
- name: Install Dependencies
uses: ./.github/actions/install-dependencies
- name: Build dependency packages
run: npm run build:dev --workspace=packages/scratch-svg-renderer --workspace=packages/scratch-render --workspace=packages/scratch-vm
- name: Lint
run: npm run lint
unit-test-vm:
runs-on: ubuntu-26.04
if: |
(!(
github.ref == 'refs/heads/develop' ||
github.ref == 'refs/heads/master' ||
github.ref == 'refs/heads/main'
))
env:
NODE_OPTIONS: --max-old-space-size=4000
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
cache: 'npm'
node-version-file: '.nvmrc'
- name: Install Dependencies
uses: ./.github/actions/install-dependencies
- name: Build dependency packages
run: npm run build:dev --workspace=packages/scratch-svg-renderer --workspace=packages/scratch-render --workspace=packages/scratch-vm
- name: Run scratch-vm Unit Tests
run: npm run test:unit --workspace=packages/scratch-vm
unit-test-gui:
runs-on: ubuntu-26.04
if: |
(!(
github.ref == 'refs/heads/develop' ||
github.ref == 'refs/heads/master' ||
github.ref == 'refs/heads/main'
))
env:
NODE_OPTIONS: --max-old-space-size=4000
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
cache: 'npm'
node-version-file: '.nvmrc'
- name: Install Dependencies
uses: ./.github/actions/install-dependencies
- name: Build dependency packages
run: npm run build:dev --workspace=packages/scratch-svg-renderer --workspace=packages/scratch-render --workspace=packages/scratch-vm
- name: Run scratch-gui Unit Tests
run: npm run test:unit --workspace=packages/scratch-gui
integration-test-vm:
runs-on: ubuntu-26.04
if: |
(!(
github.ref == 'refs/heads/develop' ||
github.ref == 'refs/heads/master' ||
github.ref == 'refs/heads/main'
))
strategy:
matrix:
shard: [1, 2]
env:
NODE_OPTIONS: --max-old-space-size=4000
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
cache: 'npm'
node-version-file: '.nvmrc'
- name: Info
env:
HEAD_REF: ${{ github.head_ref }}
run: |
cat <<EOF
Node version: $(node --version)
NPM version: $(npm --version)
GitHub ref: ${{ github.ref }}
GitHub head ref: ${HEAD_REF}
EOF
- name: Install Dependencies
uses: ./.github/actions/install-dependencies
- name: Build packages
run: npm run build:dev
- name: Create test-results directories
run: mkdir -p packages/scratch-vm/test-results
- name: Run scratch-vm Integration Tests
env:
MESH_DATA_UPDATE_INTERVAL_MS: 100
MESH_EVENT_BATCH_INTERVAL_MS: 100
MESH_PERIODIC_DATA_SYNC_INTERVAL_MS: 1000
working-directory: packages/scratch-vm
run: |
# Collect all integration test files and sort for deterministic sharding
files=$(find test/integration -name '*.js' -o -name '*.test.js' | sort)
total=$(echo "$files" | wc -l)
half=$(( (total + 1) / 2 ))
if [ "${{ matrix.shard }}" = "1" ]; then
selected=$(echo "$files" | head -n "$half")
else
selected=$(echo "$files" | tail -n +"$((half + 1))")
fi
echo "Shard ${{ matrix.shard }}/$total files: running $(echo "$selected" | wc -l) files"
echo "$selected"
echo "$selected" | xargs npx tap
- name: Store Test Results
if: always() # Even if tests fail
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: test-output-integration-vm-shard-${{ matrix.shard }}
path: packages/scratch-vm/test-results/*
integration-test-gui:
runs-on: ubuntu-26.04
if: |
(!(
github.ref == 'refs/heads/develop' ||
github.ref == 'refs/heads/master' ||
github.ref == 'refs/heads/main'
))
strategy:
matrix:
shard: [1, 2]
env:
NODE_OPTIONS: --max-old-space-size=4000
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
cache: 'npm'
node-version-file: '.nvmrc'
- uses: browser-actions/setup-chrome@48ad923757ca74d66703209fe939badbdf80f2f4 # v2.2.0
id: setup-chrome
with:
chrome-version: stable
- name: Info
env:
HEAD_REF: ${{ github.head_ref }}
run: |
cat <<EOF
Node version: $(node --version)
NPM version: $(npm --version)
Chrome: ${{ steps.setup-chrome.outputs.chrome-path }} ($(${{ steps.setup-chrome.outputs.chrome-path }} --version))
GitHub ref: ${{ github.ref }}
GitHub head ref: ${HEAD_REF}
EOF
- name: Install Dependencies
uses: ./.github/actions/install-dependencies
- name: Build packages
# A dummy classroom endpoint so the classroom menus render; the integration tests stub
# window.fetch for the classroom paths and never reach it (#1775).
env:
CLASSROOM_API_ENDPOINT: https://classroom.invalid
run: npm run build:dev
- name: Create test-results directories
run: mkdir -p packages/scratch-gui/test-results
- name: Run scratch-gui Integration Tests
env:
JEST_JUNIT_OUTPUT_DIR: packages/scratch-gui/test-results
run: npm run test:integration --workspace=packages/scratch-gui -- --shard=${{ matrix.shard }}/2
- name: Store Test Results
if: always() # Even if tests fail
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: test-output-integration-gui-shard-${{ matrix.shard }}
path: packages/scratch-gui/test-results/*
build-and-deploy:
runs-on: ubuntu-26.04
# Job-level permissions replace the workflow-level ones, so restate them and add
# id-token for the S3 + CloudFront deploy (AssumeRole with GitHub OIDC, #2002).
permissions:
contents: write
issues: write
pull-requests: write
id-token: write
env:
NODE_OPTIONS: --max-old-space-size=4000
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
cache: 'npm'
node-version-file: '.nvmrc'
- name: Info
env:
HEAD_REF: ${{ github.head_ref }}
run: |
cat <<EOF
Node version: $(node --version)
NPM version: $(npm --version)
GitHub ref: ${{ github.ref }}
GitHub head ref: ${HEAD_REF}
EOF
- name: Install Dependencies
uses: ./.github/actions/install-dependencies
- name: Run Build
env:
NODE_ENV: production
GOOGLE_CLIENT_ID: ${{ secrets.GOOGLE_CLIENT_ID }}
GOOGLE_API_KEY: ${{ secrets.GOOGLE_API_KEY }}
MESH_GRAPHQL_ENDPOINT: ${{ secrets.MESH_GRAPHQL_ENDPOINT }}
MESH_API_KEY: ${{ secrets.MESH_API_KEY }}
MESH_AWS_REGION: ${{ secrets.MESH_AWS_REGION }}
MESH_DATA_UPDATE_INTERVAL_MS: ${{ vars.MESH_DATA_UPDATE_INTERVAL_MS }}
MESH_EVENT_BATCH_INTERVAL_MS: ${{ vars.MESH_EVENT_BATCH_INTERVAL_MS }}
MESH_PERIODIC_DATA_SYNC_INTERVAL_MS: ${{ vars.MESH_PERIODIC_DATA_SYNC_INTERVAL_MS }}
RUBYTEE_RELAY_ENDPOINT: ${{ secrets.RUBYTEE_RELAY_ENDPOINT }}
CLASSROOM_API_ENDPOINT: ${{ secrets.CLASSROOM_API_ENDPOINT }}
BUG_REPORT_API_ENDPOINT: ${{ secrets.BUG_REPORT_API_ENDPOINT }}
SCRATCH_API_PROXY_ENDPOINT: ${{ vars.SCRATCH_API_PROXY_ENDPOINT }}
MICROSOFT_CLIENT_ID: ${{ secrets.MICROSOFT_CLIENT_ID }}
MAX_USER_MESSAGE_LENGTH: ${{ vars.MAX_USER_MESSAGE_LENGTH }}
MIN_USER_MESSAGE_LENGTH: ${{ vars.MIN_USER_MESSAGE_LENGTH }}
GTM_ID: ${{ github.ref == 'refs/heads/develop' && secrets.GTM_ID || '' }}
GTM_ENV_AUTH: ${{ github.ref == 'refs/heads/develop' && secrets.GTM_ENV_AUTH || '' }}
COMMIT_SHA: ${{ github.sha }}
# Admin SPA (packages/admin — EPIC #1073). Register these in
# Settings > Secrets and variables > Actions > Variables.
ADMIN_API_ENDPOINT: ${{ vars.ADMIN_API_ENDPOINT }}
ADMIN_GOOGLE_CLIENT_ID: ${{ vars.ADMIN_GOOGLE_CLIENT_ID }}
run: npm run build
- name: Generate version.json
run: |
echo '{"commitId":"${{ github.sha }}"}' > packages/scratch-gui/build/version.json
echo '{"commitId":"${{ github.sha }}"}' > packages/scratch-gui/dist/version.json
- name: Store Build Output
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: build-output
path: packages/scratch-gui/build
- name: Store Dist Output
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: dist-output
path: packages/scratch-gui/dist
- name: Prepare deployment files for smalruby.app
if: github.ref == 'refs/heads/develop'
run: touch packages/scratch-gui/dist/.nojekyll
- name: Deploy to smalruby.app GitHub Pages
uses: peaceiris/actions-gh-pages@84c30a85c19949d7eee79c4ff27748b70285e453 # v4.1.0
if: github.ref == 'refs/heads/develop'
with:
deploy_key: ${{ secrets.SMALRUBY_APP_DEPLOY_KEY }}
publish_dir: ./packages/scratch-gui/dist
full_commit_message: "Build for ${{ github.sha }} ${{ github.event.head_commit.message }}"
cname: smalruby.app
external_repository: smalruby/smalruby.app
# peaceiris/actions-gh-pages binds a fixed SSH_AUTH_SOCK
# (/tmp/ssh-auth.sock) when deploying with deploy_key. The main deploy
# above leaves that ssh-agent running, so the second (admin) deploy
# cannot rebind the socket and fails with "Address already in use".
# Free it before the second deploy.
- name: Free ssh-agent socket before the admin deploy
if: github.ref == 'refs/heads/develop'
run: |
pkill ssh-agent || true
rm -f /tmp/ssh-auth.sock
# Admin SPA at smalruby.app/admin/ (EPIC #1073). Must run AFTER the
# main deploy: that step replaces the gh-pages content, so the admin
# subdirectory is re-published on every release (destination_dir +
# keep_files).
- name: Deploy admin SPA to smalruby.app/admin/
uses: peaceiris/actions-gh-pages@84c30a85c19949d7eee79c4ff27748b70285e453 # v4.1.0
if: github.ref == 'refs/heads/develop'
with:
deploy_key: ${{ secrets.SMALRUBY_APP_DEPLOY_KEY }}
publish_dir: ./packages/admin/build
destination_dir: admin
keep_files: true
full_commit_message: "Admin build for ${{ github.sha }}"
external_repository: smalruby/smalruby.app
# The same build also goes to the production S3 + CloudFront (EPIC #1931, #2002), next to
# GitHub Pages, so the two can be compared before smalruby.app is switched over
# (docs/hosting/README.md). GitHub Pages stays the live site until then.
- name: Check the S3 site settings
id: site
if: github.ref == 'refs/heads/develop'
env:
SITE_DEPLOY_ROLE_ARN: ${{ vars.SITE_DEPLOY_ROLE_ARN }}
SITE_BUCKET_NAME: ${{ vars.SITE_BUCKET_NAME }}
SITE_DISTRIBUTION_ID: ${{ vars.SITE_DISTRIBUTION_ID }}
run: |
missing=()
[ -n "${SITE_DEPLOY_ROLE_ARN}" ] || missing+=(SITE_DEPLOY_ROLE_ARN)
[ -n "${SITE_BUCKET_NAME}" ] || missing+=(SITE_BUCKET_NAME)
[ -n "${SITE_DISTRIBUTION_ID}" ] || missing+=(SITE_DISTRIBUTION_ID)
if [ ${#missing[@]} -gt 0 ]; then
echo "::warning::repo の Variables に ${missing[*]} が未設定のため、S3 + CloudFront への配置を飛ばします(docs/hosting/README.md)。"
echo "enabled=false" >> "$GITHUB_OUTPUT"
else
echo "enabled=true" >> "$GITHUB_OUTPUT"
fi
- uses: aws-actions/configure-aws-credentials@e1253824e5c10ff9df46874f81ed3ec929e19cfd # v6.3.0
if: steps.site.outputs.enabled == 'true'
with:
role-to-assume: ${{ vars.SITE_DEPLOY_ROLE_ARN }}
aws-region: ap-northeast-1
# CloudFront does not compress files over 10,000,000 bytes, and gui.<hash>.js is about
# 25 MB (#2198). Write gui.<hash>.js.br / .gz into dist here, after GitHub Pages got dist,
# so only S3 has them. A CloudFront Function picks them by Accept-Encoding (#2200); until
# then nothing requests them. Also warns about other files over 10,000,000 bytes.
- name: Pre-compress the gui JS (brotli / gzip)
if: steps.site.outputs.enabled == 'true'
run: |
set -euo pipefail
node --test scripts/precompress-assets.test.mjs
node scripts/precompress-assets.mjs packages/scratch-gui/dist
- name: Upload to the S3 site bucket
if: steps.site.outputs.enabled == 'true'
env:
BUCKET: ${{ vars.SITE_BUCKET_NAME }}
run: |
set -euo pipefail
# upload <src dir> <s3 prefix> <extra sync options...>
# Same cache rules as the preview (docs/infra/smalruby-web-hosting.md): file names with a
# hash are immutable, the rest (index.html, service-worker.js, version.json, .well-known,
# auth-redirect.*) are re-checked on every load. Types aws s3 may guess wrong (.wasm /
# .mjs / .hex) are uploaded with an explicit Content-Type.
# The pre-compressed gui JS (gui.<hash>.js.br / .gz) go up with cp, before index.html,
# with the Cache-Control of the original, Content-Type text/javascript and
# Content-Encoding br / gzip (aws s3 sync would guess the headers from the extension).
upload_precompressed() {
local src="$1" dest="$2" f name enc cache
for f in "${src}"/gui*.js.br "${src}"/gui*.js.gz; do
[ -e "${f}" ] || continue
name="$(basename "${f}")"
case "${name}" in
*.br) enc=br ;;
*) enc=gzip ;;
esac
if printf '%s' "${name}" | grep -qE '\.[0-9a-f]{8,}\.'; then
cache='public, max-age=31536000, immutable'
else
cache='max-age=0, s-maxage=60'
fi
aws s3 cp "${f}" "${dest}${name}" --only-show-errors \
--content-type 'text/javascript' --content-encoding "${enc}" --cache-control "${cache}"
done
}
upload() {
local src="$1" dest="$2"
shift 2
local hashed plain
hashed="$(mktemp -d)"
plain="$(mktemp -d)"
(cd "${src}" && find . -type f ! -name '.DS_Store' ! -name '.nojekyll' ! -name 'CNAME' \
! -path './gui*.js.br' ! -path './gui*.js.gz' -print0) |
while IFS= read -r -d '' f; do
if printf '%s' "${f}" | grep -qE '\.[0-9a-f]{8,}\.'; then
to="${hashed}"
else
to="${plain}"
fi
mkdir -p "${to}/$(dirname "${f}")"
cp -p "${src}/${f}" "${to}/${f}"
done
local typed=(--exclude '*.wasm' --exclude '*.mjs' --exclude '*.hex')
aws s3 sync "${hashed}" "${dest}" --only-show-errors "${typed[@]}" \
--cache-control 'public, max-age=31536000, immutable'
upload_precompressed "${src}" "${dest}"
aws s3 sync "${plain}" "${dest}" --only-show-errors "${typed[@]}" \
--cache-control 'max-age=0, s-maxage=60'
local ext type dir cache
for spec in 'wasm:application/wasm' 'mjs:text/javascript' 'hex:application/octet-stream'; do
ext="${spec%%:*}"
type="${spec#*:}"
for dir in "${hashed}" "${plain}"; do
if [ "${dir}" = "${hashed}" ]; then
cache='public, max-age=31536000, immutable'
else
cache='max-age=0, s-maxage=60'
fi
aws s3 sync "${dir}" "${dest}" --only-show-errors \
--exclude '*' --include "*.${ext}" \
--content-type "${type}" --cache-control "${cache}"
done
done
# Remove what the new build no longer has. Everything left was just uploaded, so
# --size-only keeps sync from re-uploading it (which would drop the Cache-Control).
# The .br / .gz are in src too, so they stay; the ones of an old gui are removed.
aws s3 sync "${src}" "${dest}" --delete --size-only --only-show-errors \
--exclude '.DS_Store' --exclude '.nojekyll' --exclude 'CNAME' "$@"
}
# The editor at the root. admin/ is its own deploy below, so --delete must not touch it.
upload packages/scratch-gui/dist "s3://${BUCKET}/" --exclude 'admin/*'
# Admin SPA at /admin/ (--delete stays inside admin/).
upload packages/admin/build "s3://${BUCKET}/admin/"
- name: Invalidate the S3 site
if: steps.site.outputs.enabled == 'true'
env:
DISTRIBUTION_ID: ${{ vars.SITE_DISTRIBUTION_ID }}
run: |
aws cloudfront create-invalidation --distribution-id "${DISTRIBUTION_ID}" \
--paths '/*' --query 'Invalidation.Id' --output text
# Branch previews on https://preview.smalruby.app/<slug>/ (#1344), built with the stg values
# so a preview never touches production data. These replace the GitHub Pages previews
# (smalruby.jp/smalruby3-editor/<branch>/, prod values), which stopped in #1345.
#
# Environment `stg` overrides the repo Secrets / Variables of the same name (the endpoints and
# the mesh API key), and holds PREVIEW_DEPLOY_ROLE_ARN / PREVIEW_BUCKET_NAME /
# PREVIEW_DISTRIBUTION_ID (Outputs of SmalrubyWebHostingStack-stg, docs/infra/smalruby-web-hosting.md).
# The rest (Google / Microsoft client IDs, message lengths, mesh intervals) are the same as
# prod and come from the repo values.
preview-deploy:
runs-on: ubuntu-26.04
# Fork PRs get neither the secrets nor an OIDC token.
if: |
(github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository) &&
(!(
github.ref == 'refs/heads/master' ||
github.ref == 'refs/heads/main' ||
startsWith(github.ref, 'refs/heads/hotfix/') ||
startsWith(github.head_ref, 'dependabot/') ||
startsWith(github.head_ref, 'renovate/')
))
environment:
name: stg
url: ${{ steps.slug.outputs.url }}
permissions:
contents: read
id-token: write # AssumeRole the preview deploy role (GitHub OIDC)
pull-requests: write # comment the preview URL
env:
NODE_OPTIONS: --max-old-space-size=4000
BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
cache: 'npm'
node-version-file: '.nvmrc'
- name: Check the preview settings
id: check
env:
ROLE_ARN: ${{ vars.PREVIEW_DEPLOY_ROLE_ARN }}
BUCKET: ${{ vars.PREVIEW_BUCKET_NAME }}
DISTRIBUTION_ID: ${{ vars.PREVIEW_DISTRIBUTION_ID }}
run: |
set -euo pipefail
missing=()
[ -n "${ROLE_ARN}" ] || missing+=(PREVIEW_DEPLOY_ROLE_ARN)
[ -n "${BUCKET}" ] || missing+=(PREVIEW_BUCKET_NAME)
[ -n "${DISTRIBUTION_ID}" ] || missing+=(PREVIEW_DISTRIBUTION_ID)
if [ "${#missing[@]}" -gt 0 ]; then
# Not a failure of the PR: the preview is skipped until the values are registered.
echo "::warning::Environment stg の Variables に ${missing[*]} が未設定のため、preview.smalruby.app へのプレビューを出しません(docs/infra/smalruby-web-hosting.md の「CI のプレビュー」)。"
echo "enabled=false" >> "$GITHUB_OUTPUT"
else
echo "enabled=true" >> "$GITHUB_OUTPUT"
fi
# Safety net: the build below reads the endpoints from Environment `stg`, which override
# the repo values of the same name. If one is missing there, the repo value (prod) is used
# silently and the preview would read and write production data. Every endpoint host must
# start with `stg.`; otherwise fail before building. Values are secrets, so never print them.
- name: Check that the build points to stg
if: steps.check.outputs.enabled == 'true'
env:
MESH_GRAPHQL_ENDPOINT: ${{ secrets.MESH_GRAPHQL_ENDPOINT }}
RUBYTEE_RELAY_ENDPOINT: ${{ secrets.RUBYTEE_RELAY_ENDPOINT }}
CLASSROOM_API_ENDPOINT: ${{ secrets.CLASSROOM_API_ENDPOINT }}
BUG_REPORT_API_ENDPOINT: ${{ secrets.BUG_REPORT_API_ENDPOINT }}
SCRATCH_API_PROXY_ENDPOINT: ${{ vars.SCRATCH_API_PROXY_ENDPOINT }}
run: |
set -euo pipefail
bad=()
for name in MESH_GRAPHQL_ENDPOINT RUBYTEE_RELAY_ENDPOINT CLASSROOM_API_ENDPOINT \
BUG_REPORT_API_ENDPOINT SCRATCH_API_PROXY_ENDPOINT; do
value="${!name}"
host="${value#*://}"
host="${host%%/*}"
case "${host}" in
stg.*) ;;
*) bad+=("${name}") ;;
esac
done
if [ "${#bad[@]}" -gt 0 ]; then
echo "::error::${bad[*]} が stg(ホストが stg. で始まる)を向いていません。Environment stg に登録されているか確かめてください。本番のデータに触れないよう、プレビューを出しません(docs/infra/smalruby-web-hosting.md の「CI のプレビュー」)。"
exit 1
fi
echo "All endpoints point to stg."
- name: Compute the preview slug
id: slug
if: steps.check.outputs.enabled == 'true'
run: |
set -euo pipefail
node --test scripts/preview-slug.test.mjs
slug="$(node scripts/preview-slug.mjs "${BRANCH_NAME}")"
echo "slug=${slug}" >> "$GITHUB_OUTPUT"
echo "url=https://preview.smalruby.app/${slug}/" >> "$GITHUB_OUTPUT"
echo "Preview slug: ${slug}"
- name: Install Dependencies
if: steps.check.outputs.enabled == 'true'
uses: ./.github/actions/install-dependencies
- name: Build with the stg values
if: steps.check.outputs.enabled == 'true'
env:
NODE_ENV: production
GOOGLE_CLIENT_ID: ${{ secrets.GOOGLE_CLIENT_ID }}
GOOGLE_API_KEY: ${{ secrets.GOOGLE_API_KEY }}
MESH_GRAPHQL_ENDPOINT: ${{ secrets.MESH_GRAPHQL_ENDPOINT }}
MESH_API_KEY: ${{ secrets.MESH_API_KEY }}
MESH_AWS_REGION: ${{ secrets.MESH_AWS_REGION }}
MESH_DATA_UPDATE_INTERVAL_MS: ${{ vars.MESH_DATA_UPDATE_INTERVAL_MS }}
MESH_EVENT_BATCH_INTERVAL_MS: ${{ vars.MESH_EVENT_BATCH_INTERVAL_MS }}
MESH_PERIODIC_DATA_SYNC_INTERVAL_MS: ${{ vars.MESH_PERIODIC_DATA_SYNC_INTERVAL_MS }}
RUBYTEE_RELAY_ENDPOINT: ${{ secrets.RUBYTEE_RELAY_ENDPOINT }}
CLASSROOM_API_ENDPOINT: ${{ secrets.CLASSROOM_API_ENDPOINT }}
BUG_REPORT_API_ENDPOINT: ${{ secrets.BUG_REPORT_API_ENDPOINT }}
SCRATCH_API_PROXY_ENDPOINT: ${{ vars.SCRATCH_API_PROXY_ENDPOINT }}
MICROSOFT_CLIENT_ID: ${{ secrets.MICROSOFT_CLIENT_ID }}
MAX_USER_MESSAGE_LENGTH: ${{ vars.MAX_USER_MESSAGE_LENGTH }}
MIN_USER_MESSAGE_LENGTH: ${{ vars.MIN_USER_MESSAGE_LENGTH }}
GTM_ID: ''
GTM_ENV_AUTH: ''
COMMIT_SHA: ${{ github.sha }}
run: npm run build
# The build uses relative paths, and postbuild.mjs makes the fetch-worker paths absolute
# for the subdirectory.
- name: Fix the worker paths for /<slug>/
if: steps.check.outputs.enabled == 'true'
env:
PUBLIC_PATH: /${{ steps.slug.outputs.slug }}/
run: |
cd packages/scratch-gui && npm exec node ./scripts/postbuild.mjs
echo '{"commitId":"${{ github.sha }}"}' > build/version.json
- uses: aws-actions/configure-aws-credentials@e1253824e5c10ff9df46874f81ed3ec929e19cfd # v6.3.0
if: steps.check.outputs.enabled == 'true'
with:
role-to-assume: ${{ vars.PREVIEW_DEPLOY_ROLE_ARN }}
aws-region: ap-northeast-1
# Same as production (#2198, #2199): build/gui.js.br / .gz for the CloudFront Function
# (#2200). After postbuild.mjs, which edits gui.js.
- name: Pre-compress the gui JS (brotli / gzip)
if: steps.check.outputs.enabled == 'true'
run: |
set -euo pipefail
node --test scripts/precompress-assets.test.mjs
node scripts/precompress-assets.mjs packages/scratch-gui/build
- name: Upload to s3://<bucket>/<slug>/
if: steps.check.outputs.enabled == 'true'
env:
BUCKET: ${{ vars.PREVIEW_BUCKET_NAME }}
SLUG: ${{ steps.slug.outputs.slug }}
run: |
set -euo pipefail
build=packages/scratch-gui/build
dest="s3://${BUCKET}/${SLUG}/"
# Split the build by cache lifetime (docs/infra/smalruby-web-hosting.md): file names with
# a hash (same rule as Workbox's dontCacheBustURLsMatching) are immutable, the rest
# (index.html, gui.js, sw.js, ...) are re-checked on every load.
hashed="$(mktemp -d)"
plain="$(mktemp -d)"
(cd "${build}" && find . -type f ! -name '.DS_Store' \
! -path './gui*.js.br' ! -path './gui*.js.gz' -print0) |
while IFS= read -r -d '' f; do
if printf '%s' "${f}" | grep -qE '\.[0-9a-f]{8,}\.'; then
to="${hashed}"
else
to="${plain}"
fi
mkdir -p "${to}/$(dirname "${f}")"
cp -p "${build}/${f}" "${to}/${f}"
done
aws s3 sync "${hashed}" "${dest}" --only-show-errors \
--cache-control 'public, max-age=31536000, immutable'
# The pre-compressed gui.js.br / .gz, before index.html (same as production: explicit
# Content-Type / Content-Encoding, the Cache-Control of gui.js).
for f in "${build}"/gui*.js.br "${build}"/gui*.js.gz; do
[ -e "${f}" ] || continue
name="$(basename "${f}")"
case "${name}" in
*.br) enc=br ;;
*) enc=gzip ;;
esac
if printf '%s' "${name}" | grep -qE '\.[0-9a-f]{8,}\.'; then
cache='public, max-age=31536000, immutable'
else
cache='max-age=0, s-maxage=60'
fi
aws s3 cp "${f}" "${dest}${name}" --only-show-errors \
--content-type 'text/javascript' --content-encoding "${enc}" --cache-control "${cache}"
done
aws s3 sync "${plain}" "${dest}" --only-show-errors \
--cache-control 'max-age=0, s-maxage=60'
# Remove what the new build no longer has. Everything left was just uploaded, so
# --size-only keeps sync from re-uploading it (which would drop the Cache-Control).
# The .br / .gz are in build too, so they stay.
aws s3 sync "${build}" "${dest}" --delete --size-only --only-show-errors \
--exclude '.DS_Store'
# Microsoft login returns to window.location.origin + '/auth-redirect.html', i.e. the
# bucket root, which every preview shares. Only develop writes it, so a branch cannot
# break the login of the other previews.
- name: Upload the shared /auth-redirect.html (develop only)
if: steps.check.outputs.enabled == 'true' && github.ref == 'refs/heads/develop'
env:
BUCKET: ${{ vars.PREVIEW_BUCKET_NAME }}
run: |
set -euo pipefail
for f in auth-redirect.html auth-redirect.js; do
aws s3 cp "packages/scratch-gui/build/${f}" "s3://${BUCKET}/${f}" --only-show-errors \
--cache-control 'max-age=0, s-maxage=60'
done
- name: Invalidate /<slug>/*
if: steps.check.outputs.enabled == 'true'
env:
DISTRIBUTION_ID: ${{ vars.PREVIEW_DISTRIBUTION_ID }}
SLUG: ${{ steps.slug.outputs.slug }}
IS_DEVELOP: ${{ github.ref == 'refs/heads/develop' }}
run: |
set -euo pipefail
paths=("/${SLUG}/*" "/${SLUG}")
if [ "${IS_DEVELOP}" = "true" ]; then
paths+=("/auth-redirect.html" "/auth-redirect.js")
fi
aws cloudfront create-invalidation --distribution-id "${DISTRIBUTION_ID}" \
--paths "${paths[@]}" --query 'Invalidation.Id' --output text
- name: Comment preview URL on PR (once only)
if: steps.check.outputs.enabled == 'true' && github.event_name == 'pull_request'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PREVIEW_URL: ${{ steps.slug.outputs.url }}
PR_NUMBER: ${{ github.event.pull_request.number }}
REPO: ${{ github.repository }}
run: |
set -euo pipefail
ALREADY_COMMENTED=$(gh pr view "$PR_NUMBER" --repo "$REPO" --json comments \
--jq "[.comments[].body | contains(\"$PREVIEW_URL\")] | any")
if [ "$ALREADY_COMMENTED" != "true" ]; then
gh pr comment "$PR_NUMBER" --repo "$REPO" --body "🚀 Preview deployed (stg API): $PREVIEW_URL"
fi