Github actions fixes #13
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Delete Review App | ||
| on: | ||
| pull_request: | ||
| types: [closed] | ||
| issue_comment: | ||
| types: [created] | ||
| permissions: | ||
| contents: read | ||
| deployments: write | ||
| pull-requests: write | ||
| issues: write | ||
| env: | ||
| CPLN_ORG: ${{ secrets.CPLN_ORG }} | ||
| CPLN_TOKEN: ${{ secrets.CPLN_TOKEN }} | ||
| APP_NAME: qa-react-webpack-rails-tutorial-pr-${{ github.event.pull_request.number || github.event.issue.number }} | ||
| PR_NUMBER: ${{ github.event.pull_request.number || github.event.issue.number }} | ||
| jobs: | ||
| Process-Delete-Command: | ||
| if: | | ||
| (github.event_name == 'issue_comment' && | ||
| github.event.issue.pull_request && | ||
| github.event.comment.body == '/delete-review-app') || | ||
| (github.event_name == 'pull_request' && | ||
| github.event.action == 'closed') | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - name: Validate Required Secrets | ||
| run: | | ||
| missing_secrets=() | ||
| for secret in "CPLN_TOKEN" "CPLN_ORG"; do | ||
| if [ -z "${!secret}" ]; then | ||
| missing_secrets+=("$secret") | ||
| fi | ||
| done | ||
| if [ ${#missing_secrets[@]} -ne 0 ]; then | ||
| echo "Required secrets are not set: ${missing_secrets[*]}" | ||
| exit 1 | ||
| fi | ||
| - name: Setup Environment | ||
| uses: ./.github/actions/setup-environment | ||
| - name: Set shared functions | ||
| id: shared-functions | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| script: | | ||
| core.exportVariable('GET_CONSOLE_LINK', ` | ||
| function getConsoleLink(prNumber) { | ||
| return ' [Control Plane Console for Review App with PR #' + prNumber + '](' + | ||
| 'https://console.cpln.io/org/' + process.env.CPLN_ORG + '/workloads/' + process.env.APP_NAME + ')'; | ||
| } | ||
| `); | ||
| - name: Initialize Delete | ||
| id: init-delete | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| script: | | ||
| eval(process.env.GET_CONSOLE_LINK); | ||
| async function getWorkflowUrl(runId) { | ||
| // Get the current job ID | ||
| const jobs = await github.rest.actions.listJobsForWorkflowRun({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| run_id: runId | ||
| }); | ||
| const currentJob = jobs.data.jobs.find(job => job.status === 'in_progress'); | ||
| const jobId = currentJob?.id; | ||
| if (!jobId) { | ||
| console.log('Warning: Could not find current job ID'); | ||
| return `${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${runId}`; | ||
| } | ||
| return `${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${runId}/job/${jobId}`; | ||
| } | ||
| const workflowUrl = await getWorkflowUrl(context.runId); | ||
| const comment = await github.rest.issues.createComment({ | ||
| issue_number: process.env.PR_NUMBER, | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| body: [ | ||
| ' Starting app deletion...', | ||
| '', | ||
| ' [View Delete Logs](' + workflowUrl + ')', | ||
| '', | ||
| getConsoleLink(process.env.PR_NUMBER) | ||
| ].join('\n') | ||
| }); | ||
| return { | ||
| commentId: comment.data.id, | ||
| workflowUrl | ||
| }; | ||
| - name: Set workflow URL | ||
| run: | | ||
| echo "WORKFLOW_URL=${{ fromJSON(steps.init-delete.outputs.result).workflowUrl }}" >> $GITHUB_ENV | ||
| - name: Create Initial Delete Comment | ||
| id: init-delete | ||
|
Check failure on line 114 in .github/workflows/delete-review-app.yml
|
||
| uses: actions/github-script@v7 | ||
| with: | ||
| script: | | ||
| let message = '🗑️ Starting app deletion'; | ||
| if ('${{ github.event_name }}' === 'pull_request') { | ||
| const merged = '${{ github.event.pull_request.merged }}' === 'true'; | ||
| message += merged ? ' (PR merged)' : ' (PR closed)'; | ||
| } | ||
| const comment = await github.rest.issues.createComment({ | ||
| issue_number: process.env.PR_NUMBER, | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| body: message | ||
| }); | ||
| return { commentId: comment.data.id }; | ||
| - name: Delete Review App | ||
| uses: ./.github/actions/delete-control-plane-app | ||
| with: | ||
| app_name: ${{ env.APP_NAME }} | ||
| org: ${{ env.CPLN_ORG }} | ||
| github_token: ${{ secrets.GITHUB_TOKEN }} | ||
| env: | ||
| CPLN_TOKEN: ${{ secrets.CPLN_TOKEN }} | ||
| - name: Update Delete Status | ||
| if: always() | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| script: | | ||
| eval(process.env.GET_CONSOLE_LINK); | ||
| const success = '${{ job.status }}' === 'success'; | ||
| const prNumber = process.env.PR_NUMBER; | ||
| const successMessage = [ | ||
| '✅ Review app for PR #' + prNumber + ' was successfully deleted', | ||
| '', | ||
| ' [View Completed Delete Logs](' + process.env.WORKFLOW_URL + ')' | ||
| ].join('\n'); | ||
| const failureMessage = [ | ||
| '❌ Review app for PR #' + prNumber + ' failed to be deleted', | ||
| '', | ||
| ' [View Delete Logs with Errors](' + process.env.WORKFLOW_URL + ')', | ||
| '', | ||
| getConsoleLink(prNumber) | ||
| ].join('\n'); | ||
| await github.rest.issues.updateComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| comment_id: ${{ fromJSON(steps.init-delete.outputs.result).commentId }}, | ||
| body: success ? successMessage : failureMessage | ||
| }); | ||