-
Notifications
You must be signed in to change notification settings - Fork 374
158 lines (135 loc) · 5.52 KB
/
delete-review-app.yml
File metadata and controls
158 lines (135 loc) · 5.52 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
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_STAGING }}
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_STAGING" "CPLN_ORG_STAGING"; 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](' +
'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: [
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:
CPLN_TOKEN: ${{ secrets.CPLN_TOKEN_STAGING }}
- 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 + ')',
'',
' [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
});