Skip to content

Commit 9ed72f0

Browse files
authored
Fix Review App Deletion Workflows (#639)
1 parent 9894731 commit 9ed72f0

File tree

5 files changed

+66
-60
lines changed

5 files changed

+66
-60
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Delete Control Plane App
2+
description: 'Deletes a Control Plane application and all its resources'
3+
4+
inputs:
5+
app_name:
6+
description: 'Name of the application to delete'
7+
required: true
8+
cpln_org:
9+
description: 'Organization name'
10+
required: true
11+
12+
runs:
13+
using: "composite"
14+
steps:
15+
- name: Delete Application
16+
shell: bash
17+
run: ${{ github.action_path }}/delete-app.sh
18+
env:
19+
APP_NAME: ${{ inputs.app_name }}
20+
CPLN_ORG: ${{ inputs.cpln_org }}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/bash
2+
3+
# Script to delete a Control Plane application
4+
# Required environment variables:
5+
# - APP_NAME: Name of the application to delete
6+
# - CPLN_ORG: Organization name
7+
8+
set -e
9+
10+
# Validate required environment variables
11+
: "${APP_NAME:?APP_NAME environment variable is required}"
12+
: "${CPLN_ORG:?CPLN_ORG environment variable is required}"
13+
14+
# Safety check: prevent deletion of production or staging apps
15+
if echo "$APP_NAME" | grep -iqE '(production|staging)'; then
16+
echo "❌ ERROR: Cannot delete apps containing 'production' or 'staging' in their name" >&2
17+
echo "🛑 This is a safety measure to prevent accidental deletion of production or staging environments" >&2
18+
echo " App name: $APP_NAME" >&2
19+
exit 1
20+
fi
21+
22+
# Check if app exists before attempting to delete
23+
echo "🔍 Checking if application exists: $APP_NAME"
24+
if ! cpflow exists -a "$APP_NAME" --org "$CPLN_ORG"; then
25+
echo "⚠️ Application does not exist: $APP_NAME"
26+
exit 0
27+
fi
28+
29+
# Delete the application
30+
echo "🗑️ Deleting application: $APP_NAME"
31+
if ! cpflow delete -a "$APP_NAME" --org "$CPLN_ORG" --yes; then
32+
echo "❌ Failed to delete application: $APP_NAME" >&2
33+
exit 1
34+
fi
35+
36+
echo "✅ Successfully deleted application: $APP_NAME"

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

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ env:
2222
PREFIX: ${{ vars.REVIEW_APP_PREFIX }}
2323
CPLN_ORG: ${{ vars.CPLN_ORG_STAGING }}
2424
CPLN_TOKEN: ${{ secrets.CPLN_TOKEN_STAGING }}
25-
APP_NAME: ${{ vars.REVIEW_APP_PREFIX }}-pr-${{ github.event.pull_request.number || github.event.issue.number || inputs.pr_number }}
25+
APP_NAME: ${{ vars.REVIEW_APP_PREFIX }}-${{ github.event.pull_request.number || github.event.issue.number || inputs.pr_number }}
2626
PR_NUMBER: ${{ github.event.pull_request.number || github.event.issue.number || inputs.pr_number }}
2727

2828
jobs:
@@ -58,7 +58,7 @@ jobs:
5858
missing+=("Variable: CPLN_ORG_STAGING")
5959
fi
6060
61-
if [ -z "$"PREFIX" }} ]; then
61+
if [ -z "$PREFIX" ]; then
6262
missing+=("Variable: REVIEW_APP_PREFIX")
6363
fi
6464
@@ -131,29 +131,16 @@ jobs:
131131
owner: context.repo.owner,
132132
repo: context.repo.repo,
133133
body: '🗑️ Starting app deletion...'
134-
body: [
135-
message,
136-
'',
137-
' 🗑️ [View Delete Logs](' + process.env.WORKFLOW_URL + ')',
138-
'',
139-
getConsoleLink(process.env.PR_NUMBER)
140-
].join('\n')
141134
});
142135
return { commentId: comment.data.id };
143136
144137
- name: Delete Review App
145138
uses: ./.github/actions/delete-control-plane-app
146139
with:
147140
app_name: ${{ env.APP_NAME }}
148-
org: ${{ env.CPLN_ORG }}
149-
github_token: ${{ secrets.GITHUB_TOKEN }}
150-
env:
151-
APP_NAME: ${{ env.APP_NAME }}
152-
CPLN_ORG: ${{ secrets.CPLN_ORG }}
153-
CPLN_TOKEN: ${{ secrets.CPLN_TOKEN }}
141+
cpln_org: ${{ vars.CPLN_ORG_STAGING }}
154142

155143
- name: Update Delete Status
156-
if: always()
157144
uses: actions/github-script@v7
158145
with:
159146
script: |

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ concurrency:
2121

2222
env:
2323
PREFIX: ${{ vars.REVIEW_APP_PREFIX }}
24-
APP_NAME: ${{ vars.REVIEW_APP_PREFIX }}-pr-${{ github.event.pull_request.number || github.event.issue.number || github.event.inputs.pr_number }}
24+
APP_NAME: ${{ vars.REVIEW_APP_PREFIX }}-${{ github.event.pull_request.number || github.event.issue.number || github.event.inputs.pr_number }}
2525
CPLN_TOKEN: ${{ secrets.CPLN_TOKEN_STAGING }}
2626
CPLN_ORG: ${{ vars.CPLN_ORG_STAGING }}
2727
PR_NUMBER: ${{ github.event.pull_request.number || github.event.issue.number || github.event.inputs.pr_number }}
@@ -181,17 +181,19 @@ jobs:
181181
echo "Skipping deployment for non-PR comment"
182182
fi
183183
fi
184+
if [[ "${{ env.DO_DEPLOY }}" == "false" ]]; then
185+
exit 0
186+
fi
184187
185188
- name: Setup Control Plane App if Not Existing
186-
if: env.DO_DEPLOY == 'true' && env.APP_EXISTS == 'false'
189+
if: env.APP_EXISTS == 'false'
187190
env:
188191
CPLN_TOKEN: ${{ secrets.CPLN_TOKEN_STAGING }}
189192
run: |
190193
echo "🔧 Setting up new Control Plane app..."
191194
cpflow setup-app -a ${{ env.APP_NAME }} --org ${{ vars.CPLN_ORG_STAGING }}
192195
193196
- name: Create Initial Comment
194-
if: env.DO_DEPLOY != 'false'
195197
uses: actions/github-script@v7
196198
id: create-comment
197199
with:
@@ -200,13 +202,12 @@ jobs:
200202
owner: context.repo.owner,
201203
repo: context.repo.repo,
202204
issue_number: process.env.PR_NUMBER,
203-
body: '🚀 Starting deployment process...\n\n' + process.env.CONSOLE_LINK
205+
body: '🚀 Starting deployment process...\n\n'
204206
});
205207
core.setOutput('comment-id', result.data.id);
206208
207209
- name: Set Deployment URLs
208210
id: set-urls
209-
if: env.DO_DEPLOY != 'false'
210211
uses: actions/github-script@v7
211212
with:
212213
script: |
@@ -237,7 +238,6 @@ jobs:
237238
);
238239
239240
- name: Initialize GitHub Deployment
240-
if: env.DO_DEPLOY != 'false'
241241
uses: actions/github-script@v7
242242
id: init-deployment
243243
with:
@@ -316,15 +316,13 @@ jobs:
316316
});
317317
318318
- name: Deploy to Control Plane
319-
if: env.DO_DEPLOY != 'false'
320319
run: cpflow deploy-image -a ${{ env.APP_NAME }} --run-release-phase --org ${{ vars.CPLN_ORG_STAGING }} --verbose
321320

322321
- name: Retrieve App URL
323322
id: workload
324323
run: echo "WORKLOAD_URL=$(cpln workload get rails --gvc ${{ env.APP_NAME }} | tee | grep -oP 'https://[^[:space:]]*\.cpln\.app(?=\s|$)' | head -n1)" >> "$GITHUB_OUTPUT"
325324

326325
- name: Update Status - Deployment Complete
327-
if: env.DO_DEPLOY != 'false'
328326
uses: actions/github-script@v7
329327
with:
330328
script: |

.github/workflows/nightly-remove-stale-review-apps.yml

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,40 +20,5 @@ jobs:
2020
token: ${{ secrets.CPLN_TOKEN_STAGING }}
2121
org: ${{ vars.CPLN_ORG_STAGING }}
2222

23-
- name: Get Stale PRs
24-
id: stale_prs
25-
uses: actions/github-script@v7
26-
with:
27-
script: |
28-
const thirtyDaysAgo = new Date();
29-
thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30);
30-
31-
const prs = await github.rest.pulls.list({
32-
owner: context.repo.owner,
33-
repo: context.repo.repo,
34-
state: 'closed',
35-
sort: 'updated',
36-
direction: 'desc'
37-
});
38-
39-
const stalePRs = prs.data
40-
.filter(pr => new Date(pr.updated_at) < thirtyDaysAgo)
41-
.map(pr => pr.number);
42-
43-
console.log('Found stale PRs:', stalePRs);
44-
return stalePRs;
45-
4623
- name: Delete Stale Review Apps
47-
if: ${{ steps.stale_prs.outputs.result != '[]' }}
48-
run: |
49-
for pr in $(echo "${{ steps.stale_prs.outputs.result }}" | jq -r '.[]'); do
50-
APP_NAME="qa-react-webpack-rails-tutorial-pr-$pr"
51-
echo "🗑️ Deleting stale review app for PR #$pr: $APP_NAME"
52-
${{ github.workspace }}/.github/actions/deploy-to-control-plane/scripts/delete-app.sh
53-
done
54-
env:
55-
APP_NAME: qa-react-webpack-rails-tutorial-pr-${{ steps.stale_prs.outputs.result }}
56-
57-
- name: Run cleanup-images script
58-
run: |
59-
cpflow cleanup-images -a qa-react-webpack-rails-tutorial -y
24+
run: cpflow cleanup-stale-apps -a "qa-react-webpack-rails-tutorial" --yes

0 commit comments

Comments
 (0)