From f375ba8629059caba30bc0ccd9eaa3ef5945026e Mon Sep 17 00:00:00 2001 From: Sebastian Danielsson Date: Sat, 26 Sep 2026 12:08:17 +0200 Subject: [PATCH] ci: deploy to Vercel from one workflow with previews and a concurrency group Replaces sharevb-vercel-preview.yml and sharevb-vercel-prod.yml with deploy.yaml, which builds here and deploys the prebuilt output: - One concurrency group per ref with cancel-in-progress, so a burst of pushes costs one deployment instead of one per push. - A GitHub environment per run (Production or Preview), which restores the Environments sidebar and the PR's "View deployment" button. - Pull requests build their own head commit, named after the PR branch so Vercel shows the branch rather than "HEAD"; runs from forks are skipped, since they get no secrets. - Fails early on missing secrets, refuses to deploy a build without index.html, and pins the Vercel CLI. The production hostname for sitemap.xml and robots.txt comes from the VERCEL_PRODUCTION_URL repository variable instead of being hardcoded, so the same file serves any deployment. Secrets can live on the repository or on the Production/Preview environments. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_017s2w39tKNdBdD8kyfThuLy --- .github/workflows/deploy.yaml | 240 +++++++++++++++++++ .github/workflows/sharevb-vercel-preview.yml | 29 --- .github/workflows/sharevb-vercel-prod.yml | 29 --- 3 files changed, 240 insertions(+), 58 deletions(-) create mode 100644 .github/workflows/deploy.yaml delete mode 100644 .github/workflows/sharevb-vercel-preview.yml delete mode 100644 .github/workflows/sharevb-vercel-prod.yml diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml new file mode 100644 index 00000000..639821e1 --- /dev/null +++ b/.github/workflows/deploy.yaml @@ -0,0 +1,240 @@ +--- +name: Deploy + +# Builds here and deploys the result to Vercel. The Git integration is off for this project +# (`git.deploymentEnabled: false` in vercel.json), so this workflow is what deploys it. +# +# chore/all-my-stuffs goes to production. Every pull request gets a preview, linked from the +# PR's "View deployment" button through the GitHub environment set on the job. +# +# Setup: VERCEL_TOKEN, VERCEL_ORG_ID and VERCEL_PROJECT_ID as secrets, on the repository or on +# the Production and Preview environments. Optionally the VERCEL_PRODUCTION_URL repository +# variable (e.g. https://it-tools.example.com/): production builds generate sitemap.xml and +# robots.txt for it, and without it they are left out. +# +# Replaces sharevb-vercel-preview.yml and sharevb-vercel-prod.yml, which deployed the same two +# targets without a concurrency group or a GitHub environment. One deployment per burst of +# pushes keeps Vercel's daily deployment and upload caps, and Deployment Storage, in check. +permissions: + contents: read + +on: + push: + branches: + - chore/all-my-stuffs + pull_request: + workflow_dispatch: + +concurrency: + # Per ref, cancelling in progress: pushing three times in a minute should cost one + # deployment, not three. Production is grouped per ref too, so a rapid merge sequence + # collapses the same way. + group: deploy-${{ github.ref }} + cancel-in-progress: true + +jobs: + deploy: + # Same-repository events only. GitHub withholds secrets from `pull_request` runs on forks, + # so a fork would fail the credentials check on every push — and more to the point, this + # job runs the checked-out branch's build scripts with VERCEL_TOKEN in the environment. + # That is fine for branches in this repository, whose authors already have the secrets, and + # is not something to offer an arbitrary fork. + if: >- + github.event_name != 'pull_request' || + github.event.pull_request.head.repo.full_name == github.repository + runs-on: ubuntu-26.04-arm + timeout-minutes: 15 + # Records a GitHub Deployment per run, which is what puts the Environments sidebar, the + # "deployed to Preview" PR timeline entry and its "View deployment" button back — the parts + # of the Vercel Git integration lost when building moved here. The names match the + # environments that integration created, so their history carries on. The condition has to + # be restated rather than read from the `target` step: the environment is fixed before any + # step runs. `url` is evaluated at the end, so it can use the deploy step's output. + environment: + name: >- + ${{ github.ref == 'refs/heads/chore/all-my-stuffs' && github.event_name != 'pull_request' + && 'Production' || 'Preview' }} + url: ${{ steps.deploy.outputs.url }} + permissions: + contents: read + steps: + - name: Fail early if the Vercel credentials are missing + env: + VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} + VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }} + VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }} + run: | + set -euo pipefail + missing="" + for name in VERCEL_TOKEN VERCEL_ORG_ID VERCEL_PROJECT_ID; do + [ -z "${!name}" ] && missing="$missing $name" + done + if [ -n "$missing" ]; then + # Either place works, so name both rather than send anyone to the wrong settings + # page. + echo "::error::Missing secrets:$missing — set them on the repository (Settings > Secrets and variables > Actions) or on the Production and Preview environments (Settings > Environments). See .github/workflows/deploy.yaml" + exit 1 + fi + + - name: Checkout repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + # On pull requests, the exact commit that triggered the run, not GitHub's detached + # merge commit: this builds the PR's own head, as Vercel's Git integration did. + # Pinned to the event's SHA rather than the branch name, so a push that lands while + # this run is queued, or a re-run of an old run, can't deploy a different commit + # under this run's deployment. Empty on push and workflow_dispatch, which keeps the + # default ref. + ref: ${{ github.event.pull_request.head.sha }} + + # The Vercel CLI reads the deployment's branch and commit message from git. A detached + # checkout shows up in Vercel as branch "HEAD", so name the commit after the PR branch. + - name: Name the checked-out commit after the PR branch + if: github.event_name == 'pull_request' + env: + # From the environment, not interpolated: branch names can contain shell + # metacharacters. + HEAD_REF: ${{ github.head_ref }} + run: git checkout -B "$HEAD_REF" + + - uses: pnpm/setup@fbda4c85fc2e1e08721cd8763afea8f48d60f024 # v3.0.0 + with: + cache: true + + - name: Install Vercel CLI + # Into a scratch directory outside the repository, not globally: `pnpm add --global` + # needs a global bin directory that `pnpm/setup` does not configure, and it failed + # immediately on the first run with the secrets in place. Installing here also keeps + # package.json and the lockfile untouched, so the tree the build sees is the tree that + # was committed. + # + # Pinned: an unpinned CLI changes how this workflow builds and deploys with no commit + # here to explain it. + run: | + set -euo pipefail + mkdir -p "$RUNNER_TEMP/vercel-cli" + cd "$RUNNER_TEMP/vercel-cli" + pnpm init >/dev/null + # Build approvals as config rather than --allow-build flags, matching how the + # repository declares its own. This directory is outside the repository so it + # inherits nothing; both packages that run install scripts are named here. esbuild + # is the one that actually needs it — a transitive dependency of the CLI whose + # postinstall links its native binary. + # + # minimumReleaseAge is deliberately not set: pnpm's built-in default is already + # 1440 minutes, so restating it would only create a second place to keep in sync. + printf 'allowBuilds:\n vercel: true\n esbuild: true\n' > pnpm-workspace.yaml + pnpm add vercel@60.1.3 + echo "$RUNNER_TEMP/vercel-cli/node_modules/.bin" >> "$GITHUB_PATH" + + # Environment is decided once and reused, so the pull, the build and the deploy cannot + # disagree about which one they are targeting. + - name: Resolve target environment + id: target + env: + # Read from the environment, not interpolated into the script. workflow_dispatch can + # run on any branch, and a branch name containing shell metacharacters would + # otherwise be pasted straight into this `if` as source. + REF: ${{ github.ref }} + EVENT: ${{ github.event_name }} + PRODUCTION_URL: ${{ vars.VERCEL_PRODUCTION_URL }} + run: | + set -euo pipefail + if [ "$REF" = "refs/heads/chore/all-my-stuffs" ] && [ "$EVENT" != "pull_request" ]; then + { + echo "environment=production" + echo "prod_flag=--prod" + echo "hostname=$PRODUCTION_URL" + } >> "$GITHUB_OUTPUT" + else + { + echo "environment=preview" + echo "prod_flag=" + # No sitemap or robots.txt for previews: they would advertise the preview URL. + echo "hostname=" + } >> "$GITHUB_OUTPUT" + fi + + - name: Pull Vercel project settings + env: + VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} + VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }} + VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }} + run: | + set -euo pipefail + # The link is written out rather than left to the CLI to infer from environment + # variables. Relying on VERCEL_ORG_ID/VERCEL_PROJECT_ID alone failed here with + # "Could not retrieve Project Settings", and which variables a given CLI version + # honours is not something to guess at from a workflow. This file is exactly what + # `vercel link` produces, so the CLI has nothing left to work out. + mkdir -p .vercel + # printf rather than a heredoc: a heredoc's terminator has to sit at column zero, + # which it cannot inside an indented YAML block, and %s keeps the ids out of the + # shell's word splitting. + printf '{"projectId":"%s","orgId":"%s"}\n' "$VERCEL_PROJECT_ID" "$VERCEL_ORG_ID" \ + > .vercel/project.json + # Shape only — the ids are secrets and are not printed. + python3 -c "import json;d=json.load(open('.vercel/project.json'));print('link written, keys:', sorted(d))" + vercel pull --yes \ + --environment="${{ steps.target.outputs.environment }}" \ + --token="$VERCEL_TOKEN" + + - name: Build + env: + VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} + VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }} + VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }} + # What sharevb-vercel-*.yml set, kept so the build is the same: + # VITE_VERCEL_ENV is the env label the app reports (src/config.ts), and + # VITE_VERCEL_DEPLOY turns off full PWA precaching (vite.config.ts). + VITE_VERCEL_ENV: ${{ steps.target.outputs.environment }} + VITE_VERCEL_DEPLOY: true + # HOSTNAME is what vite.config.ts generates sitemap.xml and robots.txt from, so it is + # set for production only, from the VERCEL_PRODUCTION_URL repository variable. + HOSTNAME: ${{ steps.target.outputs.hostname }} + run: | + set -euo pipefail + vercel build ${{ steps.target.outputs.prod_flag }} --token="$VERCEL_TOKEN" + # A build that produces nothing would otherwise deploy an empty site over a working + # one. Checked before anything is uploaded. + test -s .vercel/output/static/index.html || { + echo "::error::Build produced no .vercel/output/static/index.html — refusing to deploy" + exit 1 + } + + - name: Deploy + id: deploy + env: + VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} + VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }} + VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }} + run: | + set -euo pipefail + # --archive=tgz uploads the output as one archive instead of file by file. The Hobby + # plan caps CLI uploads at 5000 files a day across the whole account + # ("api-upload-free"), and per-file uploads hit that after a busy + # day of deploys. + url=$(vercel deploy --prebuilt --archive=tgz ${{ steps.target.outputs.prod_flag }} --token="$VERCEL_TOKEN") + # `vercel deploy` prints the URL on stdout and progress on stderr, so this is the URL + # and nothing else. Empty means the deploy did not produce one, which is a failure + # even if the CLI exited 0. + [ -n "$url" ] || { echo "::error::Deploy returned no URL"; exit 1; } + echo "url=$url" >> "$GITHUB_OUTPUT" + echo "Deployed to $url" + + - name: Summary + if: always() + env: + ENVIRONMENT: ${{ steps.target.outputs.environment }} + URL: ${{ steps.deploy.outputs.url }} + STATUS: ${{ job.status }} + run: | + { + echo "### Deploy" + echo "" + if [ "$STATUS" = "success" ]; then + echo "Deployed to **$ENVIRONMENT**: $URL" + else + echo "**Deploy failed.** See the failing step above." + fi + } >> "$GITHUB_STEP_SUMMARY" diff --git a/.github/workflows/sharevb-vercel-preview.yml b/.github/workflows/sharevb-vercel-preview.yml deleted file mode 100644 index 8e451e11..00000000 --- a/.github/workflows/sharevb-vercel-preview.yml +++ /dev/null @@ -1,29 +0,0 @@ -name: Vercel Preview Deployment -env: - VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }} - VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }} - VITE_VERCEL_ENV: preview - VITE_VERCEL_DEPLOY: true -on: - push: - branches-ignore: - - chore/all-my-stuffs - pull_request: -jobs: - Deploy-Preview: - runs-on: ubuntu-26.04-arm - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - # versions pinned in package.json (devEngines) on PATH first. - - name: Setup pnpm and install dependencies - uses: pnpm/setup@fbda4c85fc2e1e08721cd8763afea8f48d60f024 # v3.0.0 - with: - cache: true - - name: Install Vercel CLI - run: npm install --global vercel@60.1.3 - - name: Pull Vercel Environment Information - run: vercel pull --yes --environment=preview --token=${{ secrets.VERCEL_TOKEN }} - - name: Build Project Artifacts - run: vercel build --token=${{ secrets.VERCEL_TOKEN }} - - name: Deploy Project Artifacts to Vercel - run: vercel deploy --prebuilt --archive=tgz --token=${{ secrets.VERCEL_TOKEN }} \ No newline at end of file diff --git a/.github/workflows/sharevb-vercel-prod.yml b/.github/workflows/sharevb-vercel-prod.yml deleted file mode 100644 index 55da2311..00000000 --- a/.github/workflows/sharevb-vercel-prod.yml +++ /dev/null @@ -1,29 +0,0 @@ -name: Vercel Production Deployment -env: - VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }} - VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }} - VITE_VERCEL_ENV: production - VITE_VERCEL_DEPLOY: true - HOSTNAME: https://sharevb-it-tools.vercel.app/ -on: - push: - branches: - - chore/all-my-stuffs -jobs: - Deploy-Production: - runs-on: ubuntu-26.04-arm - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - # versions pinned in package.json (devEngines) on PATH first. - - name: Setup pnpm and install dependencies - uses: pnpm/setup@fbda4c85fc2e1e08721cd8763afea8f48d60f024 # v3.0.0 - with: - cache: true - - name: Install Vercel CLI - run: npm install --global vercel@60.1.3 - - name: Pull Vercel Environment Information - run: vercel pull --yes --environment=production --token=${{ secrets.VERCEL_TOKEN }} - - name: Build Project Artifacts - run: vercel build --prod --token=${{ secrets.VERCEL_TOKEN }} - - name: Deploy Project Artifacts to Vercel - run: vercel deploy --prebuilt --archive=tgz --prod --token=${{ secrets.VERCEL_TOKEN }}