|
| 1 | +name: Delete Review App |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + control_plane_flow_ref: |
| 7 | + description: Git ref used to load shared cpflow composite actions. |
| 8 | + required: false |
| 9 | + type: string |
| 10 | + default: main |
| 11 | + |
| 12 | +permissions: |
| 13 | + contents: read |
| 14 | + issues: write |
| 15 | + pull-requests: write |
| 16 | + |
| 17 | +concurrency: |
| 18 | + group: cpflow-delete-review-app-${{ github.event.pull_request.number || github.event.issue.number || github.event.inputs.pr_number }} |
| 19 | + # Deletions must not cancel each other mid-flight — a cancelled `cpln` delete can leave |
| 20 | + # partial state behind. Let the in-progress deletion finish before the next run starts. |
| 21 | + cancel-in-progress: false |
| 22 | + |
| 23 | +env: |
| 24 | + APP_NAME: ${{ vars.REVIEW_APP_PREFIX }}-${{ github.event.pull_request.number || github.event.issue.number || github.event.inputs.pr_number }} |
| 25 | + CPLN_ORG: ${{ vars.CPLN_ORG_STAGING }} |
| 26 | + PR_NUMBER: ${{ github.event.pull_request.number || github.event.issue.number || github.event.inputs.pr_number }} |
| 27 | + |
| 28 | +jobs: |
| 29 | + delete-review-app: |
| 30 | + if: | |
| 31 | + (github.event_name == 'issue_comment' && |
| 32 | + github.event.issue.pull_request && |
| 33 | + contains(fromJson('["+review-app-delete","+review-app-delete\n","+review-app-delete\r\n"]'), github.event.comment.body) && |
| 34 | + contains(fromJson('["OWNER","MEMBER","COLLABORATOR"]'), github.event.comment.author_association)) || |
| 35 | + (github.event_name == 'pull_request_target' && github.event.action == 'closed') || |
| 36 | + github.event_name == 'workflow_dispatch' |
| 37 | + runs-on: ubuntu-latest |
| 38 | + timeout-minutes: 15 |
| 39 | + |
| 40 | + steps: |
| 41 | + - name: React to delete command |
| 42 | + if: github.event_name == 'issue_comment' |
| 43 | + continue-on-error: true |
| 44 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd |
| 45 | + with: |
| 46 | + script: | |
| 47 | + try { |
| 48 | + await github.rest.reactions.createForIssueComment({ |
| 49 | + owner: context.repo.owner, |
| 50 | + repo: context.repo.repo, |
| 51 | + comment_id: context.payload.comment.id, |
| 52 | + content: "eyes" |
| 53 | + }); |
| 54 | + } catch (error) { |
| 55 | + if (error.status === 422) { |
| 56 | + core.info("Delete command reaction already exists."); |
| 57 | + } else { |
| 58 | + throw error; |
| 59 | + } |
| 60 | + } |
| 61 | +
|
| 62 | + - name: Checkout control-plane-flow actions |
| 63 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd |
| 64 | + with: |
| 65 | + repository: shakacode/control-plane-flow |
| 66 | + ref: ${{ inputs.control_plane_flow_ref }} |
| 67 | + path: .cpflow |
| 68 | + persist-credentials: false |
| 69 | + |
| 70 | + - name: Validate required secrets and variables |
| 71 | + id: config |
| 72 | + uses: ./.cpflow/.github/actions/cpflow-validate-config |
| 73 | + env: |
| 74 | + CPLN_TOKEN_STAGING: ${{ secrets.CPLN_TOKEN_STAGING }} |
| 75 | + CPLN_ORG_STAGING: ${{ vars.CPLN_ORG_STAGING }} |
| 76 | + REVIEW_APP_PREFIX: ${{ vars.REVIEW_APP_PREFIX }} |
| 77 | + with: |
| 78 | + required: | |
| 79 | + secret:CPLN_TOKEN_STAGING |
| 80 | + variable:CPLN_ORG_STAGING |
| 81 | + variable:REVIEW_APP_PREFIX |
| 82 | + pull_request_friendly: "true" |
| 83 | + |
| 84 | + - name: Setup environment |
| 85 | + if: steps.config.outputs.ready == 'true' |
| 86 | + uses: ./.cpflow/.github/actions/cpflow-setup-environment |
| 87 | + with: |
| 88 | + token: ${{ secrets.CPLN_TOKEN_STAGING }} |
| 89 | + org: ${{ vars.CPLN_ORG_STAGING }} |
| 90 | + working_directory: .cpflow |
| 91 | + cpln_cli_version: ${{ vars.CPLN_CLI_VERSION }} |
| 92 | + cpflow_version: ${{ vars.CPFLOW_VERSION }} |
| 93 | + |
| 94 | + - name: Set workflow links |
| 95 | + if: steps.config.outputs.ready == 'true' |
| 96 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd |
| 97 | + with: |
| 98 | + script: | |
| 99 | + const workflowUrl = `${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`; |
| 100 | + core.exportVariable("WORKFLOW_URL", workflowUrl); |
| 101 | + core.exportVariable( |
| 102 | + "CONSOLE_URL", |
| 103 | + `https://console.cpln.io/console/org/${process.env.CPLN_ORG}/-info` |
| 104 | + ); |
| 105 | +
|
| 106 | + - name: Create initial PR comment |
| 107 | + if: steps.config.outputs.ready == 'true' |
| 108 | + id: create-comment |
| 109 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd |
| 110 | + with: |
| 111 | + script: | |
| 112 | + const body = [ |
| 113 | + "## 🗑️ Deleting review app...", |
| 114 | + "", |
| 115 | + `_Removing the review app for PR #${process.env.PR_NUMBER}_` |
| 116 | + ].join("\n"); |
| 117 | +
|
| 118 | + const comment = await github.rest.issues.createComment({ |
| 119 | + owner: context.repo.owner, |
| 120 | + repo: context.repo.repo, |
| 121 | + issue_number: Number(process.env.PR_NUMBER), |
| 122 | + body |
| 123 | + }); |
| 124 | + core.setOutput("comment-id", comment.data.id); |
| 125 | +
|
| 126 | + - name: Delete review app |
| 127 | + if: steps.config.outputs.ready == 'true' |
| 128 | + uses: ./.cpflow/.github/actions/cpflow-delete-control-plane-app |
| 129 | + with: |
| 130 | + app_name: ${{ env.APP_NAME }} |
| 131 | + cpln_org: ${{ vars.CPLN_ORG_STAGING }} |
| 132 | + review_app_prefix: ${{ vars.REVIEW_APP_PREFIX }} |
| 133 | + |
| 134 | + # Finalizer still runs after delete failures, but only after config validation |
| 135 | + # created the initial PR comment and workflow link env vars it updates. |
| 136 | + - name: Finalize delete status |
| 137 | + if: always() && steps.config.outputs.ready == 'true' |
| 138 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd |
| 139 | + env: |
| 140 | + COMMENT_ID: ${{ steps.create-comment.outputs.comment-id }} |
| 141 | + JOB_STATUS: ${{ job.status }} |
| 142 | + with: |
| 143 | + script: | |
| 144 | + const commentId = Number(process.env.COMMENT_ID); |
| 145 | + const success = process.env.JOB_STATUS === "success"; |
| 146 | + const body = success |
| 147 | + ? [ |
| 148 | + "## ✅ Review App Deleted", |
| 149 | + "", |
| 150 | + `_Review app for PR #${process.env.PR_NUMBER} is deleted_`, |
| 151 | + "", |
| 152 | + `🎮 [Control Plane Console](${process.env.CONSOLE_URL})`, |
| 153 | + `📋 [View Workflow Logs](${process.env.WORKFLOW_URL})` |
| 154 | + ].join("\n") |
| 155 | + : [ |
| 156 | + "## ❌ Failed to Delete Review App", |
| 157 | + "", |
| 158 | + `_Failed to delete review app for PR #${process.env.PR_NUMBER}_`, |
| 159 | + "", |
| 160 | + `🎮 [Control Plane Console](${process.env.CONSOLE_URL})`, |
| 161 | + `📋 [View Workflow Logs](${process.env.WORKFLOW_URL})` |
| 162 | + ].join("\n"); |
| 163 | +
|
| 164 | + if (!Number.isFinite(commentId) || commentId <= 0) { |
| 165 | + core.warning("Skipping delete status comment update because no comment id was created."); |
| 166 | + return; |
| 167 | + } |
| 168 | +
|
| 169 | + await github.rest.issues.updateComment({ |
| 170 | + owner: context.repo.owner, |
| 171 | + repo: context.repo.repo, |
| 172 | + comment_id: commentId, |
| 173 | + body |
| 174 | + }); |
0 commit comments