11name : Clean up relay preview
22
33# 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.
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.
57#
68# This runs under pull_request_target, so the workflow and the Alchemy program
79# come from the default branch. No PR code runs here, which is why it needs no
2224 cleanup_pr :
2325 name : Tear down relay preview
2426 if : >-
27+ github.ref == 'refs/heads/main' &&
2528 github.event_name == 'pull_request_target' &&
2629 (
2730 (github.event.action == 'unlabeled' && github.event.label.name == 'relay:preview') ||
@@ -33,11 +36,16 @@ jobs:
3336 concurrency :
3437 group : relay-preview-${{ github.event.pull_request.number }}
3538 cancel-in-progress : false
39+ permissions :
40+ contents : read
41+ issues : write
42+ pull-requests : write
3643 runs-on : blacksmith-8vcpu-ubuntu-2404
3744 timeout-minutes : 30
3845 environment :
3946 name : relay-preview-cleanup
4047 env :
48+ GH_TOKEN : ${{ github.token }}
4149 STAGE : pr-${{ github.event.pull_request.number }}
4250 CLOUDFLARE_ACCOUNT_ID : ${{ vars.CLOUDFLARE_ACCOUNT_ID }}
4351 PLANETSCALE_ORGANIZATION : ${{ vars.PLANETSCALE_ORGANIZATION }}
7785 AXIOM_TOKEN : ${{ secrets.AXIOM_TOKEN }}
7886 CLERK_SECRET_KEY : ${{ secrets.CLERK_SECRET_KEY }}
7987
88+ # Only reached after a successful destroy. A failed one keeps the label,
89+ # so the scheduled retry picks the PR up again.
90+ - name : Clear the teardown marker
91+ if : github.event.action == 'closed'
92+ run : gh pr edit "${{ github.event.pull_request.number }}" --repo "$GITHUB_REPOSITORY" --remove-label relay:preview
93+
8094 list_closed_previews :
8195 name : List closed relay previews
82- if : github.event_name != 'pull_request_target'
96+ if : github.ref == 'refs/heads/main' && github. event_name != 'pull_request_target'
8397 runs-on : blacksmith-8vcpu-ubuntu-2404
8498 timeout-minutes : 10
8599 outputs :
@@ -103,7 +117,7 @@ jobs:
103117 sweep_pr :
104118 name : Retry teardown for PR ${{ matrix.number }}
105119 needs : list_closed_previews
106- if : needs.list_closed_previews.outputs.numbers != '[]'
120+ if : github.ref == 'refs/heads/main' && needs.list_closed_previews.outputs.numbers != '[]'
107121 runs-on : blacksmith-8vcpu-ubuntu-2404
108122 timeout-minutes : 30
109123 strategy :
@@ -116,6 +130,10 @@ jobs:
116130 concurrency :
117131 group : relay-preview-${{ matrix.number }}
118132 cancel-in-progress : false
133+ permissions :
134+ contents : read
135+ issues : write
136+ pull-requests : write
119137 environment :
120138 name : relay-preview-cleanup
121139 env :
@@ -132,16 +150,16 @@ jobs:
132150 ALCHEMY_TELEMETRY_DISABLED : " 1"
133151 steps :
134152 # The lock above is taken before this runs, so the state read here is
135- # current. A reopened or relabeled PR is left alone.
136- - name : Confirm the PR is still closed and unlabeled
153+ # current. An open PR, including one reopened since the listing, is left
154+ # alone. GitHub reports merged PRs as MERGED, so anything not OPEN counts.
155+ - name : Confirm the PR is not open
137156 id : eligible
138157 run : |
139158 state=$(gh pr view "${{ matrix.number }}" --repo "$GITHUB_REPOSITORY" \
140- --json state,labels \
141- --jq 'if .state == "CLOSED" and (any(.labels[]; .name == "relay:preview") | not) then "yes" else "no" end') \
159+ --json state --jq 'if .state == "OPEN" then "no" else "yes" end') \
142160 || { echo "Could not read PR ${{ matrix.number }}" >&2; exit 1; }
143161 echo "eligible=$state" >> "$GITHUB_OUTPUT"
144- [ "$state" = yes ] || echo "PR ${{ matrix.number }} is open or labeled again; leaving $STAGE alone."
162+ [ "$state" = yes ] || echo "PR ${{ matrix.number }} is open again; leaving $STAGE alone."
145163
146164 - name : Checkout default branch
147165 if : steps.eligible.outputs.eligible == 'yes'
@@ -172,3 +190,7 @@ jobs:
172190 PLANETSCALE_API_TOKEN : ${{ secrets.PLANETSCALE_API_TOKEN }}
173191 AXIOM_TOKEN : ${{ secrets.AXIOM_TOKEN }}
174192 CLERK_SECRET_KEY : ${{ secrets.CLERK_SECRET_KEY }}
193+
194+ - name : Clear the teardown marker
195+ if : steps.eligible.outputs.eligible == 'yes'
196+ run : gh pr edit "${{ matrix.number }}" --repo "$GITHUB_REPOSITORY" --remove-label relay:preview
0 commit comments