Skip to content

Commit e83b55c

Browse files
committed
delete-on-pr-close
1 parent f489d78 commit e83b55c

File tree

2 files changed

+32
-280
lines changed

2 files changed

+32
-280
lines changed

.github/workflows/delete-review-app.yml

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,7 @@ env:
1919
PR_NUMBER: ${{ github.event.pull_request.number || github.event.issue.number }}
2020

2121
jobs:
22-
debug-trigger:
23-
if: always()
24-
runs-on: ubuntu-latest
25-
steps:
26-
- name: Debug Trigger Conditions
27-
env:
28-
EVENT_NAME: ${{ github.event_name }}
29-
IS_PR: ${{ toJSON(github.event.issue.pull_request) }}
30-
COMMENT: ${{ github.event.comment.body }}
31-
PR_ACTION: ${{ github.event.action }}
32-
PR_MERGED: ${{ github.event.pull_request.merged }}
33-
run: |
34-
echo "Debug information for delete-review-app command:"
35-
echo "Event name: $EVENT_NAME"
36-
echo "Is PR (raw): $IS_PR"
37-
echo "Comment body: $COMMENT"
38-
echo "PR action: $PR_ACTION"
39-
echo "PR merged: $PR_MERGED"
40-
echo "Raw event payload:"
41-
echo '${{ toJSON(github.event) }}'
42-
4322
Process-Delete-Command:
44-
needs: debug-trigger
4523
if: |
4624
(github.event_name == 'issue_comment' &&
4725
github.event.issue.pull_request &&

.github/workflows/deploy-to-control-plane.yml

Lines changed: 32 additions & 258 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,7 @@ env:
2020
PR_NUMBER: ${{ github.event.pull_request.number || github.event.issue.number }}
2121

2222
jobs:
23-
debug-trigger:
24-
if: always()
25-
runs-on: ubuntu-latest
26-
steps:
27-
- name: Debug Trigger Conditions
28-
env:
29-
EVENT_NAME: ${{ github.event_name }}
30-
IS_PR: ${{ toJSON(github.event.issue.pull_request) }}
31-
COMMENT: ${{ github.event.comment.body }}
32-
run: |
33-
echo "Debug information for deploy command:"
34-
echo "Event name: $EVENT_NAME"
35-
echo "Is PR (raw): $IS_PR"
36-
echo "Comment body: $COMMENT"
37-
echo "Raw event payload:"
38-
echo '${{ toJSON(github.event) }}'
39-
4023
Process-Deployment-Command:
41-
needs: debug-trigger
4224
if: |
4325
(github.event_name == 'pull_request') ||
4426
(github.event_name == 'issue_comment' &&
@@ -52,21 +34,17 @@ jobs:
5234
issues: write
5335

5436
steps:
37+
- uses: actions/checkout@v4
38+
5539
- name: Get PR HEAD Ref
5640
if: github.event_name == 'issue_comment'
5741
id: getRef
5842
run: |
59-
# For PR comments, get the actual PR head commit
60-
PR_DATA=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json headRefName,headRefOid)
61-
echo "PR_REF=$(echo "$PR_DATA" | jq -r '.headRefName')" >> $GITHUB_OUTPUT
62-
echo "PR_SHA=$(echo "$PR_DATA" | jq -r '.headRefOid')" >> $GITHUB_OUTPUT
63-
env:
64-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
65-
66-
- uses: actions/checkout@v4
67-
with:
68-
fetch-depth: 0
69-
ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.ref || steps.getRef.outputs.PR_REF || github.ref }}
43+
PR_URL=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
44+
-H "Accept: application/vnd.github.v3+json" \
45+
"${{ github.event.issue.pull_request.url }}")
46+
HEAD_REF=$(echo "$PR_URL" | jq -r .head.ref)
47+
echo "ref=$HEAD_REF" >> "$GITHUB_OUTPUT"
7048
7149
- name: Validate Required Secrets
7250
run: |
@@ -85,278 +63,74 @@ jobs:
8563
- name: Setup Environment
8664
uses: ./.github/actions/setup-environment
8765

88-
- name: Set shared functions
89-
id: shared-functions
90-
uses: actions/github-script@v7
91-
with:
92-
script: |
93-
core.exportVariable('GET_CONSOLE_LINK', `
94-
function getConsoleLink(prNumber) {
95-
return ' [Control Plane Console for Review App with PR #' + prNumber + '](' +
96-
'https://console.cpln.io/org/' + process.env.CPLN_ORG + '/workloads/' + process.env.APP_NAME + ')';
97-
}
98-
`);
99-
100-
- name: Initialize Deployment
101-
id: init-deployment
66+
- name: Create Initial Status Comment
67+
id: init-status
10268
uses: actions/github-script@v7
10369
with:
10470
script: |
105-
eval(process.env.GET_CONSOLE_LINK);
106-
107-
async function getWorkflowUrl(runId) {
108-
// Get the current job ID
109-
const jobs = await github.rest.actions.listJobsForWorkflowRun({
110-
owner: context.repo.owner,
111-
repo: context.repo.repo,
112-
run_id: runId
113-
});
114-
115-
const currentJob = jobs.data.jobs.find(job => job.status === 'in_progress');
116-
const jobId = currentJob?.id;
117-
118-
if (!jobId) {
119-
console.log('Warning: Could not find current job ID');
120-
return `${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${runId}`;
121-
}
122-
123-
return `${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${runId}/job/${jobId}`;
124-
}
125-
126-
// Create initial deployment comment
12771
const comment = await github.rest.issues.createComment({
128-
owner: context.repo.owner,
129-
repo: context.repo.repo,
13072
issue_number: process.env.PR_NUMBER,
131-
body: ' Initializing deployment...'
132-
});
133-
134-
// Create GitHub deployment
135-
const deployment = await github.rest.repos.createDeployment({
13673
owner: context.repo.owner,
13774
repo: context.repo.repo,
138-
ref: context.sha,
139-
environment: 'review',
140-
auto_merge: false,
141-
required_contexts: []
75+
body: '🚀 Starting deployment...'
14276
});
143-
144-
const workflowUrl = await getWorkflowUrl(context.runId);
145-
146-
return {
147-
deploymentId: deployment.data.id,
148-
commentId: comment.data.id,
149-
workflowUrl
150-
};
151-
152-
- name: Set comment ID and workflow URL
153-
run: |
154-
echo "COMMENT_ID=${{ fromJSON(steps.init-deployment.outputs.result).commentId }}" >> $GITHUB_ENV
155-
echo "WORKFLOW_URL=${{ fromJSON(steps.init-deployment.outputs.result).workflowUrl }}" >> $GITHUB_ENV
156-
157-
- name: Set commit hash
158-
run: |
159-
FULL_COMMIT="${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || steps.getRef.outputs.PR_SHA || github.sha }}"
160-
echo "COMMIT_HASH=${FULL_COMMIT:0:7}" >> $GITHUB_ENV
77+
return { commentId: comment.data.id };
16178
16279
- name: Update Status - Building
16380
uses: actions/github-script@v7
16481
with:
16582
script: |
16683
eval(process.env.GET_CONSOLE_LINK);
167-
168-
const buildingMessage = [
169-
' Building Docker image for PR #' + process.env.PR_NUMBER + ', commit ' + '${{ env.COMMIT_HASH }}',
170-
'',
171-
' [View Build Logs](' + process.env.WORKFLOW_URL + ')',
172-
'',
173-
getConsoleLink(process.env.PR_NUMBER)
174-
].join('\n');
175-
176-
await github.rest.issues.updateComment({
177-
owner: context.repo.owner,
178-
repo: context.repo.repo,
179-
comment_id: process.env.COMMENT_ID,
180-
body: buildingMessage
181-
});
182-
183-
- name: Build Docker Image
184-
uses: ./.github/actions/build-docker-image
185-
with:
186-
app_name: ${{ env.APP_NAME }}
187-
org: ${{ env.CPLN_ORG }}
188-
commit: ${{ env.COMMIT_HASH }}
189-
PR_NUMBER: ${{ env.PR_NUMBER }}
190-
191-
- name: Update Status - Deploying
192-
uses: actions/github-script@v7
193-
with:
194-
script: |
195-
eval(process.env.GET_CONSOLE_LINK);
196-
197-
const deployingMessage = [
198-
' Deploying to Control Plane...',
199-
'',
200-
' Waiting for deployment to be ready...',
201-
'',
202-
' [View Deploy Logs](' + process.env.WORKFLOW_URL + ')',
203-
'',
204-
getConsoleLink(process.env.PR_NUMBER)
205-
].join('\n');
20684
20785
await github.rest.issues.updateComment({
20886
owner: context.repo.owner,
20987
repo: context.repo.repo,
210-
comment_id: process.env.COMMENT_ID,
211-
body: deployingMessage
88+
comment_id: ${{ fromJSON(steps.init-status.outputs.result).commentId }},
89+
body: [
90+
'🏗️ Building review app...',
91+
'',
92+
getConsoleLink(process.env.PR_NUMBER)
93+
].join('\n')
21294
});
21395
214-
- name: Deploy to Control Plane
96+
- name: Deploy Review App
97+
id: deploy
21598
uses: ./.github/actions/deploy-to-control-plane
21699
with:
217100
app_name: ${{ env.APP_NAME }}
218101
org: ${{ env.CPLN_ORG }}
102+
wait_timeout: 900
219103
github_token: ${{ secrets.GITHUB_TOKEN }}
220-
wait_timeout: ${{ vars.WAIT_TIMEOUT || 900 }}
104+
env:
105+
CPLN_TOKEN: ${{ secrets.CPLN_TOKEN }}
221106

222-
- name: Update Status - Deployment Complete
107+
- name: Update Status - Complete
108+
if: always()
223109
uses: actions/github-script@v7
224110
with:
225111
script: |
226112
eval(process.env.GET_CONSOLE_LINK);
227-
228-
const prNumber = process.env.PR_NUMBER;
229-
const appUrl = process.env.REVIEW_APP_URL;
230-
const workflowUrl = process.env.WORKFLOW_URL;
231-
const isSuccess = '${{ job.status }}' === 'success';
232-
233-
// Create GitHub deployment status
234-
const deploymentStatus = {
235-
owner: context.repo.owner,
236-
repo: context.repo.repo,
237-
deployment_id: ${{ fromJSON(steps.init-deployment.outputs.result).deploymentId }},
238-
state: isSuccess ? 'success' : 'failure',
239-
environment_url: isSuccess ? appUrl : undefined,
240-
log_url: workflowUrl,
241-
environment: 'review'
242-
};
243113
244-
await github.rest.repos.createDeploymentStatus(deploymentStatus);
114+
const isSuccess = '${{ job.status }}' === 'success';
115+
const railsUrl = '${{ steps.deploy.outputs.rails_url }}';
245116
246-
// Define messages based on deployment status
247117
const successMessage = [
248-
' Deployment complete for PR #' + prNumber + ', commit ' + '${{ env.COMMIT_HASH }}',
249-
'',
250-
' [Review App for PR #' + prNumber + '](' + appUrl + ')',
118+
'✅ Review app deployed successfully!',
251119
'',
252-
' [View Completed Action Build and Deploy Logs](' + workflowUrl + ')',
120+
'🌐 [Rails App](' + railsUrl + ')',
253121
'',
254-
getConsoleLink(prNumber)
122+
getConsoleLink(process.env.PR_NUMBER)
255123
].join('\n');
256124
257125
const failureMessage = [
258-
' Deployment failed for PR #' + prNumber + ', commit ' + '${{ env.COMMIT_HASH }}',
259-
'',
260-
' [View Deployment Logs with Errors](' + workflowUrl + ')',
126+
'❌ Review app deployment failed',
261127
'',
262-
getConsoleLink(prNumber)
128+
getConsoleLink(process.env.PR_NUMBER)
263129
].join('\n');
264130
265-
// Update the existing comment
266131
await github.rest.issues.updateComment({
267132
owner: context.repo.owner,
268133
repo: context.repo.repo,
269-
comment_id: process.env.COMMENT_ID,
134+
comment_id: ${{ fromJSON(steps.init-status.outputs.result).commentId }},
270135
body: isSuccess ? successMessage : failureMessage
271-
});
272-
273-
debug-delete:
274-
if: always()
275-
runs-on: ubuntu-latest
276-
steps:
277-
- name: Debug Trigger Conditions
278-
env:
279-
EVENT_NAME: ${{ github.event_name }}
280-
IS_PR: ${{ toJSON(github.event.issue.pull_request) }}
281-
COMMENT: ${{ github.event.comment.body }}
282-
run: |
283-
echo "Debug information for delete-review-app command:"
284-
echo "Event name: $EVENT_NAME"
285-
echo "Is PR (raw): $IS_PR"
286-
echo "Comment body: $COMMENT"
287-
echo "Raw event payload:"
288-
echo '${{ toJSON(github.event) }}'
289-
290-
Process-Delete-Command:
291-
needs: debug-delete
292-
if: |
293-
github.event_name == 'issue_comment' &&
294-
github.event.issue.pull_request &&
295-
github.event.comment.body == '/delete-review-app'
296-
runs-on: ubuntu-latest
297-
298-
steps:
299-
- uses: actions/checkout@v4
300-
301-
- name: Validate Required Secrets
302-
run: |
303-
missing_secrets=()
304-
for secret in "CPLN_TOKEN" "CPLN_ORG"; do
305-
if [ -z "${!secret}" ]; then
306-
missing_secrets+=("$secret")
307-
fi
308-
done
309-
310-
if [ ${#missing_secrets[@]} -ne 0 ]; then
311-
echo "Required secrets are not set: ${missing_secrets[*]}"
312-
exit 1
313-
fi
314-
315-
- name: Setup Environment
316-
uses: ./.github/actions/setup-environment
317-
318-
- name: Create Initial Delete Comment
319-
id: init-delete
320-
uses: actions/github-script@v7
321-
with:
322-
script: |
323-
const comment = await github.rest.issues.createComment({
324-
issue_number: process.env.PR_NUMBER,
325-
owner: context.repo.owner,
326-
repo: context.repo.repo,
327-
body: ' Starting app deletion...'
328-
});
329-
return { commentId: comment.data.id };
330-
331-
- name: Delete Review App
332-
uses: ./.github/actions/delete-control-plane-app
333-
with:
334-
app_name: ${{ env.APP_NAME }}
335-
org: ${{ env.CPLN_ORG }}
336-
github_token: ${{ secrets.GITHUB_TOKEN }}
337-
env:
338-
CPLN_TOKEN: ${{ secrets.CPLN_TOKEN }}
339-
340-
- name: Update Delete Status
341-
if: always()
342-
uses: actions/github-script@v7
343-
with:
344-
script: |
345-
const success = '${{ job.status }}' === 'success';
346-
const prNumber = process.env.PR_NUMBER;
347-
const cpConsoleUrl = `https://console.cpln.io/org/${process.env.CPLN_ORG}/workloads/${process.env.APP_NAME}`;
348-
349-
const message = success
350-
? ' Review app for PR #' + prNumber + ' was successfully deleted'
351-
: [
352-
' Review app for PR #' + prNumber + ' failed to be deleted',
353-
'',
354-
' [Control Plane Console for Review App with PR #' + prNumber + '](' + cpConsoleUrl + ')'
355-
].join('\n');
356-
357-
await github.rest.issues.updateComment({
358-
owner: context.repo.owner,
359-
repo: context.repo.repo,
360-
comment_id: ${{ fromJSON(steps.init-delete.outputs.result).commentId }},
361-
body: message
362136
});

0 commit comments

Comments
 (0)