Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/actions/deploy-to-control-plane/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ runs:
GITHUB_TOKEN: ${{ inputs.github_token }}
PR_NUMBER: ${{ env.PR_NUMBER }}

- name: Setup Control Plane App If It Doesn't Exist
shell: bash
run: |
if ! cpflow exists -a ${{ inputs.app_name }} ; then
echo " Setting up new Control Plane app for app ${{ inputs.app_name }}..."
cpflow setup-app -a ${{ inputs.app_name }}
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

Add error handling for the setup-app command.

The setup command should include error handling to ensure the app is created successfully.

 if ! cpflow exists -a ${{ inputs.app_name }} ; then
   echo " Setting up new Control Plane app for app ${{ inputs.app_name }}..."
-  cpflow setup-app -a ${{ inputs.app_name }}
+  if ! cpflow setup-app -a ${{ inputs.app_name }}; then
+    echo "❌ Failed to set up Control Plane app"
+    exit 1
+  fi
 fi
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
- name: Setup Control Plane App If It Doesn't Exist
shell: bash
run: |
if ! cpflow exists -a ${{ inputs.app_name }} ; then
echo " Setting up new Control Plane app for app ${{ inputs.app_name }}..."
cpflow setup-app -a ${{ inputs.app_name }}
fi
- name: Setup Control Plane App If It Doesn't Exist
shell: bash
run: |
if ! cpflow exists -a ${{ inputs.app_name }} ; then
echo " Setting up new Control Plane app for app ${{ inputs.app_name }}..."
if ! cpflow setup-app -a ${{ inputs.app_name }}; then
echo "❌ Failed to set up Control Plane app"
exit 1
fi
fi


- name: Deploy to Control Plane
id: deploy
shell: bash
Expand Down
48 changes: 30 additions & 18 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 Down Expand Up @@ -31,21 +31,33 @@ 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 | 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
elif [ $? -eq 124 ]; then
echo "❌ Deployment timed out after $WAIT_TIMEOUT seconds"
exit 1
else
echo "❌ Deployment to Control Plane failed"
exit 1
if ! timeout "$WAIT_TIMEOUT" cpflow deploy-image -a "$APP_NAME" --run-release-phase --org "$CPLN_ORG" --verbose | tee "$TEMP_OUTPUT"; then
echo "❌ Deployment failed"
echo "Error output:"
exit 1
fi

# Extract app URL from deployment output
RAILS_URL=$(grep -oP 'https://rails-[^[:space:]]*\.cpln\.app(?=\s|$)' "$TEMP_OUTPUT" | head -n1)
if [ -z "$RAILS_URL" ]; then
echo "❌ Failed to get app URL from 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 "🌐 Rails URL: $RAILS_URL"
echo "rails_url=$RAILS_URL" >> "$GITHUB_OUTPUT"
36 changes: 18 additions & 18 deletions .github/workflows/add-comment-on-pr-creation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ jobs:
permissions:
pull-requests: write
steps:
name: Add GitHub Comment for review app instructions
uses: actions/github-script@v7
with:
script: |
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
body: [
"Hi 👋 Here are the commands available for this PR:",
"",
"- `/deploy-review-app`: Deploy your changes to a review environment",
"- `/delete-review-app`: Clean up the review environment when you're done",
"- `/help`: Show detailed information about all commands",
"",
"Use `/help` to see full documentation, including configuration options."
].join("\n")
});
- uses: actions/github-script@v7
name: Add GitHub Comment for review app instructions
with:
script: |
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
body: [
"Hi 👋 Here are the commands available for this PR:",
"",
"- `/deploy-review-app`: Deploy your changes to a review environment",
"- `/delete-review-app`: Clean up the review environment when you're done",
"- `/help`: Show detailed information about all commands",
"",
"Use `/help` to see full documentation, including configuration options."
].join("\n")
});
44 changes: 16 additions & 28 deletions .github/workflows/delete-review-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,11 @@ jobs:
}
`);

- name: Initialize Delete
id: init-delete
- name: Initialize and Set Workflow URL
id: init-workflow
uses: actions/github-script@v7
with:
script: |
eval(process.env.GET_CONSOLE_LINK);

async function getWorkflowUrl(runId) {
// Get the current job ID
const jobs = await github.rest.actions.listJobsForWorkflowRun({
Expand All @@ -87,34 +85,16 @@ jobs:
}

const workflowUrl = await getWorkflowUrl(context.runId);

const comment = await github.rest.issues.createComment({
issue_number: process.env.PR_NUMBER,
owner: context.repo.owner,
repo: context.repo.repo,
body: [
' Starting app deletion...',
'',
' [View Delete Logs](' + workflowUrl + ')',
'',
getConsoleLink(process.env.PR_NUMBER)
].join('\n')
});

return {
commentId: comment.data.id,
workflowUrl
};

- name: Set workflow URL
run: |
echo "WORKFLOW_URL=${{ fromJSON(steps.init-delete.outputs.result).workflowUrl }}" >> $GITHUB_ENV
core.exportVariable('WORKFLOW_URL', workflowUrl);
return { workflowUrl };

- name: Create Initial Delete Comment
id: init-delete
uses: actions/github-script@v7
with:
script: |
eval(process.env.GET_CONSOLE_LINK);

let message = '🗑️ Starting app deletion';
if ('${{ github.event_name }}' === 'pull_request') {
const merged = '${{ github.event.pull_request.merged }}' === 'true';
Expand All @@ -125,7 +105,13 @@ jobs:
issue_number: process.env.PR_NUMBER,
owner: context.repo.owner,
repo: context.repo.repo,
body: message
body: [
message,
'',
' [View Delete Logs](' + process.env.WORKFLOW_URL + ')',
'',
getConsoleLink(process.env.PR_NUMBER)
].join('\n')
});
return { commentId: comment.data.id };

Expand All @@ -151,7 +137,9 @@ jobs:
const successMessage = [
'✅ Review app for PR #' + prNumber + ' was successfully deleted',
'',
' [View Completed Delete Logs](' + process.env.WORKFLOW_URL + ')'
' [View Completed Delete Logs](' + process.env.WORKFLOW_URL + ')',
'',
' [Control Plane Organization](https://console.cpln.io/console/org/' + process.env.CPLN_ORG + '/-info)'
].join('\n');

const failureMessage = [
Expand Down
57 changes: 6 additions & 51 deletions .github/workflows/deploy-to-control-plane.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,26 @@ name: Deploy Review App to Control Plane
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) }}

on:
pull_request:
types: [opened, synchronize, reopened]
issue_comment:
types: [created]

# Use concurrency to cancel in-progress runs
concurrency:
group: deploy-${{ github.event.pull_request.number || github.event.issue.number }}
group: deploy-${{ github.event.issue.number }}
cancel-in-progress: true

env:
APP_NAME: qa-react-webpack-rails-tutorial-pr-${{ github.event.pull_request.number || github.event.issue.number }}
APP_NAME: qa-react-webpack-rails-tutorial-pr-${{ github.event.issue.number }}
CPLN_ORG: ${{ secrets.CPLN_ORG }}
CPLN_TOKEN: ${{ secrets.CPLN_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number || github.event.issue.number }}
PR_NUMBER: ${{ github.event.issue.number }}

jobs:
Process-Deployment-Command:
if: |
(github.event_name == 'pull_request') ||
(github.event_name == 'issue_comment' &&
github.event.issue.pull_request &&
github.event.comment.body == '/deploy-review-app')
github.event_name == 'issue_comment' &&
github.event.issue.pull_request &&
github.event.comment.body == '/deploy-review-app'
runs-on: ubuntu-latest
permissions:
contents: read
Expand Down Expand Up @@ -251,45 +248,3 @@ jobs:
comment_id: process.env.COMMENT_ID,
body: isSuccess ? successMessage : failureMessage
});

show-help:
if: |
github.event_name == 'issue_comment' &&
github.event.issue.pull_request &&
github.event.comment.body == '/help'
runs-on: ubuntu-latest

steps:
- name: Show Available Commands
uses: actions/github-script@v7
with:
script: |
const helpMessage = [
'## Available Commands',
'',
'### `/deploy-review-app`',
'Deploys your PR branch to a review environment on Control Plane.',
'- Creates a new review app if one doesn\'t exist',
'- Updates the existing review app if it already exists',
'- Provides a unique URL to preview your changes',
'- Shows build and deployment progress in real-time',
'',
'### `/delete-review-app`',
'Deletes the review app associated with this PR.',
'- Removes all resources from Control Plane',
'- Helpful for cleaning up when you\'re done testing',
'- Can be re-deployed later using `/deploy-review-app`',
'',
'### `/help`',
'Shows this help message explaining available commands.',
'',
'---',
'_Note: These commands only work in pull request comments._'
].join('\n');

await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.issue.number,
body: helpMessage
});
4 changes: 2 additions & 2 deletions .github/workflows/help-command.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
const helpMessage = [
'## 📚 Available Commands',
'',
'### `/deploy`',
'### `/deploy-review-app`',
'Deploys your PR branch to a review environment on Control Plane.',
'- Creates a new review app if one doesn\'t exist',
'- Updates the existing review app if it already exists',
Expand All @@ -52,7 +52,7 @@ jobs:
'Deletes the review app associated with this PR.',
'- Removes all resources from Control Plane',
'- Helpful for cleaning up when you\'re done testing',
'- Can be re-deployed later using `/deploy`',
'- Can be re-deployed later using `/deploy-review-app`',
'',
'**Required Environment Variables:**',
'- `CPLN_TOKEN`: Control Plane authentication token',
Expand Down
Loading