-
Notifications
You must be signed in to change notification settings - Fork 1
70 lines (64 loc) · 2.51 KB
/
Copy pathbench-cleanup.yml
File metadata and controls
70 lines (64 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
name: bench-cleanup
# Triggered whenever bench-sweep finishes — success, failure, OR cancellation.
# Deletes the Hetzner CCX63 server that was provisioned for the sweep.
on:
workflow_run:
workflows: [bench-sweep]
types: [completed]
permissions:
actions: read
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- name: Download server_id artifact from triggering run
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: hetzner-server-id
github-token: ${{ secrets.GH_PAT }}
run-id: ${{ github.event.workflow_run.id }}
continue-on-error: true # no artifact if provision never ran
- name: Delete Hetzner server
env:
HCLOUD_TOKEN: ${{ secrets.HCLOUD_TOKEN }}
run: |
set -euo pipefail
if [[ ! -f server_id.txt ]]; then
echo "No server_id.txt found — provision job may not have run. Nothing to delete."
exit 0
fi
SERVER_ID=$(tr -d '[:space:]' < server_id.txt)
if [[ -z "$SERVER_ID" || "$SERVER_ID" == "null" ]]; then
echo "server_id is empty — nothing to delete."
exit 0
fi
echo "Deleting Hetzner server $SERVER_ID..."
STATUS=$(curl -s -o /dev/null -w "%{http_code}" -X DELETE \
"https://api.hetzner.cloud/v1/servers/${SERVER_ID}" \
-H "Authorization: Bearer ${HCLOUD_TOKEN}")
if [[ "$STATUS" == "200" || "$STATUS" == "204" || "$STATUS" == "404" ]]; then
echo "Server $SERVER_ID deleted (HTTP $STATUS)."
else
echo "ERROR: Unexpected HTTP $STATUS when deleting server $SERVER_ID." >&2
exit 1
fi
- name: Deregister runners (best-effort)
env:
GH_TOKEN: ${{ secrets.GH_PAT }}
REPO: ${{ github.repository }}
run: |
set -euo pipefail
RUNNERS=$(curl -sf \
-H "Authorization: Bearer ${GH_TOKEN}" \
"https://api.github.com/repos/${REPO}/actions/runners?per_page=100" \
| jq -r '.runners[] | select(.status == "offline") | .id')
for RID in $RUNNERS; do
if curl -sf -X DELETE \
-H "Authorization: Bearer ${GH_TOKEN}" \
"https://api.github.com/repos/${REPO}/actions/runners/${RID}"; then
echo "Removed offline runner $RID"
fi
done