Repository navigation
198 lines (184 loc) · 8.39 KB
/
Copy pathwebsite.yml
File metadata and controls
198 lines (184 loc) · 8.39 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
name: Website
on:
# Manual override — deploys unconditionally on whatever branch it's run
# from. Toggle `prod` to force a production deploy without a release.
workflow_dispatch:
inputs:
prod:
description: "Deploy to production (alchemy.run) instead of the branch stage"
type: boolean
default: false
# Release commits update package manifests, so packages/** keeps production
# deploys automatic without redeploying the site for unrelated main commits.
push:
branches:
- main
paths:
- "submodules/distilled"
- "website/**"
- "packages/**"
- "scripts/generate-api-reference.ts"
- "patches/**"
- "package.json"
- "pnpm-lock.yaml"
- "pnpm-workspace.yaml"
- "tsconfig.json"
- ".github/workflows/website.yml"
pull_request:
# Label changes create and remove previews without needing another push.
types:
- opened
- reopened
- synchronize
- labeled
- unlabeled
- closed
paths:
- "submodules/distilled"
- "website/**"
- "packages/**"
- "scripts/generate-api-reference.ts"
- "patches/**"
- "package.json"
- "pnpm-lock.yaml"
- "pnpm-workspace.yaml"
- "tsconfig.json"
- ".github/workflows/website.yml"
concurrency:
group: deploy-website-${{ github.event_name == 'pull_request' &&
format('pr-{0}', github.event.number) || (inputs.prod ||
startsWith(github.event.head_commit.message, 'chore(release):')) &&
'prod' || (github.ref == 'refs/heads/main' && 'main' || github.ref_name)
}}
# Deploy and destroy mutate the same stack and must finish atomically. Each
# run checks the PR's current state before mutation, so queued label events
# converge to the latest desired state even when GitHub reorders them.
cancel-in-progress: false
env:
STAGE: ${{ github.event_name == 'pull_request' && format('pr-{0}',
github.event.number) || (inputs.prod ||
startsWith(github.event.head_commit.message, 'chore(release):')) && 'prod' ||
(github.ref == 'refs/heads/main' && 'main' || github.ref_name) }}
jobs:
website:
name: Website
# Pushes to main and dispatches always deploy. Release commits
# (`chore(release): x.y.z`) and dispatches with `prod` toggled deploy
# production; other main commits deploy to main.alchemy.run.
#
# Pull requests only run when internal (forks can't access the bot or
# Cloudflare secrets) and either carry the opt-in `deploy-website` label
# or just lost it. Label events for other labels are ignored. The job
# then deploys the preview while the PR is open and labeled, and
# destroys it otherwise (closed, or the label was removed).
if: >-
github.event_name != 'pull_request' ||
(github.event.pull_request.head.repo.full_name == github.repository &&
(contains(github.event.pull_request.labels.*.name, 'deploy-website') ||
github.event.action == 'unlabeled') &&
((github.event.action != 'labeled' && github.event.action != 'unlabeled') ||
github.event.label.name == 'deploy-website'))
runs-on: blacksmith-4vcpu-ubuntu-2404
permissions:
contents: read
pull-requests: write
steps:
- name: Checkout repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Checkout distilled
run: git submodule update --init --depth=1 --checkout -- submodules/distilled
# Mint a token for the alchemy-version-bot GitHub App so the PR
# preview comment (created via GitHub.Comment in alchemy.run.ts)
# posts under the bot's identity instead of `github-actions[bot]`.
# Same App as release.yml — its installation scopes already cover
# PR comments.
- name: Generate bot token
id: bot-token
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
with:
app-id: ${{ secrets.ALCHEMY_VERSION_BOT_ID }}
private-key: ${{ secrets.ALCHEMY_VERSION_BOT_PRIVATE_KEY }}
# Decide from the PR's current state, not the triggering event, so
# queued label events converge to the latest desired state even when
# GitHub reorders them.
- name: Decide deploy or destroy
id: mode
env:
GH_TOKEN: ${{ github.token }}
run: |
if [ "$GITHUB_EVENT_NAME" != pull_request ]; then
echo "mode=deploy" >> "$GITHUB_OUTPUT"
exit 0
fi
active="$(gh api "repos/$GITHUB_REPOSITORY/pulls/${{ github.event.number }}" \
--jq '[.state == "open", any(.labels[]; .name == "deploy-website")] | all')"
echo "mode=$([ "$active" = true ] && echo deploy || echo destroy)" >> "$GITHUB_OUTPUT"
- name: Restore Astro build cache
if: steps.mode.outputs.mode == 'deploy'
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: website/node_modules/.astro
key: website-astro-full-${{ runner.os }}-${{ github.event.pull_request.number || 'main' }}-${{ github.sha }}-${{ github.workflow }}-${{ github.run_attempt }}
restore-keys: |
website-astro-full-${{ runner.os }}-${{ github.event.pull_request.number || 'main' }}-${{ github.sha }}-
website-astro-full-${{ runner.os }}-${{ github.event.pull_request.number || 'main' }}-
website-astro-full-${{ runner.os }}-main-
- name: Setup pnpm, Node.js, Bun and install dependencies
uses: pnpm/setup@703c52620218391530e48b9e8870d5c0082e1b9b # v2.1.0
with:
cache: true
install: true
require-lockfile: true
# Docs on main.alchemy.run and PR previews install this commit's preview
# packages; production keeps npm (see website/plugins/preview-install.ts).
- name: Set preview package tag
if: steps.mode.outputs.mode == 'deploy' && env.STAGE != 'prod'
env:
PR: ${{ github.event.number }}
SHA: ${{ github.event.pull_request.head.sha || github.sha }}
run: |
if [ -n "$PR" ]; then tag="pr:$PR:${SHA:0:7}"; else tag="$SHA"; fi
echo "PREVIEW_PACKAGE_TAG=$tag" >> "$GITHUB_ENV"
# Main/prod use the production account; PR stages use the test account.
- name: Deploy
if: steps.mode.outputs.mode == 'deploy'
working-directory: website
run: bun alchemy deploy --stage "$STAGE" --yes
env:
PULL_REQUEST: ${{ github.event.number }}
CLOUDFLARE_API_TOKEN: ${{ (env.STAGE == 'main' || env.STAGE == 'prod') && secrets.PROD_CLOUDFLARE_API_TOKEN || secrets.TEST_CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ (env.STAGE == 'main' || env.STAGE == 'prod') && secrets.PROD_CLOUDFLARE_ACCOUNT_ID || secrets.TEST_CLOUDFLARE_ACCOUNT_ID }}
BUILD_SHA: ${{ github.event.pull_request.head.sha || github.sha }}
GITHUB_TOKEN: ${{ steps.bot-token.outputs.token }}
# PR builds upload zero-traffic versions of this Worker. Refresh the
# parent from merged main using the same test account as PR previews.
- name: Deploy preview parent
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
working-directory: website
run: bun alchemy deploy --stage preview-base --yes
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.TEST_CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.TEST_CLOUDFLARE_ACCOUNT_ID }}
BUILD_SHA: ${{ github.sha }}
# Allowlist, not denylist: destroy must only ever remove pr-N stages,
# so refuse anything else outright (prod/main/preview-base) even if a
# future STAGE refactor changes what a pull_request event resolves to.
- name: Safety check
if: steps.mode.outputs.mode == 'destroy'
run: |-
case "${{ env.STAGE }}" in
pr-[0-9]*) ;;
*)
echo "ERROR: destroy only removes pr-* stages, got '${{ env.STAGE }}'"
exit 1
;;
esac
- name: Destroy preview
if: steps.mode.outputs.mode == 'destroy'
working-directory: website
run: bun alchemy destroy --stage "$STAGE" --yes
env:
PULL_REQUEST: ${{ github.event.number }}
CLOUDFLARE_API_TOKEN: ${{ secrets.TEST_CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.TEST_CLOUDFLARE_ACCOUNT_ID }}
GITHUB_TOKEN: ${{ steps.bot-token.outputs.token }}