diff --git a/.github/workflows/preview.yaml b/.github/workflows/preview.yaml new file mode 100644 index 0000000..e3a5f78 --- /dev/null +++ b/.github/workflows/preview.yaml @@ -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 + # "-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='' + 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 diff --git a/wrangler.jsonc b/wrangler.jsonc index 0f57c13..82226d4 100644 --- a/wrangler.jsonc +++ b/wrangler.jsonc @@ -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, }