-
Notifications
You must be signed in to change notification settings - Fork 72
326 lines (284 loc) · 12.5 KB
/
deploy-to-feature-environment.yml
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
#
# OpenCRVS is also distributed under the terms of the Civil Registration
# & Healthcare Disclaimer located at http://opencrvs.org/license.
#
# Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
name: Deploy PR to feature environment
on:
pull_request:
types: [opened, synchronize]
workflow_dispatch:
inputs:
pr_number:
description: 'PR number'
required: true
type: string
concurrency:
group: ${{ inputs.pr_number || github.ref }}
cancel-in-progress: true
jobs:
generate_stack_name_and_branch:
runs-on: ubuntu-22.04
outputs:
slugified_branch: ${{ steps.slugify_bname.outputs.stack }}
branch_name: ${{ steps.set_branch_and_pr_number.outputs.BRANCH_NAME }}
pr_number: ${{ steps.set_branch_and_pr_number.outputs.PR_NUMBER }}
author: ${{ steps.get_author.outputs.AUTHOR }}
steps:
- uses: actions/checkout@v3
- name: Get branch name (when manually triggered)
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
if: ${{ github.event_name == 'workflow_dispatch' }}
run: |
PR_NUMBER=${{ github.event.inputs.pr_number }}
PR_DATA=$(gh pr view $PR_NUMBER --json headRefName,headRefOid)
BRANCH_NAME=$(echo "$PR_DATA" | jq -r '.headRefName')
echo "BRANCH_NAME=${BRANCH_NAME}" >> $GITHUB_ENV
echo "PR_NUMBER=${PR_NUMBER}" >> $GITHUB_ENV
- name: Get PR Information
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
if: ${{ github.event_name != 'workflow_dispatch' }}
run: |
PR_NUMBER=${{ github.event.pull_request.number }}
echo "PR_NUMBER=${PR_NUMBER}" >> $GITHUB_ENV
- name: Check for "🚀 Ready to deploy" label
run: |
labels=$(gh pr view $PR_NUMBER --json labels --jq '.labels[].name')
if [[ "$labels" != *"🚀 Ready to deploy"* ]]; then
echo "Label '🚀 Ready to deploy' not found. Exiting."
exit 1
else
echo "Label '🚀 Ready to deploy' found. Continuing."
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Get PR Author
id: get_author
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
run: |
AUTHOR=$(gh pr view $PR_NUMBER --json author --jq '.author.login')
echo "PR is created by $AUTHOR"
echo "AUTHOR=$(echo $AUTHOR)" >> $GITHUB_ENV
echo "::set-output name=AUTHOR::$AUTHOR"
- name: Get Branch Name (on PR creation)
if: ${{ github.event_name != 'workflow_dispatch' }}
run: |
echo "BRANCH_NAME=$(echo ${{ github.head_ref }})" >> $GITHUB_ENV
- name: Set the branch name as output
id: set_branch_and_pr_number
run: |
echo "BRANCH_NAME=$(echo ${{ env.BRANCH_NAME }})" >> $GITHUB_OUTPUT
echo "PR_NUMBER=$(echo ${{ env.PR_NUMBER }})" >> $GITHUB_OUTPUT
- name: Slugify the branch name
id: slugify_bname
uses: actions/github-script@v7
with:
script: |
function slugify(str) {
return str
.toLowerCase()
.replace(/[^\w\s-]/g, '')
.trim()
.replace(/\s+/g, '-')
.replace(/-+/g, '-')
.substr(0, 35)
.replace(/[^a-zA-Z0-9]+$/g, '');
}
core.setOutput('stack', slugify('${{ env.BRANCH_NAME }}'));
trigger-e2e:
if: ${{ (github.event_name == 'workflow_dispatch') || (!contains(github.actor, 'bot') && github.event.pull_request.head.repo.fork == false) }}
runs-on: ubuntu-22.04
needs: generate_stack_name_and_branch
environment: ${{ needs.generate_stack_name_and_branch.outputs.slugified_branch }}
outputs:
run_id: ${{ steps.dispatch_e2e.outputs.run_id }}
deployment_link: ${{ steps.print-links.outputs.deployment_link }}
steps:
- uses: actions/checkout@v3
- name: Parse the branch name and set it as environment variable
run: |
BRANCH_NAME=${{ needs.generate_stack_name_and_branch.outputs.branch_name }}
echo "BRANCH_NAME=${BRANCH_NAME}" >> $GITHUB_ENV
- name: Get PR Information (when manually triggered)
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
if: ${{ github.event_name == 'workflow_dispatch' }}
run: |
PR_NUMBER=${{ github.event.inputs.pr_number }}
PR_DATA=$(gh pr view $PR_NUMBER --json headRefName,headRefOid)
HEAD_COMMIT_HASH=$(echo "$PR_DATA" | jq -r '.headRefOid' | cut -c1-7)
echo "HEAD_COMMIT_HASH=${HEAD_COMMIT_HASH}" >> $GITHUB_ENV
- name: Get Head Commit Hash (on PR creation)
if: ${{ github.event_name != 'workflow_dispatch' }}
id: vars
run: |
COMMIT_HASH=$(git rev-parse --short=7 ${{ github.event.pull_request.head.sha }})
echo "HEAD_COMMIT_HASH=${COMMIT_HASH}" >> $GITHUB_ENV
- name: Check if branch exists in opencrvs-farajaland repo
run: |
FARAJALAND_REPO=https://github.com/opencrvs/opencrvs-farajaland
if git ls-remote --heads $FARAJALAND_REPO ${{ env.BRANCH_NAME }} | grep -q "${{ env.BRANCH_NAME }}"; then
COMMIT_HASH=$(git ls-remote $FARAJALAND_REPO refs/heads/${{ env.BRANCH_NAME }} | cut -c1-7)
else
COMMIT_HASH=$(git ls-remote $FARAJALAND_REPO refs/heads/develop | cut -c1-7)
fi
echo "FARAJALAND_COMMIT_HASH=${COMMIT_HASH}" >> $GITHUB_ENV
- name: Output Variables
run: |
echo "PR Branch: ${{ env.BRANCH_NAME }}"
echo "PR Head Commit Hash: ${{ env.HEAD_COMMIT_HASH }}"
echo "Farajaland Commit Hash: ${{ env.FARAJALAND_COMMIT_HASH }}"
- name: Parse the stack name
id: generate_stack
run: |
stack=${{ needs.generate_stack_name_and_branch.outputs.slugified_branch }}
echo "stack=${stack}" >> $GITHUB_OUTPUT
- name: Trigger E2E Workflow
id: dispatch_e2e
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GH_TOKEN }}
script: |
const result = await github.rest.repos.createDispatchEvent({
owner: 'opencrvs',
repo: 'e2e',
event_type: 'run_e2e',
client_payload: {
actor: '${{ needs.generate_stack_name_and_branch.outputs.author }}',
'core-image-tag': '${{ env.HEAD_COMMIT_HASH }}',
'countryconfig-image-tag': '${{ env.FARAJALAND_COMMIT_HASH }}',
stack: '${{ steps.generate_stack.outputs.stack }}'
}
});
console.log(result);
await new Promise(resolve => setTimeout(resolve, 10000));
const runs = await github.rest.actions.listWorkflowRunsForRepo({
owner: 'opencrvs',
repo: 'e2e',
event: 'repository_dispatch',
per_page: 1
});
if (runs.data.workflow_runs.length > 0) {
const runId = runs.data.workflow_runs[0].id;
console.log(`Captured runId: ${runId}`);
// Set the runId as an output
core.setOutput('run_id', runId);
} else {
throw new Error('No workflow run found.');
}
- name: Print link to E2E workflow run
id: print-links
run: |
E2E_RUN_LINK="https://github.com/opencrvs/e2e/actions/runs/${{ steps.dispatch_e2e.outputs.run_id }}"
DEPLOYMENT_LINK="https://${{ steps.generate_stack.outputs.stack }}.opencrvs.dev"
echo "See your E2E deployment run details here: $E2E_RUN_LINK" >> $GITHUB_STEP_SUMMARY
echo "All deployments & E2E of this environment you can see here: https://github.com/opencrvs/e2e/deployments/${{ steps.generate_stack.outputs.stack }}" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "If everything goes alright, you should see your stack getting deployed here: $DEPLOYMENT_LINK" >> $GITHUB_STEP_SUMMARY
echo "deployment_link=$DEPLOYMENT_LINK" >> $GITHUB_OUTPUT
listen-e2e:
needs: [trigger-e2e, generate_stack_name_and_branch]
runs-on: ubuntu-22.04
steps:
- name: Wait for Environment Deployment (Deploy Job)
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GH_TOKEN }}
script: |
const owner = 'opencrvs';
const repo = 'e2e';
const runId = ${{ needs.trigger-e2e.outputs.run_id }};
const prNumber = ${{ needs.generate_stack_name_and_branch.outputs.pr_number }};
const deployMessage = `Your environment is deployed to ${{ needs.trigger-e2e.outputs.deployment_link }}`;
let deployJobCompleted = false;
// Check if deploy job has completed
while (!deployJobCompleted) {
const workflowRun = await github.rest.actions.getWorkflowRun({
owner,
repo,
run_id: runId
});
const jobs = await github.rest.actions.listJobsForWorkflowRun({
owner,
repo,
run_id: runId
});
const deployJob = jobs.data.jobs.find(job => job.name === 'deploy / seed-data / seed-data');
const cancelled = jobs.data.jobs.find(job => job.conclusion === 'cancelled');
if (cancelled) {
throw new Error('E2E workflow was cancelled');
}
if (deployJob && deployJob.status === 'completed') {
deployJobCompleted = true;
if (deployJob.conclusion !== 'success') {
throw new Error('Deploy job failed');
}
console.log('Deploy job completed successfully');
}
if(workflowRun.data.status === 'completed') {
deployJobCompleted = true;
if (workflowRun.data.conclusion !== 'success') {
throw new Error('E2E workflow failed');
}
}
if (!deployJobCompleted) {
await new Promise(resolve => setTimeout(resolve, 10000));
}
}
// Check if the comment already exists
const comments = await github.rest.issues.listComments({
owner: 'opencrvs',
repo: 'opencrvs-core',
issue_number: prNumber
});
const existingComment = comments.data.find(comment => comment.body.includes(deployMessage));
if (!existingComment) {
// Add PR comment if it doesn't exist
await github.rest.issues.createComment({
owner: 'opencrvs',
repo: 'opencrvs-core',
issue_number: prNumber,
body: deployMessage
});
console.log('PR comment added');
} else {
console.log('PR comment already exists, skipping...');
}
- name: Wait for E2E Workflow Completion
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GH_TOKEN }}
script: |
const owner = 'opencrvs';
const repo = 'e2e';
const runId = ${{ needs.trigger-e2e.outputs.run_id }};
let status = 'in_progress';
while (status === 'in_progress' || status === 'queued') {
const run = await github.rest.actions.getWorkflowRun({
owner,
repo,
run_id: runId
});
status = run.data.status;
console.log(`Current status: ${status}`);
if (status === 'in_progress' || status === 'queued') {
await new Promise(resolve => setTimeout(resolve, 10000));
}
}
if (status === 'completed') {
const conclusion = await github.rest.actions.getWorkflowRun({
owner,
repo,
run_id: runId
});
console.log(`Workflow finished with conclusion: ${conclusion.data.conclusion}`);
if (conclusion.data.conclusion !== 'success') {
throw new Error('E2E workflow failed');
}
}