|
| 1 | +# Deletes the docs previews that docs-pr-preview-deploy.yaml publishes. |
| 2 | +# |
| 3 | +# On merge or close it deletes every deployment of that PR (tagged with the |
| 4 | +# deployment meta pr=<number>), and with them their |
| 5 | +# pr-<number>[-<sha>].<DOCS_PREVIEW_DOMAIN> aliases. Once a week, and on |
| 6 | +# demand, it grooms the whole preview project: any deployment whose PR is |
| 7 | +# closed goes, which catches a close event this workflow missed. |
| 8 | +# |
| 9 | +# Only the Vercel and GitHub APIs are called; nothing is checked out and no |
| 10 | +# PR content runs. Fork PRs never get a preview, so their close is skipped. |
| 11 | +name: docs-pr-preview-cleanup |
| 12 | + |
| 13 | +on: |
| 14 | + # zizmor: ignore[dangerous-triggers] Runs from the base branch so a PR cannot change its own cleanup. Nothing from the PR is checked out or run. |
| 15 | + pull_request_target: |
| 16 | + types: |
| 17 | + - closed |
| 18 | + schedule: |
| 19 | + - cron: "23 7 * * 1" |
| 20 | + workflow_dispatch: |
| 21 | + |
| 22 | +concurrency: |
| 23 | + group: ${{ github.workflow }}-${{ github.event.pull_request.number || 'groom' }} |
| 24 | + cancel-in-progress: false |
| 25 | + |
| 26 | +permissions: {} |
| 27 | + |
| 28 | +jobs: |
| 29 | + cleanup: |
| 30 | + if: github.event_name != 'pull_request_target' || github.event.pull_request.head.repo.full_name == github.repository |
| 31 | + runs-on: ubuntu-latest |
| 32 | + timeout-minutes: 30 |
| 33 | + environment: docs-preview |
| 34 | + permissions: |
| 35 | + pull-requests: read |
| 36 | + steps: |
| 37 | + - name: Harden Runner |
| 38 | + uses: step-security/harden-runner@ccd8616d44fd3846e67624a50d5aad6d37bf2d25 # v2.22.1 |
| 39 | + with: |
| 40 | + egress-policy: audit |
| 41 | + |
| 42 | + - name: Delete closed pull requests' previews |
| 43 | + env: |
| 44 | + GH_TOKEN: ${{ github.token }} |
| 45 | + REPO: ${{ github.repository }} |
| 46 | + CLOSED_PR: ${{ github.event.pull_request.number }} |
| 47 | + VERCEL_TOKEN: ${{ secrets.VERCEL_PREVIEW_TOKEN }} |
| 48 | + VERCEL_ORG_ID: ${{ vars.VERCEL_PREVIEW_ORG_ID }} |
| 49 | + VERCEL_PROJECT_ID: ${{ vars.VERCEL_PREVIEW_PROJECT_ID }} |
| 50 | + DOCS_PREVIEW_DOMAIN: ${{ vars.DOCS_PREVIEW_DOMAIN }} |
| 51 | + run: | |
| 52 | + set -euo pipefail |
| 53 | +
|
| 54 | + domain=$(printf '%s' "$DOCS_PREVIEW_DOMAIN" | tr '[:upper:]' '[:lower:]') |
| 55 | + if [ -z "$domain" ] || [[ "$domain" =~ (^|\.)vercel\.app\.?$ ]] || ! [[ "$domain" =~ ^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)+$ ]]; then |
| 56 | + echo "::error::Set the repository variable DOCS_PREVIEW_DOMAIN to a Coder-owned domain (not vercel.app)." >&2 |
| 57 | + exit 1 |
| 58 | + fi |
| 59 | +
|
| 60 | + vercel_api() { |
| 61 | + curl --fail-with-body -sS --retry 3 \ |
| 62 | + -H "Authorization: Bearer ${VERCEL_TOKEN}" "$@" |
| 63 | + } |
| 64 | +
|
| 65 | + # Every deployment in the preview project as "<id>\t<pr>\t<sha>", |
| 66 | + # following the pagination cursor to the end. |
| 67 | + list_deployments() { |
| 68 | + local until="" page |
| 69 | + while :; do |
| 70 | + page=$(vercel_api "https://api.vercel.com/v6/deployments?teamId=${VERCEL_ORG_ID}&projectId=${VERCEL_PROJECT_ID}&limit=100${until:+&until=${until}}") |
| 71 | + printf '%s' "$page" | jq -r '.deployments[] | [.uid, (.meta.pr // ""), (.meta.sha // "")] | @tsv' |
| 72 | + until=$(printf '%s' "$page" | jq -r '.pagination.next // empty') |
| 73 | + [ -n "$until" ] || break |
| 74 | + done |
| 75 | + } |
| 76 | +
|
| 77 | + # A 404 means the alias or deployment is already gone. |
| 78 | + delete() { |
| 79 | + local status |
| 80 | + status=$(vercel_api -o /dev/null -w '%{http_code}' -X DELETE "$1" || true) |
| 81 | + case "$status" in |
| 82 | + 200 | 204 | 404) ;; |
| 83 | + *) |
| 84 | + echo "DELETE $1 returned ${status}" >&2 |
| 85 | + return 1 |
| 86 | + ;; |
| 87 | + esac |
| 88 | + } |
| 89 | +
|
| 90 | + deployments=$(list_deployments) |
| 91 | +
|
| 92 | + if [ -n "$CLOSED_PR" ]; then |
| 93 | + closed="$CLOSED_PR" |
| 94 | + else |
| 95 | + closed="" |
| 96 | + for pr in $(printf '%s\n' "$deployments" | cut -f2 | grep -E '^[0-9]+$' | sort -un); do |
| 97 | + state=$(gh api "repos/${REPO}/pulls/${pr}" --jq '.state' || echo unknown) |
| 98 | + if [ "$state" = "closed" ]; then |
| 99 | + closed="${closed} ${pr}" |
| 100 | + elif [ "$state" = "unknown" ]; then |
| 101 | + echo "::warning::Could not read #${pr}; keeping its previews until the next run." |
| 102 | + fi |
| 103 | + done |
| 104 | + fi |
| 105 | +
|
| 106 | + failed=0 |
| 107 | + for pr in $closed; do |
| 108 | + removed=0 |
| 109 | + delete "https://api.vercel.com/v2/aliases/pr-${pr}.${domain}?teamId=${VERCEL_ORG_ID}" || failed=1 |
| 110 | + while IFS=$'\t' read -r id dep_pr sha; do |
| 111 | + [ "$dep_pr" = "$pr" ] || continue |
| 112 | + if [[ "$sha" =~ ^[0-9a-f]{40}$ ]]; then |
| 113 | + delete "https://api.vercel.com/v2/aliases/pr-${pr}-${sha:0:9}.${domain}?teamId=${VERCEL_ORG_ID}" || failed=1 |
| 114 | + fi |
| 115 | + if delete "https://api.vercel.com/v13/deployments/${id}?teamId=${VERCEL_ORG_ID}"; then |
| 116 | + removed=$((removed + 1)) |
| 117 | + else |
| 118 | + failed=1 |
| 119 | + fi |
| 120 | + done <<< "$deployments" |
| 121 | + echo "#${pr}: deleted ${removed} preview deployment(s)." |
| 122 | + done |
| 123 | + exit "$failed" |
0 commit comments