From 409365f1cbd9c8039698aec166c7eeb8c5e49d4e Mon Sep 17 00:00:00 2001 From: Judah Meek Date: Wed, 23 Apr 2025 21:27:20 -0500 Subject: [PATCH 01/10] fix --- .github/workflows/delete-review-app.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/delete-review-app.yml b/.github/workflows/delete-review-app.yml index 3f1451bc5..036498d9e 100644 --- a/.github/workflows/delete-review-app.yml +++ b/.github/workflows/delete-review-app.yml @@ -58,7 +58,7 @@ jobs: missing+=("Variable: CPLN_ORG_STAGING") fi - if [ -z "$"PREFIX" }} ]; then + if [ -z "$PREFIX" }} ]; then missing+=("Variable: REVIEW_APP_PREFIX") fi From f70de8062810f5e13c56d0eb0ffbf6ee071e08c7 Mon Sep 17 00:00:00 2001 From: Judah Meek Date: Wed, 23 Apr 2025 21:42:24 -0500 Subject: [PATCH 02/10] restore script --- .../scripts/delete-app.sh | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100755 .github/actions/deploy-to-control-plane/scripts/delete-app.sh diff --git a/.github/actions/deploy-to-control-plane/scripts/delete-app.sh b/.github/actions/deploy-to-control-plane/scripts/delete-app.sh new file mode 100755 index 000000000..92e8fbc32 --- /dev/null +++ b/.github/actions/deploy-to-control-plane/scripts/delete-app.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +# Script to delete a Control Plane application +# Required environment variables: +# - APP_NAME: Name of the application to delete +# - CPLN_ORG: Organization name + +set -e + +# Validate required environment variables +: "${APP_NAME:?APP_NAME environment variable is required}" +: "${CPLN_ORG:?CPLN_ORG environment variable is required}" + +# Safety check: prevent deletion of production or staging apps +if echo "$APP_NAME" | grep -iqE '(production|staging)'; then + echo "❌ ERROR: Cannot delete apps containing 'production' or 'staging' in their name" >&2 + echo "🛑 This is a safety measure to prevent accidental deletion of production or staging environments" >&2 + echo " App name: $APP_NAME" >&2 + exit 1 +fi + +# Check if app exists before attempting to delete +echo "🔍 Checking if application exists: $APP_NAME" +if ! cpflow exists -a "$APP_NAME"; then + echo "⚠️ Application does not exist: $APP_NAME" + exit 0 +fi + +# Delete the application +echo "🗑️ Deleting application: $APP_NAME" +if ! cpflow delete -a "$APP_NAME" --force; then + echo "❌ Failed to delete application: $APP_NAME" >&2 + exit 1 +fi + +echo "✅ Successfully deleted application: $APP_NAME" From 3352e90f2a17201a11c8b9c5465290582b3abb9b Mon Sep 17 00:00:00 2001 From: Judah Meek Date: Sat, 26 Apr 2025 20:27:23 -0500 Subject: [PATCH 03/10] recreate app deletion script --- .../delete-control-plane-app/action.yml | 20 +++++++++++++++++++ .../delete-app.sh | 4 ++-- .github/workflows/delete-review-app.yml | 7 +------ .../nightly-remove-stale-review-apps.yml | 9 ++------- 4 files changed, 25 insertions(+), 15 deletions(-) create mode 100644 .github/actions/delete-control-plane-app/action.yml rename .github/actions/{deploy-to-control-plane/scripts => delete-control-plane-app}/delete-app.sh (89%) diff --git a/.github/actions/delete-control-plane-app/action.yml b/.github/actions/delete-control-plane-app/action.yml new file mode 100644 index 000000000..caaef2729 --- /dev/null +++ b/.github/actions/delete-control-plane-app/action.yml @@ -0,0 +1,20 @@ +name: Delete Control Plane App +description: 'Deletes a Control Plane application and all its resources' + +inputs: + app_name: + description: 'Name of the application to delete' + required: true + cpln_org: + description: 'Organization name' + required: true + +runs: + using: "composite" + steps: + - name: Delete Application + shell: bash + run: ${{ github.action_path }}/delete-app.sh + env: + APP_NAME: ${{ inputs.app_name }} + CPLN_ORG: ${{ inputs.cpln_org }} diff --git a/.github/actions/deploy-to-control-plane/scripts/delete-app.sh b/.github/actions/delete-control-plane-app/delete-app.sh similarity index 89% rename from .github/actions/deploy-to-control-plane/scripts/delete-app.sh rename to .github/actions/delete-control-plane-app/delete-app.sh index 92e8fbc32..11b423ee0 100755 --- a/.github/actions/deploy-to-control-plane/scripts/delete-app.sh +++ b/.github/actions/delete-control-plane-app/delete-app.sh @@ -21,14 +21,14 @@ fi # Check if app exists before attempting to delete echo "🔍 Checking if application exists: $APP_NAME" -if ! cpflow exists -a "$APP_NAME"; then +if ! cpflow exists -a "$APP_NAME" --org "$CPLN_ORG"; then echo "⚠️ Application does not exist: $APP_NAME" exit 0 fi # Delete the application echo "🗑️ Deleting application: $APP_NAME" -if ! cpflow delete -a "$APP_NAME" --force; then +if ! cpflow delete -a "$APP_NAME" --org "$CPLN_ORG" --force; then echo "❌ Failed to delete application: $APP_NAME" >&2 exit 1 fi diff --git a/.github/workflows/delete-review-app.yml b/.github/workflows/delete-review-app.yml index 036498d9e..b50c6653c 100644 --- a/.github/workflows/delete-review-app.yml +++ b/.github/workflows/delete-review-app.yml @@ -145,12 +145,7 @@ jobs: uses: ./.github/actions/delete-control-plane-app with: app_name: ${{ env.APP_NAME }} - org: ${{ env.CPLN_ORG }} - github_token: ${{ secrets.GITHUB_TOKEN }} - env: - APP_NAME: ${{ env.APP_NAME }} - CPLN_ORG: ${{ secrets.CPLN_ORG }} - CPLN_TOKEN: ${{ secrets.CPLN_TOKEN }} + org: ${{ vars.CPLN_ORG_STAGING }} - name: Update Delete Status if: always() diff --git a/.github/workflows/nightly-remove-stale-review-apps.yml b/.github/workflows/nightly-remove-stale-review-apps.yml index e511d9ace..a9a231997 100644 --- a/.github/workflows/nightly-remove-stale-review-apps.yml +++ b/.github/workflows/nightly-remove-stale-review-apps.yml @@ -48,12 +48,7 @@ jobs: run: | for pr in $(echo "${{ steps.stale_prs.outputs.result }}" | jq -r '.[]'); do APP_NAME="qa-react-webpack-rails-tutorial-pr-$pr" + CPLN_ORG=${{ vars.CPLN_ORG_STAGING }} echo "🗑️ Deleting stale review app for PR #$pr: $APP_NAME" - ${{ github.workspace }}/.github/actions/deploy-to-control-plane/scripts/delete-app.sh + ${{ github.workspace }}/.github/actions/delete-control-plane-app/scripts/delete-app.sh done - env: - APP_NAME: qa-react-webpack-rails-tutorial-pr-${{ steps.stale_prs.outputs.result }} - - - name: Run cleanup-images script - run: | - cpflow cleanup-images -a qa-react-webpack-rails-tutorial -y From 3100c846ed2d9947694de9662bd4c25bcc65b102 Mon Sep 17 00:00:00 2001 From: Judah Meek Date: Mon, 28 Apr 2025 13:36:12 -0500 Subject: [PATCH 04/10] fixes --- .github/workflows/delete-review-app.yml | 4 ++-- .github/workflows/nightly-remove-stale-review-apps.yml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/delete-review-app.yml b/.github/workflows/delete-review-app.yml index b50c6653c..93e323b4f 100644 --- a/.github/workflows/delete-review-app.yml +++ b/.github/workflows/delete-review-app.yml @@ -58,7 +58,7 @@ jobs: missing+=("Variable: CPLN_ORG_STAGING") fi - if [ -z "$PREFIX" }} ]; then + if [ -z "$PREFIX" ]; then missing+=("Variable: REVIEW_APP_PREFIX") fi @@ -145,7 +145,7 @@ jobs: uses: ./.github/actions/delete-control-plane-app with: app_name: ${{ env.APP_NAME }} - org: ${{ vars.CPLN_ORG_STAGING }} + cpln_org: ${{ vars.CPLN_ORG_STAGING }} - name: Update Delete Status if: always() diff --git a/.github/workflows/nightly-remove-stale-review-apps.yml b/.github/workflows/nightly-remove-stale-review-apps.yml index a9a231997..1a849bcc9 100644 --- a/.github/workflows/nightly-remove-stale-review-apps.yml +++ b/.github/workflows/nightly-remove-stale-review-apps.yml @@ -50,5 +50,5 @@ jobs: APP_NAME="qa-react-webpack-rails-tutorial-pr-$pr" CPLN_ORG=${{ vars.CPLN_ORG_STAGING }} echo "🗑️ Deleting stale review app for PR #$pr: $APP_NAME" - ${{ github.workspace }}/.github/actions/delete-control-plane-app/scripts/delete-app.sh + ${{ github.workspace }}/.github/actions/delete-control-plane-app/delete-app.sh done From 3b8ab7634d82ecdec0760c5b290df55eaefe8556 Mon Sep 17 00:00:00 2001 From: Judah Meek Date: Tue, 29 Apr 2025 14:33:15 -0500 Subject: [PATCH 05/10] fixes --- .github/actions/delete-control-plane-app/delete-app.sh | 6 ++++-- .github/workflows/delete-review-app.yml | 8 -------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/.github/actions/delete-control-plane-app/delete-app.sh b/.github/actions/delete-control-plane-app/delete-app.sh index 11b423ee0..6024abf21 100755 --- a/.github/actions/delete-control-plane-app/delete-app.sh +++ b/.github/actions/delete-control-plane-app/delete-app.sh @@ -7,9 +7,11 @@ set -e +printenv + # Validate required environment variables -: "${APP_NAME:?APP_NAME environment variable is required}" -: "${CPLN_ORG:?CPLN_ORG environment variable is required}" +: "${$APP_NAME:?APP_NAME environment variable is required}" +: "${$CPLN_ORG:?CPLN_ORG environment variable is required}" # Safety check: prevent deletion of production or staging apps if echo "$APP_NAME" | grep -iqE '(production|staging)'; then diff --git a/.github/workflows/delete-review-app.yml b/.github/workflows/delete-review-app.yml index 93e323b4f..ca2fea888 100644 --- a/.github/workflows/delete-review-app.yml +++ b/.github/workflows/delete-review-app.yml @@ -131,13 +131,6 @@ jobs: owner: context.repo.owner, repo: context.repo.repo, body: '🗑️ Starting app deletion...' - body: [ - message, - '', - ' 🗑️ [View Delete Logs](' + process.env.WORKFLOW_URL + ')', - '', - getConsoleLink(process.env.PR_NUMBER) - ].join('\n') }); return { commentId: comment.data.id }; @@ -148,7 +141,6 @@ jobs: cpln_org: ${{ vars.CPLN_ORG_STAGING }} - name: Update Delete Status - if: always() uses: actions/github-script@v7 with: script: | From 3d45555d8798a0a26d13a0bbad67c8a8d77d9132 Mon Sep 17 00:00:00 2001 From: Judah Meek Date: Tue, 29 Apr 2025 14:49:22 -0500 Subject: [PATCH 06/10] fixes --- .../actions/delete-control-plane-app/delete-app.sh | 6 ++---- .../workflows/deploy-to-control-plane-review-app.yml | 12 +++++------- .../workflows/nightly-remove-stale-review-apps.yml | 2 +- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/.github/actions/delete-control-plane-app/delete-app.sh b/.github/actions/delete-control-plane-app/delete-app.sh index 6024abf21..11b423ee0 100755 --- a/.github/actions/delete-control-plane-app/delete-app.sh +++ b/.github/actions/delete-control-plane-app/delete-app.sh @@ -7,11 +7,9 @@ set -e -printenv - # Validate required environment variables -: "${$APP_NAME:?APP_NAME environment variable is required}" -: "${$CPLN_ORG:?CPLN_ORG environment variable is required}" +: "${APP_NAME:?APP_NAME environment variable is required}" +: "${CPLN_ORG:?CPLN_ORG environment variable is required}" # Safety check: prevent deletion of production or staging apps if echo "$APP_NAME" | grep -iqE '(production|staging)'; then diff --git a/.github/workflows/deploy-to-control-plane-review-app.yml b/.github/workflows/deploy-to-control-plane-review-app.yml index b04a1ed2c..c28605a38 100644 --- a/.github/workflows/deploy-to-control-plane-review-app.yml +++ b/.github/workflows/deploy-to-control-plane-review-app.yml @@ -181,9 +181,12 @@ jobs: echo "Skipping deployment for non-PR comment" fi fi + if [[ "${{ env.DO_DEPLOY }}" == "false" ]]; then + exit 0 + fi - name: Setup Control Plane App if Not Existing - if: env.DO_DEPLOY == 'true' && env.APP_EXISTS == 'false' + if: env.APP_EXISTS == 'false' env: CPLN_TOKEN: ${{ secrets.CPLN_TOKEN_STAGING }} run: | @@ -191,7 +194,6 @@ jobs: cpflow setup-app -a ${{ env.APP_NAME }} --org ${{ vars.CPLN_ORG_STAGING }} - name: Create Initial Comment - if: env.DO_DEPLOY != 'false' uses: actions/github-script@v7 id: create-comment with: @@ -200,13 +202,12 @@ jobs: owner: context.repo.owner, repo: context.repo.repo, issue_number: process.env.PR_NUMBER, - body: '🚀 Starting deployment process...\n\n' + process.env.CONSOLE_LINK + body: '🚀 Starting deployment process...\n\n' }); core.setOutput('comment-id', result.data.id); - name: Set Deployment URLs id: set-urls - if: env.DO_DEPLOY != 'false' uses: actions/github-script@v7 with: script: | @@ -237,7 +238,6 @@ jobs: ); - name: Initialize GitHub Deployment - if: env.DO_DEPLOY != 'false' uses: actions/github-script@v7 id: init-deployment with: @@ -316,7 +316,6 @@ jobs: }); - name: Deploy to Control Plane - if: env.DO_DEPLOY != 'false' run: cpflow deploy-image -a ${{ env.APP_NAME }} --run-release-phase --org ${{ vars.CPLN_ORG_STAGING }} --verbose - name: Retrieve App URL @@ -324,7 +323,6 @@ jobs: run: echo "WORKLOAD_URL=$(cpln workload get rails --gvc ${{ env.APP_NAME }} | tee | grep -oP 'https://[^[:space:]]*\.cpln\.app(?=\s|$)' | head -n1)" >> "$GITHUB_OUTPUT" - name: Update Status - Deployment Complete - if: env.DO_DEPLOY != 'false' uses: actions/github-script@v7 with: script: | diff --git a/.github/workflows/nightly-remove-stale-review-apps.yml b/.github/workflows/nightly-remove-stale-review-apps.yml index 1a849bcc9..47bff3645 100644 --- a/.github/workflows/nightly-remove-stale-review-apps.yml +++ b/.github/workflows/nightly-remove-stale-review-apps.yml @@ -50,5 +50,5 @@ jobs: APP_NAME="qa-react-webpack-rails-tutorial-pr-$pr" CPLN_ORG=${{ vars.CPLN_ORG_STAGING }} echo "🗑️ Deleting stale review app for PR #$pr: $APP_NAME" - ${{ github.workspace }}/.github/actions/delete-control-plane-app/delete-app.sh + APP_NAME=$APP_NAME CPLN_ORG=$CPLN_ORG ${{ github.workspace }}/.github/actions/delete-control-plane-app/delete-app.sh done From 8487edc01105488a7f40ecb587e9d395064d7488 Mon Sep 17 00:00:00 2001 From: Judah Meek Date: Tue, 29 Apr 2025 15:17:55 -0500 Subject: [PATCH 07/10] fixes --- .../delete-control-plane-app/delete-app.sh | 2 +- .../nightly-remove-stale-review-apps.yml | 32 +------------------ 2 files changed, 2 insertions(+), 32 deletions(-) diff --git a/.github/actions/delete-control-plane-app/delete-app.sh b/.github/actions/delete-control-plane-app/delete-app.sh index 11b423ee0..ea253e3d6 100755 --- a/.github/actions/delete-control-plane-app/delete-app.sh +++ b/.github/actions/delete-control-plane-app/delete-app.sh @@ -28,7 +28,7 @@ fi # Delete the application echo "🗑️ Deleting application: $APP_NAME" -if ! cpflow delete -a "$APP_NAME" --org "$CPLN_ORG" --force; then +if ! cpflow delete -a "$APP_NAME" --org "$CPLN_ORG"; then echo "❌ Failed to delete application: $APP_NAME" >&2 exit 1 fi diff --git a/.github/workflows/nightly-remove-stale-review-apps.yml b/.github/workflows/nightly-remove-stale-review-apps.yml index 47bff3645..bb68c3ae4 100644 --- a/.github/workflows/nightly-remove-stale-review-apps.yml +++ b/.github/workflows/nightly-remove-stale-review-apps.yml @@ -20,35 +20,5 @@ jobs: token: ${{ secrets.CPLN_TOKEN_STAGING }} org: ${{ vars.CPLN_ORG_STAGING }} - - name: Get Stale PRs - id: stale_prs - uses: actions/github-script@v7 - with: - script: | - const thirtyDaysAgo = new Date(); - thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30); - - const prs = await github.rest.pulls.list({ - owner: context.repo.owner, - repo: context.repo.repo, - state: 'closed', - sort: 'updated', - direction: 'desc' - }); - - const stalePRs = prs.data - .filter(pr => new Date(pr.updated_at) < thirtyDaysAgo) - .map(pr => pr.number); - - console.log('Found stale PRs:', stalePRs); - return stalePRs; - - name: Delete Stale Review Apps - if: ${{ steps.stale_prs.outputs.result != '[]' }} - run: | - for pr in $(echo "${{ steps.stale_prs.outputs.result }}" | jq -r '.[]'); do - APP_NAME="qa-react-webpack-rails-tutorial-pr-$pr" - CPLN_ORG=${{ vars.CPLN_ORG_STAGING }} - echo "🗑️ Deleting stale review app for PR #$pr: $APP_NAME" - APP_NAME=$APP_NAME CPLN_ORG=$CPLN_ORG ${{ github.workspace }}/.github/actions/delete-control-plane-app/delete-app.sh - done + run: cpflow cleanup-stale-apps --yes From 7de811b67b2773bccaeea8db101f6b189335c6b6 Mon Sep 17 00:00:00 2001 From: Judah Meek Date: Tue, 29 Apr 2025 15:34:14 -0500 Subject: [PATCH 08/10] app --- .github/workflows/nightly-remove-stale-review-apps.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/nightly-remove-stale-review-apps.yml b/.github/workflows/nightly-remove-stale-review-apps.yml index bb68c3ae4..d57c3e6e7 100644 --- a/.github/workflows/nightly-remove-stale-review-apps.yml +++ b/.github/workflows/nightly-remove-stale-review-apps.yml @@ -21,4 +21,4 @@ jobs: org: ${{ vars.CPLN_ORG_STAGING }} - name: Delete Stale Review Apps - run: cpflow cleanup-stale-apps --yes + run: cpflow cleanup-stale-apps -a "qa-react-webpack-rails-tutorial" --yes From 2ad243c9bead6bbb8014ac7bccda88a4685dc8a7 Mon Sep 17 00:00:00 2001 From: Judah Meek Date: Tue, 29 Apr 2025 15:53:20 -0500 Subject: [PATCH 09/10] fixes --- .github/workflows/delete-review-app.yml | 2 +- .github/workflows/deploy-to-control-plane-review-app.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/delete-review-app.yml b/.github/workflows/delete-review-app.yml index ca2fea888..ada1a5e71 100644 --- a/.github/workflows/delete-review-app.yml +++ b/.github/workflows/delete-review-app.yml @@ -22,7 +22,7 @@ env: PREFIX: ${{ vars.REVIEW_APP_PREFIX }} CPLN_ORG: ${{ vars.CPLN_ORG_STAGING }} CPLN_TOKEN: ${{ secrets.CPLN_TOKEN_STAGING }} - APP_NAME: ${{ vars.REVIEW_APP_PREFIX }}-pr-${{ github.event.pull_request.number || github.event.issue.number || inputs.pr_number }} + APP_NAME: ${{ vars.REVIEW_APP_PREFIX }}-${{ github.event.pull_request.number || github.event.issue.number || inputs.pr_number }} PR_NUMBER: ${{ github.event.pull_request.number || github.event.issue.number || inputs.pr_number }} jobs: diff --git a/.github/workflows/deploy-to-control-plane-review-app.yml b/.github/workflows/deploy-to-control-plane-review-app.yml index c28605a38..8388bfc1d 100644 --- a/.github/workflows/deploy-to-control-plane-review-app.yml +++ b/.github/workflows/deploy-to-control-plane-review-app.yml @@ -21,7 +21,7 @@ concurrency: env: PREFIX: ${{ vars.REVIEW_APP_PREFIX }} - APP_NAME: ${{ vars.REVIEW_APP_PREFIX }}-pr-${{ github.event.pull_request.number || github.event.issue.number || github.event.inputs.pr_number }} + APP_NAME: ${{ vars.REVIEW_APP_PREFIX }}-${{ github.event.pull_request.number || github.event.issue.number || github.event.inputs.pr_number }} CPLN_TOKEN: ${{ secrets.CPLN_TOKEN_STAGING }} CPLN_ORG: ${{ vars.CPLN_ORG_STAGING }} PR_NUMBER: ${{ github.event.pull_request.number || github.event.issue.number || github.event.inputs.pr_number }} From 744010807ae7c08b71e3741a77334ddefd85157d Mon Sep 17 00:00:00 2001 From: Judah Meek Date: Tue, 29 Apr 2025 16:39:02 -0500 Subject: [PATCH 10/10] fixes --- .github/actions/delete-control-plane-app/delete-app.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/actions/delete-control-plane-app/delete-app.sh b/.github/actions/delete-control-plane-app/delete-app.sh index ea253e3d6..6bc92bfcb 100755 --- a/.github/actions/delete-control-plane-app/delete-app.sh +++ b/.github/actions/delete-control-plane-app/delete-app.sh @@ -28,7 +28,7 @@ fi # Delete the application echo "🗑️ Deleting application: $APP_NAME" -if ! cpflow delete -a "$APP_NAME" --org "$CPLN_ORG"; then +if ! cpflow delete -a "$APP_NAME" --org "$CPLN_ORG" --yes; then echo "❌ Failed to delete application: $APP_NAME" >&2 exit 1 fi