|
1 | 1 | name: Preview cleanup |
2 | 2 |
|
| 3 | +# Since 2025-12-08 `pull_request_target` always takes the workflow file and the checked-out commit from |
| 4 | +# the default branch, so no fork code runs beside the secrets here. A Coolify 2xx is not |
| 5 | +# proof of teardown, so this ends with a forced SSH command that proves the resources are gone. |
3 | 6 | on: |
4 | | - pull_request: |
5 | | - branches: ["**"] |
6 | | - types: [closed] |
| 7 | + pull_request_target: |
| 8 | + types: [closed, unlabeled, converted_to_draft] |
7 | 9 |
|
8 | | -permissions: |
9 | | - contents: read |
| 10 | +permissions: {} |
| 11 | + |
| 12 | +concurrency: |
| 13 | + group: hephaestus-preview-lifecycle |
| 14 | + queue: max |
10 | 15 |
|
11 | 16 | jobs: |
12 | 17 | cleanup: |
13 | | - name: "Preview / Delete Coolify resources" |
| 18 | + name: "Preview / Remove and verify resources" |
14 | 19 | if: >- |
15 | 20 | vars.COOLIFY_URL != '' && |
16 | 21 | vars.COOLIFY_APP_UUID != '' && |
17 | | - github.event.pull_request.head.repo.full_name == github.repository |
| 22 | + vars.PREVIEW_HOST != '' && |
| 23 | + github.event.pull_request.head.repo.full_name == github.repository && |
| 24 | + (github.event.label.name == 'preview' || |
| 25 | + (github.event.action != 'unlabeled' && |
| 26 | + contains(github.event.pull_request.labels.*.name, 'preview'))) |
18 | 27 | runs-on: ubuntu-latest |
19 | | - timeout-minutes: 3 |
| 28 | + permissions: |
| 29 | + contents: read |
| 30 | + deployments: write |
| 31 | + pull-requests: write |
| 32 | + timeout-minutes: 8 |
20 | 33 | env: |
21 | 34 | COOLIFY_URL: ${{ vars.COOLIFY_URL }} |
22 | 35 | COOLIFY_APP_UUID: ${{ vars.COOLIFY_APP_UUID }} |
23 | | - COOLIFY_TOKEN: ${{ secrets.COOLIFY_API_TOKEN }} |
24 | 36 | PR_NUMBER: ${{ github.event.pull_request.number }} |
| 37 | + PR_URL: ${{ github.event.pull_request.html_url }} |
| 38 | + PR_TITLE: ${{ github.event.pull_request.title }} |
| 39 | + AUTHOR_ASSOCIATION: ${{ github.event.pull_request.author_association }} |
| 40 | + HEAD_REF: ${{ github.event.pull_request.head.ref }} |
| 41 | + HEAD_SHA: ${{ github.event.pull_request.head.sha }} |
| 42 | + # Coolify's routing key: the branch its preview application is configured for. |
| 43 | + BASE_REF: ${{ github.event.repository.default_branch }} |
| 44 | + ENVIRONMENT: preview/pr-${{ github.event.pull_request.number }} |
25 | 45 | steps: |
26 | | - - name: Delete preview containers, volumes, and network |
| 46 | + - name: Load the trusted preview adapter |
| 47 | + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
| 48 | + with: |
| 49 | + ref: ${{ github.sha }} |
| 50 | + persist-credentials: false |
| 51 | + sparse-checkout: | |
| 52 | + .github/actions/setup-bun |
| 53 | + package.json |
| 54 | + scripts |
| 55 | +
|
| 56 | + - name: Set up the repository's Bun version |
| 57 | + uses: ./.github/actions/setup-bun |
| 58 | + |
| 59 | + - name: Request Coolify cleanup through the signed close event |
| 60 | + id: close |
| 61 | + continue-on-error: true |
| 62 | + env: |
| 63 | + COOLIFY_WEBHOOK_SECRET: ${{ secrets.COOLIFY_PREVIEW_WEBHOOK_SECRET }} |
| 64 | + run: bun scripts/coolify-preview.ts close |
| 65 | + |
| 66 | + - name: Remove and verify matching host resources |
| 67 | + id: host_cleanup |
| 68 | + if: always() |
| 69 | + env: |
| 70 | + PREVIEW_HOST: ${{ vars.PREVIEW_HOST }} |
| 71 | + PREVIEW_HOST_KEY: ${{ vars.PREVIEW_HOST_KEY }} |
| 72 | + PREVIEW_SSH_USER: ${{ vars.PREVIEW_SSH_USER }} |
| 73 | + PREVIEW_SSH_PRIVATE_KEY: ${{ secrets.PREVIEW_SSH_PRIVATE_KEY }} |
| 74 | + run: bun scripts/preview-ssh.ts cleanup "${PR_NUMBER}" |
| 75 | + |
| 76 | + - name: Keep a verified cleanup tombstone |
| 77 | + id: tombstone |
| 78 | + # Proven-absent host resources are what frees the slot. A Coolify hiccup must not leave the |
| 79 | + # environment counting against admission forever; the tombstone says the nightly run still |
| 80 | + # owes Coolify a second pass. |
| 81 | + if: always() && steps.host_cleanup.outcome == 'success' |
| 82 | + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 83 | + with: |
| 84 | + script: | |
| 85 | + const controller = await import(`${process.env.GITHUB_WORKSPACE}/scripts/preview-controller.ts`); |
| 86 | + await controller.inactivate({ github, context, core }); |
| 87 | +
|
| 88 | + - name: Report the released preview |
| 89 | + # Always overwrites the sticky comment, closed pull requests included: a torn-down preview |
| 90 | + # must never leave a live-looking link behind. |
| 91 | + if: always() && steps.tombstone.outcome == 'success' |
| 92 | + uses: marocchino/sticky-pull-request-comment@773744901bac0e8cbb5a0dc842800d45e9b2b405 # v2 |
| 93 | + with: |
| 94 | + header: app-preview |
| 95 | + number: ${{ github.event.pull_request.number }} |
| 96 | + message: | |
| 97 | + ## 🧹 App Preview |
| 98 | +
|
| 99 | + ~~Preview removed~~ — the slot on the preview host is free again. |
| 100 | +
|
| 101 | + <sub>Add the `preview` label, or mark the pull request ready for review, to deploy it again.</sub> |
| 102 | +
|
| 103 | + - name: Fail if cleanup could not be verified |
| 104 | + if: >- |
| 105 | + always() && |
| 106 | + (steps.close.outcome != 'success' || |
| 107 | + steps.host_cleanup.outcome != 'success') |
27 | 108 | run: | |
28 | | - set -euo pipefail |
29 | | -
|
30 | | - if [ -z "${COOLIFY_TOKEN}" ]; then |
31 | | - echo "::notice::COOLIFY_API_TOKEN is not configured; preview cleanup must be done manually." |
32 | | - exit 0 |
33 | | - fi |
34 | | -
|
35 | | - body=$(mktemp) |
36 | | - status=$(curl -sS -o "${body}" -w '%{http_code}' -X DELETE \ |
37 | | - -H "Authorization: Bearer ${COOLIFY_TOKEN}" \ |
38 | | - -H 'Accept: application/json' \ |
39 | | - --retry 3 --retry-connrefused --max-time 60 \ |
40 | | - "${COOLIFY_URL}/api/v1/applications/${COOLIFY_APP_UUID}/previews/${PR_NUMBER}") |
41 | | -
|
42 | | - if [ "${status}" -eq 404 ]; then |
43 | | - echo "::notice::PR #${PR_NUMBER} had no Coolify preview to clean up." |
44 | | - exit 0 |
45 | | - fi |
46 | | - if [ "${status}" -ge 400 ]; then |
47 | | - echo "::error::Coolify preview cleanup returned HTTP ${status}: $(cat "${body}")" |
48 | | - exit 1 |
49 | | - fi |
50 | | -
|
51 | | - echo "::notice::Deleted Coolify preview and persistent volumes for PR #${PR_NUMBER}." |
| 109 | + echo "::error::Preview cleanup was not fully verified; the nightly reconciler will retry." |
| 110 | + exit 1 |
0 commit comments