Skip to content

Commit 3426aca

Browse files
committed
fix-for-delete
1 parent 66a4114 commit 3426aca

File tree

3 files changed

+145
-86
lines changed

3 files changed

+145
-86
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
name: Delete Review App
2+
3+
on:
4+
issue_comment:
5+
types: [created]
6+
7+
permissions:
8+
contents: read
9+
deployments: write
10+
pull-requests: write
11+
issues: write
12+
13+
env:
14+
CPLN_ORG: ${{ secrets.CPLN_ORG_STAGING }}
15+
CPLN_TOKEN: ${{ secrets.CPLN_TOKEN_STAGING }}
16+
17+
jobs:
18+
Process-Delete-Command:
19+
if: |
20+
github.event_name == 'issue_comment' &&
21+
github.event.issue.pull_request &&
22+
github.event.comment.body == '/delete-review-app'
23+
runs-on: ubuntu-latest
24+
25+
steps:
26+
- name: Get PR number
27+
id: pr
28+
uses: actions/github-script@v7
29+
with:
30+
script: |
31+
const prNumber = context.payload.issue.number;
32+
core.setOutput('pr_number', prNumber);
33+
core.exportVariable('PR_NUMBER', prNumber);
34+
35+
- name: Set App Name
36+
run: echo "APP_NAME=qa-react-webpack-rails-tutorial-pr-${{ env.PR_NUMBER }}" >> $GITHUB_ENV
37+
38+
- uses: actions/checkout@v4
39+
40+
- name: Setup Environment
41+
uses: ./.github/actions/setup-environment
42+
43+
- name: Create Initial Delete Comment
44+
id: init-delete
45+
uses: actions/github-script@v7
46+
with:
47+
script: |
48+
const comment = await github.rest.issues.createComment({
49+
issue_number: process.env.PR_NUMBER,
50+
owner: context.repo.owner,
51+
repo: context.repo.repo,
52+
body: '🗑️ Starting app deletion...'
53+
});
54+
return { commentId: comment.data.id };
55+
56+
- name: Delete Review App
57+
uses: ./.github/actions/delete-control-plane-app
58+
env:
59+
APP_NAME: ${{ env.APP_NAME }}
60+
CPLN_ORG: ${{ secrets.CPLN_ORG }}
61+
CPLN_TOKEN: ${{ secrets.CPLN_TOKEN }}
62+
63+
- name: Update Delete Status
64+
if: always()
65+
uses: actions/github-script@v7
66+
with:
67+
script: |
68+
const success = '${{ job.status }}' === 'success';
69+
const prNumber = process.env.PR_NUMBER;
70+
const cpConsoleUrl = `https://console.cpln.io/org/${process.env.CPLN_ORG}/workloads/${process.env.APP_NAME}`;
71+
72+
const message = success
73+
? '✅ Review app for PR #' + prNumber + ' was successfully deleted'
74+
: [
75+
'❌ Review app for PR #' + prNumber + ' failed to be deleted',
76+
'',
77+
'[View in Control Plane Console](' + cpConsoleUrl + ')'
78+
].join('\n');
79+
80+
await github.rest.issues.updateComment({
81+
owner: context.repo.owner,
82+
repo: context.repo.repo,
83+
comment_id: ${{ fromJSON(steps.init-delete.outputs.result).commentId }},
84+
body: message
85+
});

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

Lines changed: 8 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ env:
2222
jobs:
2323
Process-Deployment-Command:
2424
if: |
25-
github.event_name == 'pull_request' ||
25+
(github.event_name == 'pull_request') ||
2626
(github.event_name == 'issue_comment' &&
27-
(github.event.comment.body == '/deploy-review-app' ||
28-
github.event.comment.body == '/delete-review-app') &&
29-
github.event.issue.pull_request)
27+
github.event.issue.pull_request &&
28+
github.event.comment.body == '/deploy')
3029
runs-on: ubuntu-latest
3130
permissions:
3231
contents: read
@@ -46,72 +45,15 @@ jobs:
4645
env:
4746
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4847

49-
- name: Determine Action
50-
id: determine_action
51-
run: |
52-
if [[ "${{ github.event.comment.body }}" == "/delete-review-app" ]]; then
53-
echo "action=delete" >> $GITHUB_OUTPUT
54-
else
55-
echo "action=deploy" >> $GITHUB_OUTPUT
56-
fi
57-
5848
- uses: actions/checkout@v4
5949
with:
6050
fetch-depth: 0
61-
# For PR events: use the head SHA
62-
# For PR comments: use the PR head SHA
63-
# For other events: use the push SHA
64-
ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || steps.getRef.outputs.PR_SHA || github.sha }}
51+
ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.ref || steps.getRef.outputs.PR_REF || github.ref }}
6552

6653
- name: Setup Environment
6754
uses: ./.github/actions/setup-environment
6855

69-
# Delete App Steps
70-
- name: Create Initial Delete Comment
71-
if: steps.determine_action.outputs.action == 'delete'
72-
uses: actions/github-script@v7
73-
id: init-delete
74-
with:
75-
script: |
76-
const comment = await github.rest.issues.createComment({
77-
issue_number: process.env.PR_NUMBER,
78-
owner: context.repo.owner,
79-
repo: context.repo.repo,
80-
body: '🗑️ Starting app deletion...'
81-
});
82-
return { commentId: comment.data.id };
83-
84-
- name: Delete App
85-
if: steps.determine_action.outputs.action == 'delete'
86-
id: delete
87-
run: |
88-
echo "🗑️ Deleting review app: $APP_NAME"
89-
${{ github.workspace }}/.github/actions/deploy-to-control-plane/scripts/delete-app.sh
90-
91-
- name: Update Delete Status
92-
if: steps.determine_action.outputs.action == 'delete'
93-
uses: actions/github-script@v7
94-
with:
95-
script: |
96-
const success = '${{ steps.delete.outcome }}' === 'success';
97-
const message = success
98-
? [
99-
'✅ Review app successfully deleted',
100-
'',
101-
'⚡ [Control Plane Console](https://console.cpln.io/console/org/' + process.env.CPLN_ORG + '/gvc/' + process.env.APP_NAME + '/workload)'
102-
].join('\n')
103-
: '❌ Review app deletion failed';
104-
105-
await github.rest.issues.updateComment({
106-
owner: context.repo.owner,
107-
repo: context.repo.repo,
108-
comment_id: ${{ fromJSON(steps.init-delete.outputs.result).commentId }},
109-
body: message
110-
});
111-
112-
# Deploy Steps
11356
- name: Initialize Deployment
114-
if: steps.determine_action.outputs.action == 'deploy'
11557
id: init-deployment
11658
uses: actions/github-script@v7
11759
with:
@@ -162,19 +104,16 @@ jobs:
162104
};
163105
164106
- name: Set comment ID and workflow URL
165-
if: steps.determine_action.outputs.action == 'deploy'
166107
run: |
167108
echo "COMMENT_ID=${{ fromJSON(steps.init-deployment.outputs.result).commentId }}" >> $GITHUB_ENV
168109
echo "WORKFLOW_URL=${{ fromJSON(steps.init-deployment.outputs.result).workflowUrl }}" >> $GITHUB_ENV
169110
170111
- name: Set commit hash
171-
if: steps.determine_action.outputs.action == 'deploy'
172112
run: |
173113
FULL_COMMIT="${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || steps.getRef.outputs.PR_SHA || github.sha }}"
174114
echo "COMMIT_HASH=${FULL_COMMIT:0:7}" >> $GITHUB_ENV
175115
176116
- name: Update Status - Building
177-
if: steps.determine_action.outputs.action == 'deploy'
178117
uses: actions/github-script@v7
179118
with:
180119
script: |
@@ -192,7 +131,6 @@ jobs:
192131
});
193132
194133
- name: Build Docker Image
195-
if: steps.determine_action.outputs.action == 'deploy'
196134
uses: ./.github/actions/build-docker-image
197135
with:
198136
app_name: ${{ env.APP_NAME }}
@@ -201,7 +139,6 @@ jobs:
201139
PR_NUMBER: ${{ env.PR_NUMBER }}
202140

203141
- name: Update Status - Deploying
204-
if: steps.determine_action.outputs.action == 'deploy'
205142
uses: actions/github-script@v7
206143
with:
207144
script: |
@@ -221,28 +158,13 @@ jobs:
221158
});
222159
223160
- name: Deploy to Control Plane
224-
if: steps.determine_action.outputs.action == 'deploy'
225-
id: deploy
226161
uses: ./.github/actions/deploy-to-control-plane
227162
with:
228163
app_name: ${{ env.APP_NAME }}
229164
org: ${{ env.CPLN_ORG }}
230165
github_token: ${{ secrets.GITHUB_TOKEN }}
231166

232-
- name: Update Status - Success
233-
if: steps.determine_action.outputs.action == 'deploy' && success()
234-
uses: actions/github-script@v7
235-
with:
236-
script: |
237-
await github.rest.issues.updateComment({
238-
owner: context.repo.owner,
239-
repo: context.repo.repo,
240-
comment_id: process.env.COMMENT_ID,
241-
body: `🚀 Review App for PR #${process.env.PR_NUMBER}: [\`${process.env.REVIEW_APP_URL}\`](${process.env.REVIEW_APP_URL})`
242-
});
243-
244167
- name: Update Status - Deployment Complete
245-
if: steps.determine_action.outputs.action == 'deploy'
246168
uses: actions/github-script@v7
247169
with:
248170
script: |
@@ -268,15 +190,15 @@ jobs:
268190
const successMessage = [
269191
'✅ Deployment complete for PR #' + prNumber + ', commit ' + '${{ env.COMMIT_HASH }}',
270192
'',
271-
'🚀 Review App for PR #' + prNumber + ': [`' + appUrl + '`](' + appUrl + ')',
193+
'🚀 [Review App for PR #' + prNumber + '](' + appUrl + ')',
272194
'',
273195
'📋 [View Completed Action Build and Deploy Logs](' + workflowUrl + ')'
274196
].join('\n');
275197
276198
const failureMessage = [
277-
'❌ Deployment failed for PR #' + prNumber,
199+
'❌ Deployment failed for PR #' + prNumber + ', commit ' + '${{ env.COMMIT_HASH }}',
278200
'',
279-
'[View Workflow Status](' + workflowUrl + ')'
201+
'📋 [View Deployment Logs with Errors](' + workflowUrl + ')'
280202
].join('\n');
281203
282204
// Update the existing comment
@@ -285,4 +207,4 @@ jobs:
285207
repo: context.repo.repo,
286208
comment_id: process.env.COMMENT_ID,
287209
body: isSuccess ? successMessage : failureMessage
288-
});
210+
});

.github/workflows/help-command.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: Show Help for Commands
2+
3+
on:
4+
issue_comment:
5+
types: [created]
6+
7+
permissions:
8+
issues: write
9+
pull-requests: write
10+
11+
jobs:
12+
show-help:
13+
if: |
14+
github.event_name == 'issue_comment' &&
15+
github.event.issue.pull_request &&
16+
github.event.comment.body == '/help'
17+
runs-on: ubuntu-latest
18+
19+
steps:
20+
- name: Show Available Commands
21+
uses: actions/github-script@v7
22+
with:
23+
script: |
24+
const helpMessage = [
25+
'## 📚 Available Commands',
26+
'',
27+
'### `/deploy`',
28+
'Deploys your PR branch to a review environment on Control Plane.',
29+
'- Creates a new review app if one doesn\'t exist',
30+
'- Updates the existing review app if it already exists',
31+
'- Provides a unique URL to preview your changes',
32+
'- Shows build and deployment progress in real-time',
33+
'',
34+
'### `/delete-review-app`',
35+
'Deletes the review app associated with this PR.',
36+
'- Removes all resources from Control Plane',
37+
'- Helpful for cleaning up when you\'re done testing',
38+
'- Can be re-deployed later using `/deploy`',
39+
'',
40+
'### `/help`',
41+
'Shows this help message explaining available commands.',
42+
'',
43+
'---',
44+
'_Note: These commands only work in pull request comments._'
45+
].join('\n');
46+
47+
await github.rest.issues.createComment({
48+
owner: context.repo.owner,
49+
repo: context.repo.repo,
50+
issue_number: context.payload.issue.number,
51+
body: helpMessage
52+
});

0 commit comments

Comments
 (0)