Capture on Deploy with GitHub Actions

One step in a GitHub Actions workflow has WRL capture your pages after each deploy. The step sends a POST to the site's publish hook, a secret URL that needs no API key.

Where the step goes depends on who deploys the site:

  • Your workflow builds and deploys it, for example to GitHub Pages or your own server. Add the step after the deploy step.
  • A platform deploys it when you push. Run the step when the platform reports the finished deployment to GitHub.

Prerequisites


Store the hook URL as a secret

On GitHub, open the repository's Settings, then Secrets and variables, then Actions, and choose New repository secret. Name it WRL_HOOK_URL and paste the hook URL as its value. With the GitHub CLI:

gh secret set WRL_HOOK_URL

Keep the URL out of the workflow file. In the workflow log GitHub redacts the value of a secret.


After a push to main

name: Deploy
on:
  push:
    branches: [main]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      # Your build and deploy steps go here.
      - name: Ask WRL to check the site
        env:
          WRL_HOOK_URL: ${{ secrets.WRL_HOOK_URL }}
        run: curl --fail-with-body -sS -X POST "$WRL_HOOK_URL"

The call has no body, so WRL answers {"site":"example.com","mode":"check","status":"queued"} and runs a check within about a minute: it reads your sitemap and captures the pages your account has no capture of yet, then the pages whose <lastmod> is later than their latest capture. That covers new pages, and edited pages if your sitemap gives each page its own <lastmod>. If it doesn't, name the edited pages, as in Send the pages that changed.

--fail-with-body makes the step fail when WRL answers with an error, and prints the answer. A 404 means the URL in the secret was replaced or mistyped. A 429 means the hook was called more than 60 times in an hour. If a failed call should not turn the run red, add continue-on-error: true to the step.

A site gets at most one check in 5 minutes. Several pushes in a row are fine: the next check covers the calls in between.

If your site is deployed by a second job, give the job with this step needs: with that job's name, so it runs once the deploy is done.


After a deployment by a platform

When a platform builds the site, a push is not yet a deploy: the build takes minutes, and a call made at push time would have WRL read the old sitemap. A platform that reports its deployments to GitHub lets a workflow start when the deployment has succeeded. Vercel's GitHub integration does that.

name: Record the deployed site
on: deployment_status

jobs:
  record:
    if: github.event.deployment_status.state == 'success' && startsWith(github.event.deployment.environment, 'Production')
    runs-on: ubuntu-latest
    steps:
      - name: Ask WRL to check the site
        env:
          WRL_HOOK_URL: ${{ secrets.WRL_HOOK_URL }}
        run: curl --fail-with-body -sS -X POST "$WRL_HOOK_URL"

The if line leaves out failed deployments and previews. On the repositories we looked at, Vercel names its production environment Production, or Production followed by a dash and the project's name. The repository's Deployments page on GitHub shows the name yours has.

Vercel's own docs now recommend its repository_dispatch events for this. The job is the same with them, and the trigger and the condition change:

on:
  repository_dispatch:
    types: ['vercel.deployment.success']

jobs:
  record:
    if: github.event.client_payload.environment == 'production'

GitHub runs a repository_dispatch workflow only when its file is on the default branch. Vercel also sends vercel.deployment.promoted when a deployment is promoted to production, by hand or automatically.

A deployment that a workflow creates with the repository's own GITHUB_TOKEN, as the GitHub Pages deploy action does, starts no other workflow, so a deployment_status workflow never runs for it. Put the step into the deploying workflow there, as in After a push to main.

The docs of Netlify and Cloudflare Pages describe commit statuses and check runs on GitHub and don't mention deployment statuses, so try the workflow on your repository before you rely on it. Both platforms can also post to a URL when a deploy has succeeded. Enter the hook URL there and you need no workflow: see Netlify, Vercel and Cloudflare Pages.


Send the pages that changed

A check finds an edited page only when the sitemap's <lastmod> for it changes, and many generated sitemaps leave that date out or give every page the time of the build. Naming the pages in the call is exact. In a site built from files, git diff lists the files a push changed, and one sed line turns file names into page paths:

name: Deploy
on:
  push:
    branches: [main]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
        with:
          fetch-depth: 0
      # Your build and deploy steps go here.
      - name: Ask WRL to capture the pages that changed
        env:
          WRL_HOOK_URL: ${{ secrets.WRL_HOOK_URL }}
          BEFORE: ${{ github.event.before }}
        run: |
          if ! git cat-file -e "$BEFORE^{commit}" 2>/dev/null; then
            # First push of the branch or a force push: nothing to compare with.
            curl --fail-with-body -sS -X POST "$WRL_HOOK_URL"
            exit 0
          fi
          git diff --name-only --diff-filter=AMR "$BEFORE" "$GITHUB_SHA" -- 'content/*.md' \
            | sed -E -e 's#^content/#/#' -e 's#/index\.md$#/#' -e 's#\.md$#/#' > pages.txt
          [ -s pages.txt ] || exit 0
          split -l 100 pages.txt batch-
          for f in batch-*; do
            curl --fail-with-body -sS -X POST "$WRL_HOOK_URL" \
              -H "Content-Type: text/plain" --data-binary "@$f"
          done

What the step does:

  • fetch-depth: 0 makes the checkout fetch the history. By default it fetches one commit, and git diff then has nothing to compare with.
  • github.event.before is the commit the branch pointed to before the push, so the diff covers all commits of the push. On the first push of a branch and after a force push that commit isn't in the checkout. The step then asks for a check and stops.
  • --diff-filter=AMR keeps added, modified and renamed files. Take the filter away to send deleted pages too: WRL then captures the "not found" answer, which records that the page is gone.
  • The sed line is the mapping from files to pages. Here content/pricing.md becomes /pricing/ and content/blog/index.md becomes /blog/. Change it to the way your generator builds URLs. The hook reads the body as one page per line and takes paths as well as full URLs.
  • A hook call takes up to 100 pages, so split sends a longer list in several calls.

WRL answers each call with what it did:

{
  "site": "example.com",
  "mode": "urls",
  "accepted": 2,
  "captures": [
    { "url": "https://example.com/pricing/", "id": "cap_a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6" },
    { "url": "https://example.com/blog/", "id": "cap_b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6a7" }
  ],
  "skipped": [],
  "rejected": [],
  "leftOut": []
}

WRL waits 60 seconds before it captures the pages of a hook call, so the CDN has time to serve the new version. The site's hookDelaySeconds changes that wait (see Set up a site).

The step sees content files only. A change to a template or a stylesheet changes pages without touching their files, and those pages get no new capture. After such a change, capture the affected pages in a batch.

The step also works in a deployment_status workflow. Add the checkout step with fetch-depth: 2, which fetches the deployed commit and its parent. Drop the if block at the start of the step and compare HEAD^ HEAD in place of "$BEFORE" "$GITHUB_SHA". That covers a merged pull request or a single commit. When several commits were pushed at once, only the last one is compared.


Check that it works

Push a change to a page. About two minutes after the workflow has run, the page has a new capture:

curl "https://api.webresourceledger.com/v1/captures?host=example.com&limit=5" \
  -H "Authorization: Bearer YOUR_API_KEY"

The step's log has WRL's answer. accepted counts the pages WRL will capture, and skipped lists pages that were captured in the last 2 minutes or have a capture pending. If the sed line builds the wrong paths, WRL captures your site's "not found" page for each of them. In the dashboard those pages are under Needs attention, with the state "HTTP 404".