-
Notifications
You must be signed in to change notification settings - Fork 373
170 lines (144 loc) · 5.95 KB
/
delete-review-app.yml
File metadata and controls
170 lines (144 loc) · 5.95 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
name: Delete Review App
on:
pull_request:
types: [closed]
issue_comment:
types: [created]
workflow_dispatch:
inputs:
pr_number:
description: 'PR number of the review app targeted for deletion'
required: true
type: string
permissions:
contents: read
deployments: write
pull-requests: write
issues: write
env:
PREFIX: ${{ vars.REVIEW_APP_PREFIX }}
CPLN_ORG: ${{ vars.CPLN_ORG_STAGING }}
CPLN_TOKEN: ${{ secrets.CPLN_TOKEN_STAGING }}
APP_NAME: ${{ vars.REVIEW_APP_PREFIX }}-${{ 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:
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
shell: bash
run: |
missing=()
# Check required secrets
if [ -z "$CPLN_TOKEN" ]; then
missing+=("Secret: CPLN_TOKEN_STAGING")
fi
# Check required variables
if [ -z "$CPLN_ORG" ]; then
missing+=("Variable: CPLN_ORG_STAGING")
fi
if [ -z "$PREFIX" ]; then
missing+=("Variable: REVIEW_APP_PREFIX")
fi
if [ ${#missing[@]} -ne 0 ]; then
echo "Required secrets/variables are not set: ${missing[*]}"
exit 1
fi
- 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...'
});
return { commentId: comment.data.id };
- name: Delete Review App
uses: ./.github/actions/delete-control-plane-app
with:
app_name: ${{ env.APP_NAME }}
cpln_org: ${{ vars.CPLN_ORG_STAGING }}
- name: Update Delete Status
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
});