From 788e600d152cc96a529758d6cbd03d328fbcc4f5 Mon Sep 17 00:00:00 2001 From: Justin Gordon Date: Tue, 28 Jan 2025 16:26:28 -1000 Subject: [PATCH] separate to two jobs and staging --- .controlplane/controlplane.yml | 2 +- ...=> deploy-to-control-plane-review-app.yml} | 123 ++++++++---------- .../deploy-to-control-plane-staging.yml | 84 ++++++++++-- 3 files changed, 131 insertions(+), 78 deletions(-) rename .github/workflows/{deploy-to-control-plane.yml => deploy-to-control-plane-review-app.yml} (89%) diff --git a/.controlplane/controlplane.yml b/.controlplane/controlplane.yml index f394fe1c..357b54fe 100644 --- a/.controlplane/controlplane.yml +++ b/.controlplane/controlplane.yml @@ -55,7 +55,7 @@ apps: # QA Apps are like Heroku review apps, but the use `prefix` so you can run a commmand like # this to create a QA app for the tutorial app. # `cpflow setup gvc postgres redis rails -a qa-react-webpack-rails-tutorial-pr-1234` - qa-react-webpack-rails-tutorial: + qa-react-webpack-rails-tutorial-pr: <<: *common # Order matters! setup_app_templates: diff --git a/.github/workflows/deploy-to-control-plane.yml b/.github/workflows/deploy-to-control-plane-review-app.yml similarity index 89% rename from .github/workflows/deploy-to-control-plane.yml rename to .github/workflows/deploy-to-control-plane-review-app.yml index d8a9783f..a22433e2 100644 --- a/.github/workflows/deploy-to-control-plane.yml +++ b/.github/workflows/deploy-to-control-plane-review-app.yml @@ -33,10 +33,10 @@ jobs: debug: uses: ./.github/workflows/debug-workflow.yml with: - debug_enabled: false # Will still run if vars.DEBUG_WORKFLOW is true - - Process-Deployment-Command: - needs: debug # Add this to ensure debug runs first + debug_enabled: false + + process-deployment: + needs: debug if: | (github.event_name == 'pull_request') || (github.event_name == 'push') || @@ -45,12 +45,13 @@ jobs: github.event.issue.pull_request && contains(github.event.comment.body, '/deploy-review-app')) runs-on: ubuntu-latest - permissions: - contents: read - deployments: write - pull-requests: write - issues: write - + outputs: + pr_number: ${{ env.PR_NUMBER }} + pr_sha: ${{ env.PR_SHA }} + pr_ref: ${{ steps.getRef.outputs.PR_REF }} + do_deploy: ${{ env.DO_DEPLOY }} + comment_id: ${{ steps.create-comment.outputs.comment-id }} + deployment_id: ${{ steps.init-deployment.outputs.result }} steps: # Initial checkout only for pull_request and push events - name: Checkout code @@ -157,13 +158,6 @@ jobs: echo "PR_REF=$(echo $PR_DATA | jq -r .headRefName)" >> $GITHUB_OUTPUT echo "PR_SHA=$(echo $PR_DATA | jq -r .headRefOid)" >> $GITHUB_ENV - - name: Checkout PR code - if: github.event_name == 'workflow_dispatch' || github.event_name == 'issue_comment' - uses: actions/checkout@v4 - with: - fetch-depth: 0 - ref: ${{ steps.getRef.outputs.PR_SHA }} - - name: Setup Environment uses: ./.github/actions/setup-environment with: @@ -253,26 +247,6 @@ jobs: }); core.setOutput('comment-id', result.data.id); - - name: Update Comment - Building - if: env.DO_DEPLOY != 'false' - uses: actions/github-script@v7 - with: - script: | - const buildingMessage = [ - `🏗️ Building Docker image for PR #${process.env.PR_NUMBER}, commit ${process.env.PR_SHA}`, - '', - `📝 [View Build Logs](${process.env.WORKFLOW_URL})`, - '', - process.env.CONSOLE_LINK - ].join('\n'); - - await github.rest.issues.updateComment({ - owner: context.repo.owner, - repo: context.repo.repo, - comment_id: ${{ steps.create-comment.outputs.comment-id }}, - body: buildingMessage - }); - - name: Set Deployment URLs id: set-urls if: env.DO_DEPLOY != 'false' @@ -305,30 +279,6 @@ jobs: 'https://console.cpln.io/console/org/' + process.env.CPLN_ORG + '/gvc/' + process.env.APP_NAME + '/-info)' ); - - name: Update Status - Building - if: env.DO_DEPLOY != 'false' - uses: actions/github-script@v7 - with: - script: | - const buildingMessage = [ - '🏗️ Building Docker image for PR #' + process.env.PR_NUMBER + ', commit ' + '${{ env.PR_SHA }}', - '', - '📝 [View Build Logs](' + process.env.WORKFLOW_URL + ')', - '', - process.env.CONSOLE_LINK - ].join('\n'); - - await github.rest.issues.updateComment({ - owner: context.repo.owner, - repo: context.repo.repo, - comment_id: ${{ steps.create-comment.outputs.comment-id }}, - body: buildingMessage - }); - - - name: Checkout PR Branch - if: env.DO_DEPLOY != 'false' - run: git checkout ${{ steps.getRef.outputs.PR_REF }} - - name: Initialize GitHub Deployment if: env.DO_DEPLOY != 'false' uses: actions/github-script@v7 @@ -359,17 +309,58 @@ jobs: return deployment.data.id; + build: + needs: process-deployment + if: needs.process-deployment.outputs.do_deploy != 'false' + runs-on: ubuntu-latest + outputs: + image_tag: ${{ steps.build.outputs.image_tag }} + comment_id: ${{ needs.process-deployment.outputs.comment_id }} + pr_number: ${{ needs.process-deployment.outputs.pr_number }} + do_deploy: ${{ needs.process-deployment.outputs.do_deploy }} + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + ref: ${{ needs.process-deployment.outputs.pr_ref }} + + - name: Update Status - Building + uses: actions/github-script@v7 + with: + script: | + const buildingMessage = [ + '🏗️ Building Docker image for PR #${{ needs.process-deployment.outputs.pr_number }}, commit ${{ needs.process-deployment.outputs.pr_sha }}', + '', + '📝 [View Build Logs](${{ env.WORKFLOW_URL }})', + '', + process.env.CONSOLE_LINK + ].join('\n'); + + await github.rest.issues.updateComment({ + owner: context.repo.owner, + repo: context.repo.repo, + comment_id: ${{ needs.process-deployment.outputs.comment_id }}, + body: buildingMessage + }); + - name: Build Docker Image - if: env.DO_DEPLOY != 'false' + id: build uses: ./.github/actions/build-docker-image with: app_name: ${{ env.APP_NAME }} org: ${{ vars.CPLN_ORG_STAGING }} - commit: ${{ env.PR_SHA }} - PR_NUMBER: ${{ env.PR_NUMBER }} + commit: ${{ needs.process-deployment.outputs.pr_sha }} + PR_NUMBER: ${{ needs.process-deployment.outputs.pr_number }} + + deploy: + needs: build + if: needs.build.outputs.do_deploy != 'false' + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 - name: Update Status - Deploying - if: env.DO_DEPLOY != 'false' uses: actions/github-script@v7 with: script: | @@ -378,7 +369,7 @@ jobs: '', '⏳ Waiting for deployment to be ready...', '', - '📝 [View Deploy Logs](' + process.env.WORKFLOW_URL + ')', + '📝 [View Deploy Logs](${{ env.WORKFLOW_URL }})', '', process.env.CONSOLE_LINK ].join('\n'); diff --git a/.github/workflows/deploy-to-control-plane-staging.yml b/.github/workflows/deploy-to-control-plane-staging.yml index 898c447c..1a49bad8 100644 --- a/.github/workflows/deploy-to-control-plane-staging.yml +++ b/.github/workflows/deploy-to-control-plane-staging.yml @@ -1,32 +1,94 @@ # Control Plane GitHub Action -name: Deploy Main Branch to Control Plane Staging +name: Deploy to Control Plane Staging +run-name: Deploy Control Plane Staging App # Controls when the workflow will run on: - # Uncomment the lines you want actions that will cause the workflow to Triggers the workflow on push or pull request events but only for the main branch push: - branches: [master] - - # Allows you to run this workflow manually from the Actions tab + branches: + - 'main' + - 'master' + - ${{ github.vars.STAGING_APP_BRANCH }} workflow_dispatch: # Convert the GitHub secret variables to environment variables for use by the Control Plane CLI env: - CPLN_ORG: ${{ vars.CPLN_ORG_STAGING }} + APP_NAME: ${{ vars.STAGING_APP_NAME }} CPLN_TOKEN: ${{ secrets.CPLN_TOKEN_STAGING }} + CPLN_ORG: ${{ vars.CPLN_ORG_STAGING }} + STAGING_APP_BRANCH: ${{ vars.STAGING_APP_BRANCH }} + +concurrency: + group: deploy-staging + cancel-in-progress: true jobs: - deploy-to-control-plane-staging: + debug: + uses: ./.github/workflows/debug-workflow.yml + with: + debug_enabled: false + + validate-branch: runs-on: ubuntu-latest + steps: + - name: Check if allowed branch + run: | + if [[ -n "${STAGING_APP_BRANCH}" ]]; then + if [[ "${GITHUB_REF#refs/heads/}" != "${STAGING_APP_BRANCH}" ]]; then + echo "This workflow only runs on configured branch: ${STAGING_APP_BRANCH}" + echo "Current branch: ${GITHUB_REF#refs/heads/}" + exit 1 + fi + elif [[ "${GITHUB_REF}" != "refs/heads/main" && "${GITHUB_REF}" != "refs/heads/master" ]]; then + echo "This workflow only runs on main or master branch (no STAGING_APP_BRANCH configured)" + echo "Current branch: ${GITHUB_REF#refs/heads/}" + exit 1 + fi + build: + needs: validate-branch + runs-on: ubuntu-latest + outputs: + image_tag: ${{ steps.build.outputs.image_tag }} steps: - - uses: actions/checkout@v4 + - name: Checkout code + uses: actions/checkout@v4 with: - fetch-depth: 0 # Fetch all history for proper SHA handling - ref: master # Explicitly checkout master branch + fetch-depth: 0 + + - name: Setup Environment + uses: ./.github/actions/setup-environment + with: + token: ${{ secrets.CPLN_TOKEN_STAGING }} + org: ${{ vars.CPLN_ORG_STAGING }} + + - name: Build Docker Image + id: build + uses: ./.github/actions/build-docker-image + with: + app_name: ${{ env.APP_NAME }} + org: ${{ vars.CPLN_ORG_STAGING }} + commit: ${{ github.sha }} + + deploy: + needs: build + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup Environment + uses: ./.github/actions/setup-environment + with: + token: ${{ secrets.CPLN_TOKEN_STAGING }} + org: ${{ vars.CPLN_ORG_STAGING }} - - uses: ./.github/actions/deploy-to-control-plane + - name: Deploy to Control Plane + uses: ./.github/actions/deploy-to-control-plane with: app_name: ${{ vars.STAGING_APP_NAME }} org: ${{ vars.CPLN_ORG_STAGING }} + github_token: ${{ secrets.GITHUB_TOKEN }} + wait_timeout: ${{ vars.WAIT_TIMEOUT || 900 }} + cpln_token: ${{ secrets.CPLN_TOKEN_STAGING }}