Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

relay-preview-cleanup.yml

201 lines8.5 KB

1name: Clean up relay preview
2​
3# Tears down stage `pr-<n>` when `relay:preview` is removed or the PR closes,
4# and retries teardown for closed labeled PRs on a schedule. A label stays on a
5# PR only while its teardown is outstanding, so the scheduled retry lists
6# exactly the failures.
7#
8# This runs under pull_request_target, so the workflow and the Alchemy program
9# come from the default branch. No PR code runs here, which is why it needs no
10# reviewer approval and why a PR cannot edit its own teardown to reach secrets.
11on:
12 pull_request_target:
13 types: [unlabeled, closed]
14 schedule:
15 - cron: "17 7 * * *"
16 workflow_dispatch:
17​
18permissions:
19 contents: read
20 issues: read
21 pull-requests: read
22​
23jobs:
24 cleanup_pr:
25 name: Tear down relay preview
26 if: >-
27 github.ref == 'refs/heads/main' &&
28 github.event_name == 'pull_request_target' &&
29 (
30 (github.event.action == 'unlabeled' && github.event.label.name == 'relay:preview') ||
31 (github.event.action == 'closed' && contains(github.event.pull_request.labels.*.name, 'relay:preview'))
32 )
33 # Job-level so that events which skip this job, such as a closed PR without
34 # the label, never take the group's pending slot. GitHub replaces a pending
35 # run in a group with a newer one, which would drop a queued teardown.
36 concurrency:
37 group: relay-preview-${{ github.event.pull_request.number }}
38 cancel-in-progress: false
39 permissions:
40 contents: read
41 pull-requests: write
42 runs-on: blacksmith-8vcpu-ubuntu-2404
43 timeout-minutes: 30
44 environment:
45 name: relay-preview-cleanup
46 env:
47 GH_TOKEN: ${{ github.token }}
48 STAGE: pr-${{ github.event.pull_request.number }}
49 CLOUDFLARE_ACCOUNT_ID: ${{ vars.CLOUDFLARE_ACCOUNT_ID }}
50 PLANETSCALE_ORGANIZATION: ${{ vars.PLANETSCALE_ORGANIZATION }}
51 AXIOM_ORG_ID: ${{ vars.AXIOM_ORG_ID }}
52 RELAY_API_ZONE_NAME: ${{ vars.RELAY_API_ZONE_NAME }}
53 RELAY_TUNNEL_ZONE_NAME: ${{ vars.RELAY_TUNNEL_ZONE_NAME }}
54 CLERK_PUBLISHABLE_KEY: ${{ vars.CLERK_PUBLISHABLE_KEY }}
55 CLERK_JWT_AUDIENCE: ${{ vars.CLERK_JWT_AUDIENCE }}
56 APNS_ENABLED: "false"
57 ALCHEMY_TELEMETRY_DISABLED: "1"
58 steps:
59 - name: Checkout default branch
60 uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
61 with:
62 ref: ${{ github.event.repository.default_branch }}
63 sparse-checkout: |
64 /*
65 !/.repos/
66 sparse-checkout-cone-mode: false
67​
68 - name: Setup Vite+
69 uses: voidzero-dev/setup-vp@250f29ce396baf5e8f24498e17c0dfdebabc26eb # v1.15.0
70 with:
71 node-version-file: package.json
72 cache: true
73 run-install: |
74 args:
75 - --filter=t3code-relay...
76​
77 # Destroy also runs the preview tunnel sweep, which is part of the stack.
78 - name: Destroy preview stage
79 run: vp run --filter t3code-relay destroy --stage "$STAGE" --yes --no-input
80 env:
81 CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
82 PLANETSCALE_API_TOKEN_ID: ${{ secrets.PLANETSCALE_API_TOKEN_ID }}
83 PLANETSCALE_API_TOKEN: ${{ secrets.PLANETSCALE_API_TOKEN }}
84 AXIOM_TOKEN: ${{ secrets.AXIOM_TOKEN }}
85 CLERK_SECRET_KEY: ${{ secrets.CLERK_SECRET_KEY }}
86​
87 # Only reached after a successful destroy. A failed one keeps the label,
88 # so the scheduled retry picks the PR up again.
89 - name: Clear the teardown marker
90 if: github.event.action == 'closed'
91 run: gh pr edit "${{ github.event.pull_request.number }}" --repo "$GITHUB_REPOSITORY" --remove-label relay:preview
92​
93 # An unlabeled event has already removed the label, so a failed teardown
94 # must put it back or no retry will ever see the stage. Labels added with
95 # GITHUB_TOKEN do not start new runs, so this does not redeploy.
96 - name: Restore the teardown marker after a failed destroy
97 if: failure() && github.event.action == 'unlabeled'
98 run: gh pr edit "${{ github.event.pull_request.number }}" --repo "$GITHUB_REPOSITORY" --add-label relay:preview
99​
100 list_closed_previews:
101 name: List closed relay previews
102 if: github.ref == 'refs/heads/main' && github.event_name != 'pull_request_target'
103 runs-on: blacksmith-8vcpu-ubuntu-2404
104 timeout-minutes: 10
105 outputs:
106 numbers: ${{ steps.list.outputs.numbers }}
107 steps:
108 # The issues endpoint is paginated by gh, so there is no listing cap. Pull
109 # requests appear there with a `pull_request` key.
110 - name: List closed PRs labeled relay:preview
111 id: list
112 env:
113 GH_TOKEN: ${{ github.token }}
114 run: |
115 numbers=$(gh api --paginate --method GET "repos/$GITHUB_REPOSITORY/issues" \
116 -f labels=relay:preview -f state=closed -f per_page=100 \
117 --jq '.[] | select(.pull_request) | .number') \
118 || { echo "Could not list closed relay:preview PRs" >&2; exit 1; }
119 json=$(printf '%s\n' "$numbers" | jq -Rsc 'split("\n") | map(select(length > 0) | tonumber)')
120 echo "Closed relay:preview PRs: $json"
121 echo "numbers=$json" >> "$GITHUB_OUTPUT"
122​
123 sweep_pr:
124 name: Retry teardown for PR ${{ matrix.number }}
125 needs: list_closed_previews
126 if: github.ref == 'refs/heads/main' && needs.list_closed_previews.outputs.numbers != '[]'
127 runs-on: blacksmith-8vcpu-ubuntu-2404
128 timeout-minutes: 30
129 strategy:
130 fail-fast: false
131 max-parallel: 4
132 matrix:
133 number: ${{ fromJSON(needs.list_closed_previews.outputs.numbers) }}
134 # Same group as the deploy and teardown for this PR, so a reopened PR's
135 # deploy cannot run concurrently with a teardown of its previous stage.
136 concurrency:
137 group: relay-preview-${{ matrix.number }}
138 cancel-in-progress: false
139 permissions:
140 contents: read
141 pull-requests: write
142 environment:
143 name: relay-preview-cleanup
144 env:
145 GH_TOKEN: ${{ github.token }}
146 STAGE: pr-${{ matrix.number }}
147 CLOUDFLARE_ACCOUNT_ID: ${{ vars.CLOUDFLARE_ACCOUNT_ID }}
148 PLANETSCALE_ORGANIZATION: ${{ vars.PLANETSCALE_ORGANIZATION }}
149 AXIOM_ORG_ID: ${{ vars.AXIOM_ORG_ID }}
150 RELAY_API_ZONE_NAME: ${{ vars.RELAY_API_ZONE_NAME }}
151 RELAY_TUNNEL_ZONE_NAME: ${{ vars.RELAY_TUNNEL_ZONE_NAME }}
152 CLERK_PUBLISHABLE_KEY: ${{ vars.CLERK_PUBLISHABLE_KEY }}
153 CLERK_JWT_AUDIENCE: ${{ vars.CLERK_JWT_AUDIENCE }}
154 APNS_ENABLED: "false"
155 ALCHEMY_TELEMETRY_DISABLED: "1"
156 steps:
157 # The lock above is taken before this runs, so the state read here is
158 # current. An open PR, including one reopened since the listing, is left
159 # alone. GitHub reports merged PRs as MERGED, so anything not OPEN counts.
160 - name: Confirm the PR is not open
161 id: eligible
162 run: |
163 state=$(gh pr view "${{ matrix.number }}" --repo "$GITHUB_REPOSITORY" \
164 --json state --jq 'if .state == "OPEN" then "no" else "yes" end') \
165 || { echo "Could not read PR ${{ matrix.number }}" >&2; exit 1; }
166 echo "eligible=$state" >> "$GITHUB_OUTPUT"
167 [ "$state" = yes ] || echo "PR ${{ matrix.number }} is open again; leaving $STAGE alone."
168​
169 - name: Checkout default branch
170 if: steps.eligible.outputs.eligible == 'yes'
171 uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
172 with:
173 ref: ${{ github.event.repository.default_branch }}
174 sparse-checkout: |
175 /*
176 !/.repos/
177 sparse-checkout-cone-mode: false
178​
179 - name: Setup Vite+
180 if: steps.eligible.outputs.eligible == 'yes'
181 uses: voidzero-dev/setup-vp@250f29ce396baf5e8f24498e17c0dfdebabc26eb # v1.15.0
182 with:
183 node-version-file: package.json
184 cache: true
185 run-install: |
186 args:
187 - --filter=t3code-relay...
188​
189 - name: Destroy preview stage
190 if: steps.eligible.outputs.eligible == 'yes'
191 run: vp run --filter t3code-relay destroy --stage "$STAGE" --yes --no-input
192 env:
193 CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
194 PLANETSCALE_API_TOKEN_ID: ${{ secrets.PLANETSCALE_API_TOKEN_ID }}
195 PLANETSCALE_API_TOKEN: ${{ secrets.PLANETSCALE_API_TOKEN }}
196 AXIOM_TOKEN: ${{ secrets.AXIOM_TOKEN }}
197 CLERK_SECRET_KEY: ${{ secrets.CLERK_SECRET_KEY }}
198​
199 - name: Clear the teardown marker
200 if: steps.eligible.outputs.eligible == 'yes'
201 run: gh pr edit "${{ matrix.number }}" --repo "$GITHUB_REPOSITORY" --remove-label relay:preview