diff --git a/.github/actions/deploy-to-control-plane/action.yml b/.github/actions/deploy-to-control-plane/action.yml deleted file mode 100644 index fcb684396..000000000 --- a/.github/actions/deploy-to-control-plane/action.yml +++ /dev/null @@ -1,47 +0,0 @@ -# Control Plane GitHub Action - -name: Deploy to Control Plane -description: 'Deploys an application to Control Plane' - -inputs: - app_name: - description: 'Name of the application' - required: true - org: - description: 'Organization name' - required: true - github_token: - description: 'GitHub token' - required: true - wait_timeout: - description: 'Timeout in seconds for waiting for workloads to be ready' - required: false - default: '900' - cpln_token: - description: 'Control Plane token' - required: true - pr_number: - description: 'Pull Request number' - required: true - -outputs: - review_app_url: - description: 'URL of the deployed application' - value: ${{ steps.deploy.outputs.review_app_url }} - -runs: - using: "composite" - steps: - - name: Deploy to Control Plane - id: deploy - shell: bash - env: - APP_NAME: ${{ inputs.app_name }} - CPLN_ORG: ${{ inputs.org }} - CPLN_TOKEN: ${{ inputs.cpln_token }} - WAIT_TIMEOUT: ${{ inputs.wait_timeout }} - run: | - # Run the deployment script - if ! ${{ github.action_path }}/scripts/deploy.sh; then - exit 1 - fi diff --git a/.github/actions/deploy-to-control-plane/scripts/deploy.sh b/.github/actions/deploy-to-control-plane/scripts/deploy.sh deleted file mode 100755 index f90ede239..000000000 --- a/.github/actions/deploy-to-control-plane/scripts/deploy.sh +++ /dev/null @@ -1,64 +0,0 @@ -#!/bin/bash - -# This script handles the deployment to Control Plane and extracts the Rails URL -# -# Required environment variables: -# - APP_NAME: Name of the application to deploy -# - CPLN_ORG: Control Plane organization -# -# Optional environment variables: -# - WAIT_TIMEOUT: Timeout in seconds for deployment (default: 900) -# Must be a positive integer -# -# Outputs: -# - ENV APP_URL: URL of the deployed application - -set -e - -# Validate required environment variables -: "${APP_NAME:?APP_NAME environment variable is required}" -: "${CPLN_ORG:?CPLN_ORG environment variable is required}" - -# Set and validate deployment timeout -WAIT_TIMEOUT=${WAIT_TIMEOUT:-900} -if ! [[ "${WAIT_TIMEOUT}" =~ ^[0-9]+$ ]]; then - echo "❌ Invalid timeout value: ${WAIT_TIMEOUT}" - exit 1 -fi - -TEMP_OUTPUT=$(mktemp) -trap 'rm -f "$TEMP_OUTPUT"' EXIT - -# Deploy the application -echo "🚀 Deploying to Control Plane (timeout: ${WAIT_TIMEOUT}s)" -if ! timeout "${WAIT_TIMEOUT}" cpflow deploy-image -a "$APP_NAME" --run-release-phase --org "$CPLN_ORG" --verbose 2>&1 | tee "$TEMP_OUTPUT"; then - echo "❌ Deployment failed" - echo "Full output:" - cat "$TEMP_OUTPUT" - exit 1 -fi - -# Extract app URL from deployment output -APP_URL=$(grep -oP 'https://[^[:space:]]*\.cpln\.app(?=\s|$)' "$TEMP_OUTPUT" | head -n1) -if [ -z "$APP_URL" ]; then - echo "❌ Error: Could not find app URL in deployment output" - exit 1 -fi - -# Wait for all workloads to be ready -echo "⏳ Waiting for all workloads to be ready (timeout: ${WAIT_TIMEOUT}s)" -if ! timeout "${WAIT_TIMEOUT}" bash -c "cpflow ps:wait -a \"$APP_NAME\"" 2>&1 | tee -a "$TEMP_OUTPUT"; then - TIMEOUT_EXIT=$? - if [ ${TIMEOUT_EXIT} -eq 124 ]; then - echo "❌ Timed out waiting for workloads after ${WAIT_TIMEOUT} seconds" - else - echo "❌ Workloads did not become ready" - fi - echo "Full output:" - cat "$TEMP_OUTPUT" - exit 1 -fi - -echo "✅ Deployment successful" -echo "🌐 App URL: $APP_URL" -echo "APP_URL=$APP_URL" >> "$GITHUB_OUTPUT" diff --git a/.github/actions/setup-environment/action.yml b/.github/actions/setup-environment/action.yml index 710b6c71e..e9212d6e0 100644 --- a/.github/actions/setup-environment/action.yml +++ b/.github/actions/setup-environment/action.yml @@ -24,7 +24,7 @@ runs: run: | sudo npm install -g @controlplane/cli@3.3.1 cpln --version - gem install cpflow -v 4.1.0 + gem install cpflow -v 4.1.1 cpflow --version - name: Setup Control Plane Profile diff --git a/.github/actions/validate-required-vars/action.yml b/.github/actions/validate-required-vars/action.yml deleted file mode 100644 index c370039d0..000000000 --- a/.github/actions/validate-required-vars/action.yml +++ /dev/null @@ -1,28 +0,0 @@ -name: 'Validate Required Variables' -description: 'Validates that all required secrets and variables for Control Plane operations' - -runs: - using: 'composite' - steps: - - name: Validate Required Secrets and Variables - shell: bash - run: | - missing=() - - # Check required secrets - if [ -z "$CPLN_TOKEN_STAGING" ]; then - missing+=("Secret: CPLN_TOKEN_STAGING") - fi - - # Check required variables - if [ -z "$CPLN_ORG_STAGING" ]; then - missing+=("Variable: CPLN_ORG_STAGING") - fi - if [ -z "$REVIEW_APP_PREFIX" ]; then - missing+=("Variable: REVIEW_APP_PREFIX") - fi - - if [ ${#missing[@]} -ne 0 ]; then - echo "Required secrets/variables are not set: ${missing[*]}" - exit 1 - fi diff --git a/.github/workflows/delete-review-app.yml b/.github/workflows/delete-review-app.yml index a0b3611a8..3f1451bc5 100644 --- a/.github/workflows/delete-review-app.yml +++ b/.github/workflows/delete-review-app.yml @@ -19,6 +19,7 @@ permissions: issues: write 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 }} @@ -43,7 +44,28 @@ jobs: - uses: actions/checkout@v4 - name: Validate Required Secrets and Variables - uses: ./.github/actions/validate-required-vars + shell: bash + run: | + missing=() + + # Check required secrets + if [ -z "$CPLN_TOKEN" ]; then + missing+=("Secret: CPLN_TOKEN_STAGING") + fi + + # Check required variables + if [ -z "$CPLN_ORG" ]; then + missing+=("Variable: CPLN_ORG_STAGING") + fi + + if [ -z "$"PREFIX" }} ]; then + missing+=("Variable: REVIEW_APP_PREFIX") + fi + + if [ ${#missing[@]} -ne 0 ]; then + echo "Required secrets/variables are not set: ${missing[*]}" + exit 1 + fi - name: Setup Environment uses: ./.github/actions/setup-environment diff --git a/.github/workflows/deploy-to-control-plane-review-app.yml b/.github/workflows/deploy-to-control-plane-review-app.yml index ea05f98b0..74a4468b9 100644 --- a/.github/workflows/deploy-to-control-plane-review-app.yml +++ b/.github/workflows/deploy-to-control-plane-review-app.yml @@ -2,14 +2,12 @@ name: Deploy PR Review App to Control Plane run-name: Deploy PR Review App - PR #${{ github.event.pull_request.number || github.event.issue.number || github.event.inputs.pr_number }} + on: - pull_request: - types: [opened, synchronize, reopened] push: - branches: - - '**' # Any branch - - '!main' # Except main - - '!master' # Except master + branches: [ "master" ] + pull_request: + branches: [ "master" ] issue_comment: types: [created] workflow_dispatch: @@ -24,6 +22,7 @@ concurrency: cancel-in-progress: true 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 }} CPLN_TOKEN: ${{ secrets.CPLN_TOKEN_STAGING }} CPLN_ORG: ${{ vars.CPLN_ORG_STAGING }} @@ -35,8 +34,7 @@ jobs: with: debug_enabled: false - process-deployment: - needs: debug + deploy: if: | (github.event_name == 'pull_request') || (github.event_name == 'push') || @@ -45,13 +43,6 @@ jobs: github.event.issue.pull_request && contains(github.event.comment.body, '/deploy-review-app')) runs-on: ubuntu-latest - 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 @@ -70,7 +61,28 @@ jobs: fetch-depth: 0 - name: Validate Required Secrets and Variables - uses: ./.github/actions/validate-required-vars + shell: bash + run: | + missing=() + + # Check required secrets + if [ -z "$CPLN_TOKEN" ]; then + missing+=("Secret: CPLN_TOKEN_STAGING") + fi + + # Check required variables + if [ -z "$CPLN_ORG" ]; then + missing+=("Variable: CPLN_ORG_STAGING") + fi + + if [ -z "$PREFIX" ]; then + missing+=("Variable: REVIEW_APP_PREFIX") + fi + + if [ ${#missing[@]} -ne 0 ]; then + echo "Required secrets/variables are not set: ${missing[*]}" + exit 1 + fi - name: Get PR HEAD Ref id: getRef @@ -288,33 +300,12 @@ 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: Setup Environment - uses: ./.github/actions/setup-environment - with: - token: ${{ secrets.CPLN_TOKEN_STAGING }} - org: ${{ vars.CPLN_ORG_STAGING }} - - 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 }}', + '🏗️ Building Docker image for PR #${{ env.PR_NUMBER }}, commit ${{ env.PR_SHA }}', '', '📝 [View Build Logs](${{ env.WORKFLOW_URL }})', '', @@ -324,7 +315,7 @@ jobs: await github.rest.issues.updateComment({ owner: context.repo.owner, repo: context.repo.repo, - comment_id: ${{ needs.process-deployment.outputs.comment_id }}, + comment_id: ${{ steps.create-comment.outputs.comment-id }}, body: buildingMessage }); @@ -334,22 +325,8 @@ jobs: with: app_name: ${{ env.APP_NAME }} org: ${{ vars.CPLN_ORG_STAGING }} - 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: Setup Environment - uses: ./.github/actions/setup-environment - with: - token: ${{ secrets.CPLN_TOKEN_STAGING }} - org: ${{ vars.CPLN_ORG_STAGING }} + commit: ${{ env.PR_SHA }} + PR_NUMBER: ${{ env.PR_NUMBER }} - name: Update Status - Deploying uses: actions/github-script@v7 @@ -374,14 +351,11 @@ jobs: - name: Deploy to Control Plane if: env.DO_DEPLOY != 'false' - uses: ./.github/actions/deploy-to-control-plane - with: - app_name: ${{ env.APP_NAME }} - org: ${{ vars.CPLN_ORG_STAGING }} - github_token: ${{ secrets.GITHUB_TOKEN }} - wait_timeout: ${{ vars.WAIT_TIMEOUT || 900 }} - cpln_token: ${{ secrets.CPLN_TOKEN_STAGING }} - pr_number: ${{ env.PR_NUMBER }} + run: cpflow deploy-image -a ${{ env.APP_NAME }} --run-release-phase --org ${{ vars.CPLN_ORG_STAGING }} --verbose + + - name: Retrieve App URL + id: workload + 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' @@ -389,7 +363,7 @@ jobs: with: script: | const prNumber = process.env.PR_NUMBER; - const appUrl = process.env.APP_URL; + const appUrl = '${{ steps.workload.outputs.WORKLOAD_URL }}'; const workflowUrl = process.env.WORKFLOW_URL; const isSuccess = '${{ job.status }}' === 'success';