Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions .github/workflows/preview.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
name: Preview

on:
pull_request:
branches:
- main
types: [opened, synchronize, reopened]

permissions:
contents: read
pull-requests: write

concurrency:
group: preview-${{ github.event.pull_request.number }}
cancel-in-progress: true

jobs:
preview:
# Fork PRs get no secrets, so a preview upload cannot work for them.
if: github.event.pull_request.head.repo.full_name == github.repository
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7

- uses: pnpm/action-setup@73e46992e05b19abd31f2f3c20e0e7370d762115 # v6

- uses: jdx/mise-action@924e7c586e33d7d4bae9c14e26c5d22e2beb6e78 # v5

- name: Install dependencies
run: pnpm install --frozen-lockfile

- name: Build
run: pnpm run build

# Aliases allow only [a-z0-9-], must start with a letter, and
# "<alias>-website" must fit in a 63-character DNS label.
- name: Compute alias
id: alias
env:
BRANCH: ${{ github.head_ref }}
run: |
alias=$(printf '%s' "$BRANCH" | tr 'A-Z' 'a-z' | sed -E 's/[^a-z0-9]+/-/g; s/^-+//; s/-+$//')
case "$alias" in
[a-z]*) ;;
*) alias="b-$alias" ;;
esac
alias=$(printf '%s' "${alias:0:54}" | sed -E 's/-+$//')
echo "alias=$alias" >> "$GITHUB_OUTPUT"

# Why npx instead of cloudflare/wrangler-action: see deploy.yaml.
- name: Upload version
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
WRANGLER_OUTPUT_FILE_PATH: ${{ runner.temp }}/wrangler-output.ndjson
ALIAS: ${{ steps.alias.outputs.alias }}
run: npx --yes wrangler@4.158.0 versions upload --preview-alias "$ALIAS"

- name: Read preview URL
id: url
env:
OUTPUT: ${{ runner.temp }}/wrangler-output.ndjson
run: |
url=$(jq -r 'select(.type == "version-upload") | .preview_alias_url // empty' "$OUTPUT" | tail -n 1)
if [ -z "$url" ]; then
echo "::error::No preview alias URL in wrangler output. Check that Preview URLs are enabled on the website Worker and the account has a workers.dev subdomain."
cat "$OUTPUT"
exit 1
fi
echo "url=$url" >> "$GITHUB_OUTPUT"

- name: Comment on PR
env:
GH_TOKEN: ${{ github.token }}
PR: ${{ github.event.pull_request.number }}
URL: ${{ steps.url.outputs.url }}
SHA: ${{ github.event.pull_request.head.sha }}
run: |
marker='<!-- preview-build -->'
body=$(printf '%s\n**Preview build is ready**\n\n- URL: %s\n- Commit: %s' "$marker" "$URL" "$SHA")
id=$(gh api "repos/$GITHUB_REPOSITORY/issues/$PR/comments" --paginate \
--jq "[.[] | select(.body | startswith(\"$marker\"))][0].id // empty")
if [ -n "$id" ]; then
gh api -X PATCH "repos/$GITHUB_REPOSITORY/issues/comments/$id" -f body="$body"
else
gh api "repos/$GITHUB_REPOSITORY/issues/$PR/comments" -f body="$body"
fi
4 changes: 3 additions & 1 deletion wrangler.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@
// pulsate-dev/infra manages, so cutover and rollback need no DNS change.
"routes": [{ "pattern": "pulsate.dev/*", "zone_name": "pulsate.dev" }],
"workers_dev": false,
"preview_urls": false,
// Version URLs stay on so preview.yaml can publish per-branch aliases; the
// production route is unaffected by them.
"preview_urls": true,
}
Loading