Skip to content

Commit 788e600

Browse files
committed
separate to two jobs and staging
1 parent f0c726f commit 788e600

File tree

3 files changed

+131
-78
lines changed

3 files changed

+131
-78
lines changed

.controlplane/controlplane.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ apps:
5555
# QA Apps are like Heroku review apps, but the use `prefix` so you can run a commmand like
5656
# this to create a QA app for the tutorial app.
5757
# `cpflow setup gvc postgres redis rails -a qa-react-webpack-rails-tutorial-pr-1234`
58-
qa-react-webpack-rails-tutorial:
58+
qa-react-webpack-rails-tutorial-pr:
5959
<<: *common
6060
# Order matters!
6161
setup_app_templates:

.github/workflows/deploy-to-control-plane.yml .github/workflows/deploy-to-control-plane-review-app.yml

+57-66
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ jobs:
3333
debug:
3434
uses: ./.github/workflows/debug-workflow.yml
3535
with:
36-
debug_enabled: false # Will still run if vars.DEBUG_WORKFLOW is true
37-
38-
Process-Deployment-Command:
39-
needs: debug # Add this to ensure debug runs first
36+
debug_enabled: false
37+
38+
process-deployment:
39+
needs: debug
4040
if: |
4141
(github.event_name == 'pull_request') ||
4242
(github.event_name == 'push') ||
@@ -45,12 +45,13 @@ jobs:
4545
github.event.issue.pull_request &&
4646
contains(github.event.comment.body, '/deploy-review-app'))
4747
runs-on: ubuntu-latest
48-
permissions:
49-
contents: read
50-
deployments: write
51-
pull-requests: write
52-
issues: write
53-
48+
outputs:
49+
pr_number: ${{ env.PR_NUMBER }}
50+
pr_sha: ${{ env.PR_SHA }}
51+
pr_ref: ${{ steps.getRef.outputs.PR_REF }}
52+
do_deploy: ${{ env.DO_DEPLOY }}
53+
comment_id: ${{ steps.create-comment.outputs.comment-id }}
54+
deployment_id: ${{ steps.init-deployment.outputs.result }}
5455
steps:
5556
# Initial checkout only for pull_request and push events
5657
- name: Checkout code
@@ -157,13 +158,6 @@ jobs:
157158
echo "PR_REF=$(echo $PR_DATA | jq -r .headRefName)" >> $GITHUB_OUTPUT
158159
echo "PR_SHA=$(echo $PR_DATA | jq -r .headRefOid)" >> $GITHUB_ENV
159160
160-
- name: Checkout PR code
161-
if: github.event_name == 'workflow_dispatch' || github.event_name == 'issue_comment'
162-
uses: actions/checkout@v4
163-
with:
164-
fetch-depth: 0
165-
ref: ${{ steps.getRef.outputs.PR_SHA }}
166-
167161
- name: Setup Environment
168162
uses: ./.github/actions/setup-environment
169163
with:
@@ -253,26 +247,6 @@ jobs:
253247
});
254248
core.setOutput('comment-id', result.data.id);
255249
256-
- name: Update Comment - Building
257-
if: env.DO_DEPLOY != 'false'
258-
uses: actions/github-script@v7
259-
with:
260-
script: |
261-
const buildingMessage = [
262-
`🏗️ Building Docker image for PR #${process.env.PR_NUMBER}, commit ${process.env.PR_SHA}`,
263-
'',
264-
`📝 [View Build Logs](${process.env.WORKFLOW_URL})`,
265-
'',
266-
process.env.CONSOLE_LINK
267-
].join('\n');
268-
269-
await github.rest.issues.updateComment({
270-
owner: context.repo.owner,
271-
repo: context.repo.repo,
272-
comment_id: ${{ steps.create-comment.outputs.comment-id }},
273-
body: buildingMessage
274-
});
275-
276250
- name: Set Deployment URLs
277251
id: set-urls
278252
if: env.DO_DEPLOY != 'false'
@@ -305,30 +279,6 @@ jobs:
305279
'https://console.cpln.io/console/org/' + process.env.CPLN_ORG + '/gvc/' + process.env.APP_NAME + '/-info)'
306280
);
307281
308-
- name: Update Status - Building
309-
if: env.DO_DEPLOY != 'false'
310-
uses: actions/github-script@v7
311-
with:
312-
script: |
313-
const buildingMessage = [
314-
'🏗️ Building Docker image for PR #' + process.env.PR_NUMBER + ', commit ' + '${{ env.PR_SHA }}',
315-
'',
316-
'📝 [View Build Logs](' + process.env.WORKFLOW_URL + ')',
317-
'',
318-
process.env.CONSOLE_LINK
319-
].join('\n');
320-
321-
await github.rest.issues.updateComment({
322-
owner: context.repo.owner,
323-
repo: context.repo.repo,
324-
comment_id: ${{ steps.create-comment.outputs.comment-id }},
325-
body: buildingMessage
326-
});
327-
328-
- name: Checkout PR Branch
329-
if: env.DO_DEPLOY != 'false'
330-
run: git checkout ${{ steps.getRef.outputs.PR_REF }}
331-
332282
- name: Initialize GitHub Deployment
333283
if: env.DO_DEPLOY != 'false'
334284
uses: actions/github-script@v7
@@ -359,17 +309,58 @@ jobs:
359309
360310
return deployment.data.id;
361311
312+
build:
313+
needs: process-deployment
314+
if: needs.process-deployment.outputs.do_deploy != 'false'
315+
runs-on: ubuntu-latest
316+
outputs:
317+
image_tag: ${{ steps.build.outputs.image_tag }}
318+
comment_id: ${{ needs.process-deployment.outputs.comment_id }}
319+
pr_number: ${{ needs.process-deployment.outputs.pr_number }}
320+
do_deploy: ${{ needs.process-deployment.outputs.do_deploy }}
321+
steps:
322+
- name: Checkout code
323+
uses: actions/checkout@v4
324+
with:
325+
ref: ${{ needs.process-deployment.outputs.pr_ref }}
326+
327+
- name: Update Status - Building
328+
uses: actions/github-script@v7
329+
with:
330+
script: |
331+
const buildingMessage = [
332+
'🏗️ Building Docker image for PR #${{ needs.process-deployment.outputs.pr_number }}, commit ${{ needs.process-deployment.outputs.pr_sha }}',
333+
'',
334+
'📝 [View Build Logs](${{ env.WORKFLOW_URL }})',
335+
'',
336+
process.env.CONSOLE_LINK
337+
].join('\n');
338+
339+
await github.rest.issues.updateComment({
340+
owner: context.repo.owner,
341+
repo: context.repo.repo,
342+
comment_id: ${{ needs.process-deployment.outputs.comment_id }},
343+
body: buildingMessage
344+
});
345+
362346
- name: Build Docker Image
363-
if: env.DO_DEPLOY != 'false'
347+
id: build
364348
uses: ./.github/actions/build-docker-image
365349
with:
366350
app_name: ${{ env.APP_NAME }}
367351
org: ${{ vars.CPLN_ORG_STAGING }}
368-
commit: ${{ env.PR_SHA }}
369-
PR_NUMBER: ${{ env.PR_NUMBER }}
352+
commit: ${{ needs.process-deployment.outputs.pr_sha }}
353+
PR_NUMBER: ${{ needs.process-deployment.outputs.pr_number }}
354+
355+
deploy:
356+
needs: build
357+
if: needs.build.outputs.do_deploy != 'false'
358+
runs-on: ubuntu-latest
359+
steps:
360+
- name: Checkout code
361+
uses: actions/checkout@v4
370362

371363
- name: Update Status - Deploying
372-
if: env.DO_DEPLOY != 'false'
373364
uses: actions/github-script@v7
374365
with:
375366
script: |
@@ -378,7 +369,7 @@ jobs:
378369
'',
379370
'⏳ Waiting for deployment to be ready...',
380371
'',
381-
'📝 [View Deploy Logs](' + process.env.WORKFLOW_URL + ')',
372+
'📝 [View Deploy Logs](${{ env.WORKFLOW_URL }})',
382373
'',
383374
process.env.CONSOLE_LINK
384375
].join('\n');
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,94 @@
11
# Control Plane GitHub Action
22

3-
name: Deploy Main Branch to Control Plane Staging
3+
name: Deploy to Control Plane Staging
4+
run-name: Deploy Control Plane Staging App
45

56
# Controls when the workflow will run
67
on:
7-
# 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
88
push:
9-
branches: [master]
10-
11-
# Allows you to run this workflow manually from the Actions tab
9+
branches:
10+
- 'main'
11+
- 'master'
12+
- ${{ github.vars.STAGING_APP_BRANCH }}
1213
workflow_dispatch:
1314

1415
# Convert the GitHub secret variables to environment variables for use by the Control Plane CLI
1516
env:
16-
CPLN_ORG: ${{ vars.CPLN_ORG_STAGING }}
17+
APP_NAME: ${{ vars.STAGING_APP_NAME }}
1718
CPLN_TOKEN: ${{ secrets.CPLN_TOKEN_STAGING }}
19+
CPLN_ORG: ${{ vars.CPLN_ORG_STAGING }}
20+
STAGING_APP_BRANCH: ${{ vars.STAGING_APP_BRANCH }}
21+
22+
concurrency:
23+
group: deploy-staging
24+
cancel-in-progress: true
1825

1926
jobs:
20-
deploy-to-control-plane-staging:
27+
debug:
28+
uses: ./.github/workflows/debug-workflow.yml
29+
with:
30+
debug_enabled: false
31+
32+
validate-branch:
2133
runs-on: ubuntu-latest
34+
steps:
35+
- name: Check if allowed branch
36+
run: |
37+
if [[ -n "${STAGING_APP_BRANCH}" ]]; then
38+
if [[ "${GITHUB_REF#refs/heads/}" != "${STAGING_APP_BRANCH}" ]]; then
39+
echo "This workflow only runs on configured branch: ${STAGING_APP_BRANCH}"
40+
echo "Current branch: ${GITHUB_REF#refs/heads/}"
41+
exit 1
42+
fi
43+
elif [[ "${GITHUB_REF}" != "refs/heads/main" && "${GITHUB_REF}" != "refs/heads/master" ]]; then
44+
echo "This workflow only runs on main or master branch (no STAGING_APP_BRANCH configured)"
45+
echo "Current branch: ${GITHUB_REF#refs/heads/}"
46+
exit 1
47+
fi
2248
49+
build:
50+
needs: validate-branch
51+
runs-on: ubuntu-latest
52+
outputs:
53+
image_tag: ${{ steps.build.outputs.image_tag }}
2354
steps:
24-
- uses: actions/checkout@v4
55+
- name: Checkout code
56+
uses: actions/checkout@v4
2557
with:
26-
fetch-depth: 0 # Fetch all history for proper SHA handling
27-
ref: master # Explicitly checkout master branch
58+
fetch-depth: 0
59+
60+
- name: Setup Environment
61+
uses: ./.github/actions/setup-environment
62+
with:
63+
token: ${{ secrets.CPLN_TOKEN_STAGING }}
64+
org: ${{ vars.CPLN_ORG_STAGING }}
65+
66+
- name: Build Docker Image
67+
id: build
68+
uses: ./.github/actions/build-docker-image
69+
with:
70+
app_name: ${{ env.APP_NAME }}
71+
org: ${{ vars.CPLN_ORG_STAGING }}
72+
commit: ${{ github.sha }}
73+
74+
deploy:
75+
needs: build
76+
runs-on: ubuntu-latest
77+
steps:
78+
- name: Checkout code
79+
uses: actions/checkout@v4
80+
81+
- name: Setup Environment
82+
uses: ./.github/actions/setup-environment
83+
with:
84+
token: ${{ secrets.CPLN_TOKEN_STAGING }}
85+
org: ${{ vars.CPLN_ORG_STAGING }}
2886

29-
- uses: ./.github/actions/deploy-to-control-plane
87+
- name: Deploy to Control Plane
88+
uses: ./.github/actions/deploy-to-control-plane
3089
with:
3190
app_name: ${{ vars.STAGING_APP_NAME }}
3291
org: ${{ vars.CPLN_ORG_STAGING }}
92+
github_token: ${{ secrets.GITHUB_TOKEN }}
93+
wait_timeout: ${{ vars.WAIT_TIMEOUT || 900 }}
94+
cpln_token: ${{ secrets.CPLN_TOKEN_STAGING }}

0 commit comments

Comments
 (0)