Skip to content

Deploying Review App #65

Deploying Review App

Deploying Review App #65

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 }}
cancel-in-progress: true
env:
APP_NAME: qa-react-webpack-rails-tutorial-pr-${{ github.event.pull_request.number || 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 }}
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')
runs-on: ubuntu-latest
permissions:
contents: read
deployments: write
pull-requests: write
issues: write
steps:
- uses: actions/checkout@v4
- name: Get PR HEAD Ref
if: github.event_name == 'issue_comment'
id: getRef
run: |
PR_URL=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
"${{ github.event.issue.pull_request.url }}")
HEAD_REF=$(echo "$PR_URL" | jq -r .head.ref)
echo "ref=$HEAD_REF" >> "$GITHUB_OUTPUT"
- name: Validate Required Secrets
run: |
missing_secrets=()
for secret in "CPLN_TOKEN" "CPLN_ORG"; do
if [ -z "${!secret}" ]; then
missing_secrets+=("$secret")
fi
done
if [ ${#missing_secrets[@]} -ne 0 ]; then
echo "Required secrets are not set: ${missing_secrets[*]}"
exit 1
fi
- name: Setup Environment
uses: ./.github/actions/setup-environment
- name: Create Initial Status Comment
id: init-status
uses: actions/github-script@v7
with:
script: |
const comment = await github.rest.issues.createComment({
issue_number: process.env.PR_NUMBER,
owner: context.repo.owner,
repo: context.repo.repo,
body: '🚀 Starting deployment...'
});
return { commentId: comment.data.id };
- name: Update Status - Building
uses: actions/github-script@v7
with:
script: |
function getConsoleLink(prNumber) {
return `[Control Plane Console](https://console.cpln.io/org/${process.env.CPLN_ORG}/workloads/${process.env.APP_NAME})`;
}
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: ${{ fromJSON(steps.init-status.outputs.result).commentId }},
body: [
'🏗️ Building review app...',
'',
getConsoleLink(process.env.PR_NUMBER)
].join('\n')
});
- name: Deploy Review App
id: deploy
uses: ./.github/actions/deploy-to-control-plane
with:
app_name: ${{ env.APP_NAME }}
org: ${{ env.CPLN_ORG }}
wait_timeout: 900
github_token: ${{ secrets.GITHUB_TOKEN }}
env:
CPLN_TOKEN: ${{ secrets.CPLN_TOKEN }}
- name: Update Status - Complete
if: always()
uses: actions/github-script@v7
with:
script: |
function getConsoleLink(prNumber) {
return `[Control Plane Console](https://console.cpln.io/org/${process.env.CPLN_ORG}/workloads/${process.env.APP_NAME})`;
}
const isSuccess = '${{ job.status }}' === 'success';
const railsUrl = '${{ steps.deploy.outputs.rails_url }}';
const successMessage = [
'✅ Review app deployed successfully!',
'',
'🌐 [Rails App](' + railsUrl + ')',
'',
getConsoleLink(process.env.PR_NUMBER)
].join('\n');
const failureMessage = [
'❌ Review app deployment failed',
'',
getConsoleLink(process.env.PR_NUMBER)
].join('\n');
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: ${{ fromJSON(steps.init-status.outputs.result).commentId }},
body: isSuccess ? successMessage : failureMessage
});