Skip to content

Commit 8bd409c

Browse files
authored
Merge de55983 into 005eab7
2 parents 005eab7 + de55983 commit 8bd409c

File tree

4 files changed

+153
-64
lines changed

4 files changed

+153
-64
lines changed

.controlplane/Dockerfile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
ARG RUBY_VERSION=3.3.4
33
FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base
44

5+
# Current commit hash environment variable
6+
ARG GIT_COMMIT
7+
ENV GIT_COMMIT_SHA=${GIT_COMMIT}
8+
59
# Install packages needed to build gems and node modules
610
RUN apt-get update -qq && \
711
apt-get install --no-install-recommends -y build-essential curl git libpq-dev libvips node-gyp pkg-config python-is-python3
@@ -76,7 +80,3 @@ ENTRYPOINT ["./.controlplane/entrypoint.sh"]
7680
# Default args to pass to the entry point that can be overridden
7781
# For Kubernetes and ControlPlane, these are the "workload args"
7882
CMD ["./bin/rails", "server"]
79-
80-
# Current commit hash environment variable
81-
ARG GIT_COMMIT_SHA
82-
ENV GIT_COMMIT_SHA=${GIT_COMMIT_SHA}

.github/actions/deploy-to-control-plane/action.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ inputs:
77
app_name:
88
description: 'The name of the app to deploy'
99
required: true
10-
default:
1110
org:
1211
description: 'The org of the app to deploy'
1312
required: true
14-
default:
1513

1614
runs:
1715
using: 'composite'
@@ -22,13 +20,14 @@ runs:
2220
- name: Set Short SHA
2321
id: vars
2422
shell: bash
25-
run: echo "::set-output name=sha_short::$(git rev-parse --short HEAD)"
23+
run: |
24+
echo "sha_short=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"
2625
27-
# Caching step
28-
- uses: actions/cache@v2
26+
# Updated caching step to v3
27+
- uses: actions/cache@v3
2928
with:
3029
path: /tmp/.docker-cache
31-
key: ${{ runner.os }}-docker-${{ hashFiles('**/Dockerfile', '**/package.json', '**/yarn.lock') }}-${{ github.sha }}
30+
key: ${{ runner.os }}-docker-${{ hashFiles('**/Dockerfile', '**/package.json', '**/yarn.lock') }}-${{ github.sha }}
3231
restore-keys: |
3332
${{ runner.os }}-docker-${{ hashFiles('**/Dockerfile', '**/package.json', '**/yarn.lock') }}
3433
${{ runner.os }}-docker-
@@ -39,6 +38,7 @@ runs:
3938
if ! cpflow exists -a ${{ inputs.app_name }} ; then
4039
cpflow setup-app -a ${{ inputs.app_name }}
4140
fi
41+
4242
# Provision all infrastructure on Control Plane.
4343
# app react-webpack-rails-tutorial will be created per definition in .controlplane/controlplane.yml
4444
- name: cpflow build-image
@@ -47,10 +47,10 @@ runs:
4747
cpln image docker-login
4848
# Use BUILDKIT_PROGRESS=plain to get more verbose logging of the build
4949
# BUILDKIT_PROGRESS=plain cpflow build-image -a ${{ inputs.app_name }} --commit ${{steps.vars.outputs.sha_short}} --org ${{inputs.org}}
50-
cpflow build-image -a ${{ inputs.app_name }} --commit ${{steps.vars.outputs.sha_short}} --org ${{inputs.org}}
51-
# --cache /tmp/.docker-cache
50+
cpflow build-image -a ${{ inputs.app_name }} --commit=${{steps.vars.outputs.sha_short}} --org=${{inputs.org}}
51+
5252
- name: Deploy to Control Plane
5353
shell: bash
5454
run: |
5555
echo "Deploying to Control Plane"
56-
cpflow deploy-image -a ${{ inputs.app_name }} --run-release-phase --org ${{inputs.org}} --verbose
56+
cpflow deploy-image -a ${{ inputs.app_name }} --run-release-phase --org ${{inputs.org}} --verbose

.github/actions/setup-environment/action.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ runs:
1414
- name: Install Control Plane CLI and cpflow gem
1515
shell: bash
1616
run: |
17-
sudo npm install -g @controlplane/cli@3.1.0
17+
sudo npm install -g @controlplane/cli@3.3.0
1818
cpln --version
19-
gem install cpflow -v 4.0.0
19+
gem install cpflow -v 4.1.0
2020
cpflow --version
2121
2222
- name: cpln profile
Lines changed: 138 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,174 @@
1-
# Control Plane GitHub Action
2-
31
name: Deploy Review App to Control Plane
42

5-
# Controls when the workflow will run
63
on:
7-
# Allows you to run this workflow manually from the Actions tab
84
workflow_dispatch:
9-
10-
# Uncomment these lines to trigger the workflow on pull request events
11-
# pull_request:
12-
# branches:
13-
# - master
14-
15-
# deploy on comment "/deploy-review-app"
5+
pull_request:
6+
types: [opened, synchronize, reopened]
7+
branches: [master]
168
issue_comment:
17-
types: [created, edited]
9+
types: [created]
10+
11+
concurrency:
12+
group: review-app-${{ github.event.pull_request.number || github.event.issue.number }}
13+
cancel-in-progress: true
1814

19-
# Convert the GitHub secret variables to environment variables for use by the Control Plane CLI
2015
env:
2116
CPLN_ORG: ${{secrets.CPLN_ORG_STAGING}}
2217
CPLN_TOKEN: ${{secrets.CPLN_TOKEN_STAGING}}
23-
# Uncomment this line to use the PR number from the pull requests trigger event (that trigger is commented)
24-
# PR_NUMBER: ${{ github.event.pull_request.number || github.event.issue.number }}
25-
PR_NUMBER: ${{ github.event.issue.number }}
18+
PR_NUMBER: ${{ github.event.pull_request.number || github.event.issue.number }}
2619

2720
jobs:
21+
check-concurrent:
22+
runs-on: ubuntu-latest
23+
outputs:
24+
cancelled: ${{ steps.check.outputs.cancelled }}
25+
steps:
26+
- name: Check for concurrent deployment
27+
id: check
28+
run: |
29+
if [ "${{ github.run_attempt }}" != "1" ]; then
30+
echo "⚠️ Cancelling previous deployment due to new code push..."
31+
echo "cancelled=true" >> $GITHUB_OUTPUT
32+
else
33+
echo "cancelled=false" >> $GITHUB_OUTPUT
34+
fi
35+
2836
deploy-to-control-plane-review:
29-
if: ${{ github.event_name != 'issue_comment' || (github.event.comment.body == '/deploy-review-app' && github.event.issue.pull_request) }}
37+
needs: check-concurrent
38+
if: |
39+
needs.check-concurrent.outputs.cancelled != 'true' &&
40+
(github.event_name == 'workflow_dispatch' ||
41+
github.event_name == 'pull_request' ||
42+
(github.event_name == 'issue_comment' &&
43+
github.event.comment.body == '/deploy-review-app' &&
44+
github.event.issue.pull_request))
3045
runs-on: ubuntu-latest
3146

47+
permissions:
48+
contents: read
49+
deployments: write
50+
pull-requests: write
51+
52+
outputs:
53+
app_url: ${{ steps.deploy.outputs.app_url }}
54+
deployment_id: ${{ steps.create-deployment.outputs.result.deployment_id }}
55+
3256
steps:
57+
- name: Notify deployment start
58+
uses: actions/github-script@v7
59+
with:
60+
script: |
61+
const message = `🚀 Starting new deployment for commit: ${context.sha.substring(0, 7)}
62+
${context.payload.commits ? `\nChanges: ${context.payload.commits[0].message}` : ''}`;
63+
64+
await github.rest.issues.createComment({
65+
issue_number: context.issue.number || context.payload.pull_request.number,
66+
owner: context.repo.owner,
67+
repo: context.repo.repo,
68+
body: message
69+
});
70+
71+
- name: Create GitHub Deployment
72+
id: create-deployment
73+
uses: actions/github-script@v7
74+
with:
75+
script: |
76+
const deployment = await github.rest.repos.createDeployment({
77+
owner: context.repo.owner,
78+
repo: context.repo.repo,
79+
ref: context.sha,
80+
environment: 'review-app',
81+
auto_merge: false,
82+
required_contexts: []
83+
});
84+
return { deployment_id: deployment.data.id };
85+
3386
- name: Get PR HEAD Ref
3487
if: ${{ github.event_name == 'issue_comment' }}
3588
id: getRef
36-
run: echo "PR_REF=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json headRefName | jq -r '.headRefName')" >> $GITHUB_OUTPUT
89+
run: |
90+
echo "PR_REF=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json headRefName | jq -r '.headRefName')" >> $GITHUB_OUTPUT
3791
env:
3892
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3993

40-
- name: Checkout source code from Github
94+
- name: Checkout source code
4195
uses: actions/checkout@v4
4296
with:
4397
fetch-depth: 0
4498
ref: ${{ steps.getRef.outputs.PR_REF || github.ref }}
4599

46-
- name: Add GitHub Comment
47-
if: ${{ github.event_name == 'issue_comment' }}
100+
- name: Update deployment status (in_progress)
48101
uses: actions/github-script@v7
49102
with:
50103
script: |
51-
github.rest.issues.createComment({
52-
issue_number: context.issue.number,
104+
await github.rest.repos.createDeploymentStatus({
53105
owner: context.repo.owner,
54106
repo: context.repo.repo,
55-
body: "We started working on your review-app deployment. You can track progress in the `Actions` Tab [here](https://github.com/shakacode/react-webpack-rails-tutorial/actions/workflows/deploy-to-control-plane-review.yml) on Github."
56-
})
107+
deployment_id: ${{ steps.create-deployment.outputs.result.deployment_id }},
108+
state: 'in_progress',
109+
description: 'Deployment is in progress'
110+
});
57111
58-
- name: Get PR number
59-
if: ${{ github.event_name != 'issue_comment' }}
60-
run: |
61-
echo "GITHUB_REPOSITORY: \"$GITHUB_REPOSITORY\""
62-
if [ -z "$PR_NUMBER" ]; then
63-
echo "PR_NUMBER is not in the trigger event. Fetching PR number from open PRs."
64-
REF="${{ github.ref }}"
65-
REF=${REF#refs/heads/} # Remove 'refs/heads/' prefix
66-
echo "REF: \"$REF\""
67-
API_RESPONSE=$(curl --location --request GET "https://github.com/api/repos/${GITHUB_REPOSITORY}/pulls?state=open" \
68-
--header 'Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}')
69-
PR_NUMBER=$(echo "$API_RESPONSE" | jq '.[] | select(.head.ref=="'$REF'") | .number')
70-
fi
71-
echo "PR_NUMBER: $PR_NUMBER"
72-
if [ -z "$PR_NUMBER" ]; then
73-
echo "PR_NUMBER is not set. Aborting."
74-
exit 1
75-
fi
76-
echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
77-
- name: Get App Name
112+
- name: Configure app name
113+
id: app-config
78114
run: |
79-
echo "PR_NUMBER: ${{ env.PR_NUMBER }}"
80-
echo "APP_NAME=qa-react-webpack-rails-tutorial-pr-${{ env.PR_NUMBER }}" >> "$GITHUB_ENV"
81-
echo "App Name: ${{ env.APP_NAME }}"
82-
- uses: ./.github/actions/deploy-to-control-plane
115+
APP_NAME="qa-react-webpack-rails-tutorial-pr-${{ env.PR_NUMBER }}"
116+
echo "APP_NAME=$APP_NAME" >> $GITHUB_ENV
117+
echo "app_name=$APP_NAME" >> $GITHUB_OUTPUT
118+
119+
- name: Deploy to Control Plane
120+
id: deploy
121+
uses: ./.github/actions/deploy-to-control-plane
83122
with:
84123
app_name: ${{ env.APP_NAME }}
85124
org: ${{ env.CPLN_ORG }}
125+
126+
- name: Update deployment status (success)
127+
if: success()
128+
uses: actions/github-script@v7
129+
with:
130+
script: |
131+
const message = `✅ Deployment successful!
132+
Environment: review-app
133+
Commit: ${context.sha.substring(0, 7)}
134+
URL: ${{ steps.deploy.outputs.app_url }}`;
135+
136+
await github.rest.issues.createComment({
137+
issue_number: context.issue.number || context.payload.pull_request.number,
138+
owner: context.repo.owner,
139+
repo: context.repo.repo,
140+
body: message
141+
});
142+
143+
await github.rest.repos.createDeploymentStatus({
144+
owner: context.repo.owner,
145+
repo: context.repo.repo,
146+
deployment_id: ${{ steps.create-deployment.outputs.result.deployment_id }},
147+
state: 'success',
148+
environment_url: '${{ steps.deploy.outputs.app_url }}',
149+
description: 'Deployment successful'
150+
});
151+
152+
- name: Update deployment status (failure)
153+
if: failure()
154+
uses: actions/github-script@v7
155+
with:
156+
script: |
157+
const message = `❌ Deployment failed
158+
Commit: ${context.sha.substring(0, 7)}
159+
Please check the [workflow logs](${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}) for details.`;
160+
161+
await github.rest.issues.createComment({
162+
issue_number: context.issue.number || context.payload.pull_request.number,
163+
owner: context.repo.owner,
164+
repo: context.repo.repo,
165+
body: message
166+
});
167+
168+
await github.rest.repos.createDeploymentStatus({
169+
owner: context.repo.owner,
170+
repo: context.repo.repo,
171+
deployment_id: ${{ steps.create-deployment.outputs.result.deployment_id }},
172+
state: 'failure',
173+
description: 'Deployment failed'
174+
});

0 commit comments

Comments
 (0)