The best way to control Claude Code, Codex and more from any computer or phone

main
yml 198 lines 8.39 KB
Raw
1 name: Website
2
3 on:
4 # Manual override — deploys unconditionally on whatever branch it's run
5 # from. Toggle `prod` to force a production deploy without a release.
6 workflow_dispatch:
7 inputs:
8 prod:
9 description: "Deploy to production (alchemy.run) instead of the branch stage"
10 type: boolean
11 default: false
12 # Release commits update package manifests, so packages/** keeps production
13 # deploys automatic without redeploying the site for unrelated main commits.
14 push:
15 branches:
16 - main
17 paths:
18 - "submodules/distilled"
19 - "website/**"
20 - "packages/**"
21 - "scripts/generate-api-reference.ts"
22 - "patches/**"
23 - "package.json"
24 - "pnpm-lock.yaml"
25 - "pnpm-workspace.yaml"
26 - "tsconfig.json"
27 - ".github/workflows/website.yml"
28 pull_request:
29 # Label changes create and remove previews without needing another push.
30 types:
31 - opened
32 - reopened
33 - synchronize
34 - labeled
35 - unlabeled
36 - closed
37 paths:
38 - "submodules/distilled"
39 - "website/**"
40 - "packages/**"
41 - "scripts/generate-api-reference.ts"
42 - "patches/**"
43 - "package.json"
44 - "pnpm-lock.yaml"
45 - "pnpm-workspace.yaml"
46 - "tsconfig.json"
47 - ".github/workflows/website.yml"
48
49 concurrency:
50 group: deploy-website-${{ github.event_name == 'pull_request' &&
51 format('pr-{0}', github.event.number) || (inputs.prod ||
52 startsWith(github.event.head_commit.message, 'chore(release):')) &&
53 'prod' || (github.ref == 'refs/heads/main' && 'main' || github.ref_name)
54 }}
55 # Deploy and destroy mutate the same stack and must finish atomically. Each
56 # run checks the PR's current state before mutation, so queued label events
57 # converge to the latest desired state even when GitHub reorders them.
58 cancel-in-progress: false
59
60 env:
61 STAGE: ${{ github.event_name == 'pull_request' && format('pr-{0}',
62 github.event.number) || (inputs.prod ||
63 startsWith(github.event.head_commit.message, 'chore(release):')) && 'prod' ||
64 (github.ref == 'refs/heads/main' && 'main' || github.ref_name) }}
65
66 jobs:
67 website:
68 name: Website
69 # Pushes to main and dispatches always deploy. Release commits
70 # (`chore(release): x.y.z`) and dispatches with `prod` toggled deploy
71 # production; other main commits deploy to main.alchemy.run.
72 #
73 # Pull requests only run when internal (forks can't access the bot or
74 # Cloudflare secrets) and either carry the opt-in `deploy-website` label
75 # or just lost it. Label events for other labels are ignored. The job
76 # then deploys the preview while the PR is open and labeled, and
77 # destroys it otherwise (closed, or the label was removed).
78 if: >-
79 github.event_name != 'pull_request' ||
80 (github.event.pull_request.head.repo.full_name == github.repository &&
81 (contains(github.event.pull_request.labels.*.name, 'deploy-website') ||
82 github.event.action == 'unlabeled') &&
83 ((github.event.action != 'labeled' && github.event.action != 'unlabeled') ||
84 github.event.label.name == 'deploy-website'))
85 runs-on: blacksmith-4vcpu-ubuntu-2404
86 permissions:
87 contents: read
88 pull-requests: write
89 steps:
90 - name: Checkout repository
91 uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
92
93 - name: Checkout distilled
94 run: git submodule update --init --depth=1 --checkout -- submodules/distilled
95
96 # Mint a token for the alchemy-version-bot GitHub App so the PR
97 # preview comment (created via GitHub.Comment in alchemy.run.ts)
98 # posts under the bot's identity instead of `github-actions[bot]`.
99 # Same App as release.yml — its installation scopes already cover
100 # PR comments.
101 - name: Generate bot token
102 id: bot-token
103 uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
104 with:
105 app-id: ${{ secrets.ALCHEMY_VERSION_BOT_ID }}
106 private-key: ${{ secrets.ALCHEMY_VERSION_BOT_PRIVATE_KEY }}
107
108 # Decide from the PR's current state, not the triggering event, so
109 # queued label events converge to the latest desired state even when
110 # GitHub reorders them.
111 - name: Decide deploy or destroy
112 id: mode
113 env:
114 GH_TOKEN: ${{ github.token }}
115 run: |
116 if [ "$GITHUB_EVENT_NAME" != pull_request ]; then
117 echo "mode=deploy" >> "$GITHUB_OUTPUT"
118 exit 0
119 fi
120 active="$(gh api "repos/$GITHUB_REPOSITORY/pulls/${{ github.event.number }}" \
121 --jq '[.state == "open", any(.labels[]; .name == "deploy-website")] | all')"
122 echo "mode=$([ "$active" = true ] && echo deploy || echo destroy)" >> "$GITHUB_OUTPUT"
123
124 - name: Restore Astro build cache
125 if: steps.mode.outputs.mode == 'deploy'
126 uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
127 with:
128 path: website/node_modules/.astro
129 key: website-astro-full-${{ runner.os }}-${{ github.event.pull_request.number || 'main' }}-${{ github.sha }}-${{ github.workflow }}-${{ github.run_attempt }}
130 restore-keys: |
131 website-astro-full-${{ runner.os }}-${{ github.event.pull_request.number || 'main' }}-${{ github.sha }}-
132 website-astro-full-${{ runner.os }}-${{ github.event.pull_request.number || 'main' }}-
133 website-astro-full-${{ runner.os }}-main-
134
135 - name: Setup pnpm, Node.js, Bun and install dependencies
136 uses: pnpm/setup@703c52620218391530e48b9e8870d5c0082e1b9b # v2.1.0
137 with:
138 cache: true
139 install: true
140 require-lockfile: true
141
142 # Docs on main.alchemy.run and PR previews install this commit's preview
143 # packages; production keeps npm (see website/plugins/preview-install.ts).
144 - name: Set preview package tag
145 if: steps.mode.outputs.mode == 'deploy' && env.STAGE != 'prod'
146 env:
147 PR: ${{ github.event.number }}
148 SHA: ${{ github.event.pull_request.head.sha || github.sha }}
149 run: |
150 if [ -n "$PR" ]; then tag="pr:$PR:${SHA:0:7}"; else tag="$SHA"; fi
151 echo "PREVIEW_PACKAGE_TAG=$tag" >> "$GITHUB_ENV"
152
153 # Main/prod use the production account; PR stages use the test account.
154 - name: Deploy
155 if: steps.mode.outputs.mode == 'deploy'
156 working-directory: website
157 run: bun alchemy deploy --stage "$STAGE" --yes
158 env:
159 PULL_REQUEST: ${{ github.event.number }}
160 CLOUDFLARE_API_TOKEN: ${{ (env.STAGE == 'main' || env.STAGE == 'prod') && secrets.PROD_CLOUDFLARE_API_TOKEN || secrets.TEST_CLOUDFLARE_API_TOKEN }}
161 CLOUDFLARE_ACCOUNT_ID: ${{ (env.STAGE == 'main' || env.STAGE == 'prod') && secrets.PROD_CLOUDFLARE_ACCOUNT_ID || secrets.TEST_CLOUDFLARE_ACCOUNT_ID }}
162 BUILD_SHA: ${{ github.event.pull_request.head.sha || github.sha }}
163 GITHUB_TOKEN: ${{ steps.bot-token.outputs.token }}
164
165 # PR builds upload zero-traffic versions of this Worker. Refresh the
166 # parent from merged main using the same test account as PR previews.
167 - name: Deploy preview parent
168 if: github.event_name == 'push' && github.ref == 'refs/heads/main'
169 working-directory: website
170 run: bun alchemy deploy --stage preview-base --yes
171 env:
172 CLOUDFLARE_API_TOKEN: ${{ secrets.TEST_CLOUDFLARE_API_TOKEN }}
173 CLOUDFLARE_ACCOUNT_ID: ${{ secrets.TEST_CLOUDFLARE_ACCOUNT_ID }}
174 BUILD_SHA: ${{ github.sha }}
175
176 # Allowlist, not denylist: destroy must only ever remove pr-N stages,
177 # so refuse anything else outright (prod/main/preview-base) even if a
178 # future STAGE refactor changes what a pull_request event resolves to.
179 - name: Safety check
180 if: steps.mode.outputs.mode == 'destroy'
181 run: |-
182 case "${{ env.STAGE }}" in
183 pr-[0-9]*) ;;
184 *)
185 echo "ERROR: destroy only removes pr-* stages, got '${{ env.STAGE }}'"
186 exit 1
187 ;;
188 esac
189
190 - name: Destroy preview
191 if: steps.mode.outputs.mode == 'destroy'
192 working-directory: website
193 run: bun alchemy destroy --stage "$STAGE" --yes
194 env:
195 PULL_REQUEST: ${{ github.event.number }}
196 CLOUDFLARE_API_TOKEN: ${{ secrets.TEST_CLOUDFLARE_API_TOKEN }}
197 CLOUDFLARE_ACCOUNT_ID: ${{ secrets.TEST_CLOUDFLARE_ACCOUNT_ID }}
198 GITHUB_TOKEN: ${{ steps.bot-token.outputs.token }}