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 }}