-
Notifications
You must be signed in to change notification settings - Fork 374
Expand file tree
/
Copy pathdeploy-to-control-plane-review.yml
More file actions
178 lines (155 loc) · 6.1 KB
/
deploy-to-control-plane-review.yml
File metadata and controls
178 lines (155 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
name: Deploy Review App to Control Plane
on:
workflow_dispatch:
pull_request:
types: [opened, synchronize, reopened]
branches: [master]
issue_comment:
types: [created]
concurrency:
group: review-app-${{ github.event.pull_request.number || github.event.issue.number }}
cancel-in-progress: true
env:
CPLN_ORG: ${{secrets.CPLN_ORG_STAGING}}
CPLN_TOKEN: ${{secrets.CPLN_TOKEN_STAGING}}
PR_NUMBER: ${{ github.event.pull_request.number || github.event.issue.number }}
STATUS_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}/jobs/${{ github.job }}?pr=${{ github.event.pull_request.number || github.event.issue.number }}
jobs:
check-concurrent:
runs-on: ubuntu-latest
outputs:
cancelled: ${{ steps.check.outputs.cancelled }}
steps:
- name: Check for concurrent deployment
id: check
run: |
if [ "${{ github.run_attempt }}" != "1" ]; then
echo "⚠️ Cancelling previous deployment due to new code push..."
echo "cancelled=true" >> $GITHUB_OUTPUT
else
echo "cancelled=false" >> $GITHUB_OUTPUT
fi
deploy-to-control-plane-review:
needs: check-concurrent
if: |
needs.check-concurrent.outputs.cancelled != 'true' &&
(github.event_name == 'workflow_dispatch' ||
github.event_name == 'pull_request' ||
(github.event_name == 'issue_comment' &&
github.event.comment.body == '/deploy-review-app' &&
github.event.issue.pull_request))
runs-on: ubuntu-latest
permissions:
contents: read
deployments: write
pull-requests: write
outputs:
app_url: ${{ steps.deploy.outputs.app_url }}
deployment_id: ${{ steps.create-deployment.outputs.result }}
steps:
- name: Create comment
id: create-comment
uses: actions/github-script@v7
with:
script: |
const createComment = async (message) => {
await github.rest.issues.createComment({
issue_number: context.issue.number || context.payload.pull_request.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: message
});
};
core.exportVariable('createComment', createComment);
- name: Notify deployment start
uses: actions/github-script@v7
with:
script: |
const message = `🚀 Starting new deployment for commit: ${context.sha.substring(0, 7)}
${context.payload.commits ? `\nChanges: ${context.payload.commits[0].message}` : ''}
Status: ${{ env.STATUS_URL }}`;
await eval(process.env.createComment)(message);
- name: Create GitHub Deployment
id: create-deployment
uses: actions/github-script@v7
with:
script: |
const deployment = await github.rest.repos.createDeployment({
owner: context.repo.owner,
repo: context.repo.repo,
ref: context.sha,
environment: 'review-app',
auto_merge: false,
required_contexts: []
});
return deployment.data.id;
- name: Get PR HEAD Ref
if: ${{ github.event_name == 'issue_comment' }}
id: getRef
run: |
echo "PR_REF=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json headRefName | jq -r '.headRefName')" >> $GITHUB_OUTPUT
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Checkout source code
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ steps.getRef.outputs.PR_REF || github.ref }}
- name: Update deployment status (in_progress)
uses: actions/github-script@v7
with:
script: |
await github.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: ${{ steps.create-deployment.outputs.result }},
state: 'in_progress',
description: 'Deployment is in progress'
});
- name: Configure app name
id: app-config
run: |
APP_NAME="qa-react-webpack-rails-tutorial-pr-${{ env.PR_NUMBER }}"
echo "APP_NAME=$APP_NAME" >> $GITHUB_ENV
echo "app_name=$APP_NAME" >> $GITHUB_OUTPUT
- name: Deploy to Control Plane
id: deploy
uses: ./.github/actions/deploy-to-control-plane
with:
app_name: ${{ env.APP_NAME }}
org: ${{ env.CPLN_ORG }}
- name: Update deployment status (success)
if: success()
uses: actions/github-script@v7
with:
script: |
const message = `✅ Deployment successful!
Environment: review-app
Commit: ${context.sha.substring(0, 7)}
URL: ${{ steps.deploy.outputs.app_url }}
Status: ${{ env.STATUS_URL }}`;
await eval(process.env.createComment)(message);
await github.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: ${{ steps.create-deployment.outputs.result }},
state: 'success',
environment_url: '${{ steps.deploy.outputs.app_url }}',
description: 'Deployment successful'
});
- name: Update deployment status (failure)
if: failure()
uses: actions/github-script@v7
with:
script: |
const message = `❌ Deployment failed
Commit: ${context.sha.substring(0, 7)}
Status: ${{ env.STATUS_URL }}`;
await eval(process.env.createComment)(message);
await github.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: ${{ steps.create-deployment.outputs.result }},
state: 'failure',
description: 'Deployment failed'
});