Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Github Action / Control Plane Pipeline #626

Merged
merged 19 commits into from
Mar 19, 2025
Merged
7 changes: 0 additions & 7 deletions .github/actions/deploy-to-control-plane/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,10 @@ inputs:
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
Expand All @@ -38,7 +32,6 @@ runs:
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
Expand Down
54 changes: 22 additions & 32 deletions .github/actions/deploy-to-control-plane/scripts/deploy.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/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
Expand All @@ -11,7 +11,7 @@
# Must be a positive integer
#
# Outputs:
# - ENV APP_URL: URL of the deployed application
# - rails_url: URL of the deployed Rails application

set -e

Expand All @@ -29,36 +29,26 @@ fi
TEMP_OUTPUT=$(mktemp)
trap 'rm -f "$TEMP_OUTPUT"' EXIT

last_output=$(cpln workload get 2>&1)
echo "$last_output"

# 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
if timeout "$WAIT_TIMEOUT" cpflow deploy-image -a "$APP_NAME" --run-release-phase --org "$CPLN_ORG" --verbose | tee "$TEMP_OUTPUT"; then
# Extract Rails URL from deployment output
RAILS_URL=$(grep -oP 'https://rails-[^[:space:]]*\.cpln\.app(?=\s|$)' "$TEMP_OUTPUT" | head -n1)
if [ -n "$RAILS_URL" ]; then
echo "rails_url=$RAILS_URL" >> "$GITHUB_OUTPUT"
echo "✅ Deployment successful"
echo "🚀 Rails URL: $RAILS_URL"
else
echo "❌ Failed to extract Rails URL from deployment output"
exit 1
fi
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Ensure Reliable Deployment Command Execution

The deployment command is executed within a pipeline:

if timeout "$WAIT_TIMEOUT" cpflow deploy-image -a "$APP_NAME" --run-release-phase --org "$CPLN_ORG" --verbose | tee "$TEMP_OUTPUT"; then

Without pipefail enabled, the exit status of cpflow can be masked by the tee command—potentially causing the script to treat a failed or timed-out deployment as successful. With the addition of set -o pipefail (as suggested above), you will be better positioned to capture non-zero exit codes accurately.

elif [ $? -eq 124 ]; then
echo "❌ Deployment timed out after $WAIT_TIMEOUT seconds"
exit 1
else
echo "❌ Deployment to Control Plane failed"
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"
2 changes: 1 addition & 1 deletion .github/actions/setup-environment/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ runs:
run: |
sudo npm install -g @controlplane/[email protected]
cpln --version
gem install cpflow -v 4.1.0
gem install cpflow -v 4.1.1
cpflow --version

- name: Setup Control Plane Profile
Expand Down
28 changes: 0 additions & 28 deletions .github/actions/validate-required-vars/action.yml

This file was deleted.

24 changes: 23 additions & 1 deletion .github/workflows/delete-review-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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

Comment on lines 46 to 69
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Validation Script: Critical Syntax Issue in Variable Check.
In the validation step (line 61), the condition

if [ -z "$"PREFIX" }} ]; then

is syntactically incorrect. Please change it to:

if [ -z "$PREFIX" ]; then

to correctly validate if PREFIX is set.

🧰 Tools
🪛 YAMLlint (1.35.1)

[error] 50-50: trailing spaces

(trailing-spaces)


[error] 55-55: trailing spaces

(trailing-spaces)


[error] 64-64: trailing spaces

(trailing-spaces)


[error] 68-68: trailing spaces

(trailing-spaces)

- name: Setup Environment
uses: ./.github/actions/setup-environment
Expand Down
50 changes: 36 additions & 14 deletions .github/workflows/deploy-to-control-plane-review-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 }}
Expand Down Expand Up @@ -70,7 +69,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
Expand Down Expand Up @@ -158,6 +178,8 @@ jobs:
if ! cpflow exists -a ${{ env.APP_NAME }}; then
echo "APP_EXISTS=false" >> $GITHUB_ENV
else
last_output=$(cpln workload get 2>&1)
echo "$last_output"
echo "APP_EXISTS=true" >> $GITHUB_ENV
fi

Expand Down Expand Up @@ -294,9 +316,6 @@ jobs:
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
Expand Down Expand Up @@ -338,7 +357,7 @@ jobs:
PR_NUMBER: ${{ needs.process-deployment.outputs.pr_number }}

deploy:
needs: build
needs: [build, process-deployment]
if: needs.build.outputs.do_deploy != 'false'
runs-on: ubuntu-latest
steps:
Expand All @@ -350,6 +369,9 @@ jobs:
with:
token: ${{ secrets.CPLN_TOKEN_STAGING }}
org: ${{ vars.CPLN_ORG_STAGING }}

- name: Confirm Auth
run: cpln profile get

- name: Update Status - Deploying
uses: actions/github-script@v7
Expand All @@ -368,7 +390,7 @@ jobs:
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: ${{ steps.create-comment.outputs.comment-id }},
comment_id: ${{ needs.process-deployment.outputs.comment_id }},
body: deployingMessage
});

Expand Down Expand Up @@ -399,7 +421,7 @@ jobs:
const deploymentStatus = {
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: ${{ steps.init-deployment.outputs.result }},
deployment_id: ${{ needs.process-deployment.outputs.deployment_id }},
state: isSuccess ? 'success' : 'failure',
environment_url: isSuccess ? appUrl : undefined,
log_url: workflowUrl,
Expand Down Expand Up @@ -430,6 +452,6 @@ jobs:
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: ${{ steps.create-comment.outputs.comment-id }},
comment_id: ${{ needs.process-deployment.outputs.comment_id }},
body: isSuccess ? successMessage : failureMessage
});
Loading