Skip to content

Commit 2a51c92

Browse files
committed
fixes
1 parent 5c8b3ab commit 2a51c92

File tree

5 files changed

+158
-16
lines changed

5 files changed

+158
-16
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+
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 }}/../deploy-to-control-plane/scripts/delete-app.sh
18+
env:
19+
APP_NAME: ${{ inputs.app_name }}
20+
CPLN_ORG: ${{ inputs.org }}

.github/actions/deploy-to-control-plane/action.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,15 @@ runs:
2828
- name: Get Commit SHA
2929
id: get_sha
3030
shell: bash
31-
run: |
32-
chmod +x ${{ github.action_path }}/scripts/get-commit-sha.sh
33-
${{ github.action_path }}/scripts/get-commit-sha.sh
31+
run: ${{ github.action_path }}/scripts/get-commit-sha.sh
3432
env:
3533
GITHUB_TOKEN: ${{ inputs.github_token }}
3634
PR_NUMBER: ${{ env.PR_NUMBER }}
3735

3836
- name: Deploy to Control Plane
3937
id: deploy
4038
shell: bash
41-
run: |
42-
chmod +x ${{ github.action_path }}/scripts/deploy.sh
43-
${{ github.action_path }}/scripts/deploy.sh
39+
run: ${{ github.action_path }}/scripts/deploy.sh
4440
env:
4541
APP_NAME: ${{ inputs.app_name }}
4642
CPLN_ORG: ${{ inputs.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"; 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" --force; 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/deploy-to-control-plane.yml

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: Deploy to Control Plane
22

3-
run-name: "${{ github.event_name == 'pull_request' || (github.event_name == 'issue_comment' && github.event.issue.pull_request) ? 'Deploying Review App' : format('Deploying {0} to Staging App', github.ref_name) }}"
3+
run-name: ${{ (github.event_name == 'pull_request' || (github.event_name == 'issue_comment' && github.event.issue.pull_request)) && 'Deploying Review App' || format('Deploying {0} to Staging App', github.ref_name) }}
44

55
on:
66
pull_request:
@@ -20,11 +20,12 @@ env:
2020
PR_NUMBER: ${{ github.event.pull_request.number || github.event.issue.number }}
2121

2222
jobs:
23-
deploy:
23+
process-command:
2424
if: |
2525
github.event_name == 'pull_request' ||
2626
(github.event_name == 'issue_comment' &&
27-
github.event.comment.body == '/deploy-review-app' &&
27+
(github.event.comment.body == '/deploy-review-app' ||
28+
github.event.comment.body == '/delete-app') &&
2829
github.event.issue.pull_request)
2930
runs-on: ubuntu-latest
3031
permissions:
@@ -34,8 +35,67 @@ jobs:
3435
issues: write
3536

3637
steps:
38+
- name: Determine Action
39+
id: determine_action
40+
run: |
41+
if [[ "${{ github.event.comment.body }}" == "/delete-app" ]]; then
42+
echo "action=delete" >> $GITHUB_OUTPUT
43+
else
44+
echo "action=deploy" >> $GITHUB_OUTPUT
45+
fi
46+
47+
- uses: actions/checkout@v4
48+
with:
49+
fetch-depth: 0
50+
ref: ${{ steps.getRef.outputs.PR_REF || github.ref }}
51+
52+
- name: Setup Environment
53+
uses: ./.github/actions/setup-environment
54+
55+
# Delete App Steps
56+
- name: Create Initial Delete Comment
57+
if: steps.determine_action.outputs.action == 'delete'
58+
uses: actions/github-script@v7
59+
id: init-delete
60+
with:
61+
script: |
62+
const comment = await github.rest.issues.createComment({
63+
issue_number: process.env.PR_NUMBER,
64+
owner: context.repo.owner,
65+
repo: context.repo.repo,
66+
body: '🗑️ Starting app deletion...'
67+
});
68+
return { commentId: comment.data.id };
69+
70+
- name: Delete App
71+
if: steps.determine_action.outputs.action == 'delete'
72+
id: delete
73+
uses: ./.github/actions/delete-control-plane-app
74+
continue-on-error: true
75+
with:
76+
app_name: ${{ env.APP_NAME }}
77+
org: ${{ env.CPLN_ORG }}
78+
79+
- name: Update Delete Status
80+
if: steps.determine_action.outputs.action == 'delete'
81+
uses: actions/github-script@v7
82+
with:
83+
script: |
84+
const success = '${{ steps.delete.outcome }}' === 'success';
85+
const message = success
86+
? '✅ App deletion successful'
87+
: '❌ App deletion failed';
88+
89+
await github.rest.issues.updateComment({
90+
owner: context.repo.owner,
91+
repo: context.repo.repo,
92+
comment_id: ${{ fromJSON(steps.init-delete.outputs.result).commentId }},
93+
body: message
94+
});
95+
96+
# Deploy Steps (existing steps)
3797
- name: Get PR HEAD Ref
38-
if: ${{ github.event_name == 'issue_comment' }}
98+
if: steps.determine_action.outputs.action == 'deploy'
3999
id: getRef
40100
run: |
41101
echo "PR_REF=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json headRefName | jq -r '.headRefName')" >> $GITHUB_OUTPUT
@@ -47,9 +107,6 @@ jobs:
47107
fetch-depth: 0
48108
ref: ${{ steps.getRef.outputs.PR_REF || github.ref }}
49109

50-
- name: Setup Environment
51-
uses: ./.github/actions/setup-environment
52-
53110
- name: Initialize Deployment
54111
id: init-deployment
55112
uses: actions/github-script@v7
@@ -82,7 +139,7 @@ jobs:
82139
run_id: context.runId
83140
});
84141
85-
const jobId = jobs.data.jobs.find(job => job.name === 'deploy')?.id;
142+
const jobId = jobs.data.jobs.find(job => job.name === 'process-command')?.id;
86143
const jobUrl = getJobUrl(context.runId, jobId, prNumber);
87144
88145
// Create initial comment

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

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,42 @@ jobs:
2121
- name: Setup Environment
2222
uses: ./.github/actions/setup-environment
2323

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

0 commit comments

Comments
 (0)