Skip to content

Github Actions Promote #66

Github Actions Promote

Github Actions Promote #66

name: Delete Review App
on:
pull_request:
types: [closed]
issue_comment:
types: [created]
workflow_dispatch:
inputs:
pr_number:
description: 'PR number to delete review app for'
required: true
type: string
permissions:
contents: read
deployments: write
pull-requests: write
issues: write
env:
CPLN_ORG: ${{ vars.CPLN_ORG_STAGING }}
CPLN_TOKEN: ${{ secrets.CPLN_TOKEN_STAGING }}
APP_NAME: ${{ vars.REVIEW_APP_PREFIX }}-pr-${{ github.event.pull_request.number || github.event.issue.number || inputs.pr_number }}
PR_NUMBER: ${{ github.event.pull_request.number || github.event.issue.number || inputs.pr_number }}
jobs:
debug:
uses: ./.github/workflows/debug-workflow.yml
with:
debug_enabled: false # Will still run if vars.DEBUG_WORKFLOW is true
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') ||
github.event_name == 'workflow_dispatch'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Validate Required Secrets and Variables
uses: ./.github/actions/validate-required-vars
- name: Setup Environment
uses: ./.github/actions/setup-environment
with:
org: ${{ env.CPLN_ORG }}
token: ${{ env.CPLN_TOKEN }}
- 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](' +
'https://console.cpln.io/console/org/' + process.env.CPLN_ORG + '/gvc/' + process.env.APP_NAME + '/-info)';
}
`);
- name: Setup Workflow URL
id: setup-workflow-url
uses: actions/github-script@v7
with:
script: |
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);
core.exportVariable('WORKFLOW_URL', workflowUrl);
return { workflowUrl };
- name: Create Initial Delete Comment
id: create-delete-comment
uses: actions/github-script@v7
with:
script: |
eval(process.env.GET_CONSOLE_LINK);
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: '🗑️ Starting app deletion...'
body: [
message,
'',
' 🗑️ [View Delete Logs](' + process.env.WORKFLOW_URL + ')',
'',
getConsoleLink(process.env.PR_NUMBER)
].join('\n')
});
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:
APP_NAME: ${{ env.APP_NAME }}
CPLN_ORG: ${{ secrets.CPLN_ORG }}
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 cpConsoleUrl = `https://console.cpln.io/org/${process.env.CPLN_ORG}/workloads/${process.env.APP_NAME}`;
const successMessage = [
'✅ Review app for PR #' + prNumber + ' was successfully deleted',
'',
' [View Completed Delete Logs](' + process.env.WORKFLOW_URL + ')',
'',
' [Control Plane Organization](https://console.cpln.io/console/org/' + process.env.CPLN_ORG + '/-info)'
].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.create-delete-comment.outputs.result).commentId }},
body: success ? successMessage : failureMessage
});