Skip to content

Commit f19d79f

Browse files
committed
Updates to deployment
1 parent 8355030 commit f19d79f

File tree

2 files changed

+72
-54
lines changed

2 files changed

+72
-54
lines changed

.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: 70 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,103 @@
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:
2821
deploy-to-control-plane-review:
29-
if: ${{ github.event_name != 'issue_comment' || (github.event.comment.body == '/deploy-review-app' && github.event.issue.pull_request) }}
22+
if: |
23+
github.event_name == 'workflow_dispatch' ||
24+
github.event_name == 'pull_request' ||
25+
(github.event_name == 'issue_comment' &&
26+
github.event.comment.body == '/deploy-review-app' &&
27+
github.event.issue.pull_request)
3028
runs-on: ubuntu-latest
3129

30+
outputs:
31+
app_url: ${{ steps.deploy.outputs.app_url }}
32+
deployment_id: ${{ steps.create-deployment.outputs.deployment_id }}
33+
3234
steps:
35+
- name: Create GitHub Deployment
36+
id: create-deployment
37+
uses: actions/github-script@v7
38+
with:
39+
script: |
40+
const deployment = await github.rest.repos.createDeployment({
41+
owner: context.repo.owner,
42+
repo: context.repo.repo,
43+
ref: context.sha,
44+
environment: 'review-app',
45+
auto_merge: false,
46+
required_contexts: []
47+
});
48+
return deployment.data.id;
49+
3350
- name: Get PR HEAD Ref
3451
if: ${{ github.event_name == 'issue_comment' }}
3552
id: getRef
36-
run: echo "PR_REF=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json headRefName | jq -r '.headRefName')" >> $GITHUB_OUTPUT
53+
run: |
54+
echo "PR_REF=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json headRefName | jq -r '.headRefName')" >> $GITHUB_OUTPUT
3755
env:
3856
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3957

40-
- name: Checkout source code from Github
58+
- name: Checkout source code
4159
uses: actions/checkout@v4
4260
with:
4361
fetch-depth: 0
4462
ref: ${{ steps.getRef.outputs.PR_REF || github.ref }}
4563

46-
- name: Add GitHub Comment
47-
if: ${{ github.event_name == 'issue_comment' }}
64+
- name: Update deployment status (in_progress)
4865
uses: actions/github-script@v7
4966
with:
5067
script: |
51-
github.rest.issues.createComment({
52-
issue_number: context.issue.number,
68+
await github.rest.repos.createDeploymentStatus({
5369
owner: context.repo.owner,
5470
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-
})
71+
deployment_id: ${{ steps.create-deployment.outputs.deployment_id }},
72+
state: 'in_progress',
73+
description: 'Deployment is in progress'
74+
});
5775
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
78-
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
76+
# ... rest of your existing steps ...
77+
78+
- name: Update deployment status (success)
79+
if: success()
80+
uses: actions/github-script@v7
8381
with:
84-
app_name: ${{ env.APP_NAME }}
85-
org: ${{ env.CPLN_ORG }}
82+
script: |
83+
await github.rest.repos.createDeploymentStatus({
84+
owner: context.repo.owner,
85+
repo: context.repo.repo,
86+
deployment_id: ${{ steps.create-deployment.outputs.deployment_id }},
87+
state: 'success',
88+
environment_url: '${{ steps.deploy.outputs.app_url }}',
89+
description: 'Deployment successful'
90+
});
91+
92+
- name: Update deployment status (failure)
93+
if: failure()
94+
uses: actions/github-script@v7
95+
with:
96+
script: |
97+
await github.rest.repos.createDeploymentStatus({
98+
owner: context.repo.owner,
99+
repo: context.repo.repo,
100+
deployment_id: ${{ steps.create-deployment.outputs.deployment_id }},
101+
state: 'failure',
102+
description: 'Deployment failed'
103+
});

0 commit comments

Comments
 (0)