-
Notifications
You must be signed in to change notification settings - Fork 0
136 lines (122 loc) · 5.21 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
name: "Deploy to feature environment"
on:
pull_request:
branches:
- main
workflow_dispatch:
inputs:
stack:
type: string
description: Stack name
required: false
core-image-tag:
type: string
description: Core DockerHub image tag
required: false
countryconfig-image-tag:
type: string
description: Countryconfig DockerHub image tag
required: false
jobs:
trigger:
runs-on: ubuntu-latest
outputs:
stack: ${{ steps.trigger-workflow.outputs.stack }}
steps:
- name: Fetch latest commit tag from opencrvs-core branch
if: inputs.core-image-tag == ''
run: |
CORE_BRANCH=$(curl -s https://api.github.com/repos/opencrvs/opencrvs-core/branches/${{ github.head_ref }} | jq -r '.name // "develop"')
CORE_COMMIT_HASH=$(curl -s https://api.github.com/repos/opencrvs/opencrvs-core/commits/$CORE_BRANCH | jq -r '.sha' | cut -c1-7)
echo "CORE_COMMIT_HASH=$CORE_COMMIT_HASH" >> $GITHUB_ENV
- name: Fetch latest commit tag from opencrvs-farajaland branch
if: inputs.countryconfig-image-tag == ''
run: |
FARAJALAND_BRANCH=$(curl -s https://api.github.com/repos/opencrvs/opencrvs-farajaland/branches/${{ github.head_ref }} | jq -r '.name // "mosip"')
FARAJALAND_COMMIT_HASH=$(curl -s https://api.github.com/repos/opencrvs/opencrvs-farajaland/commits/$FARAJALAND_BRANCH | jq -r '.sha' | cut -c1-7)
echo "FARAJALAND_COMMIT_HASH=$FARAJALAND_COMMIT_HASH" >> $GITHUB_ENV
- name: Trigger workflow in e2e repository
id: trigger-workflow
env:
GITHUB_TOKEN: ${{ secrets.OLLIE_BOT_GITHUB_TOKEN }}
CORE_COMMIT_HASH: ${{ env.CORE_COMMIT_HASH }}
FARAJALAND_COMMIT_HASH: ${{ env.FARAJALAND_COMMIT_HASH }}
run: |
KEBAB_CASE_STACK=$(echo ${{ github.head_ref }} | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/-/g' | sed 's/-\+/-/g')
MOSIP_STACK=$(echo "mosip-$KEBAB_CASE_STACK" | cut -c 1-35)
echo "stack=$MOSIP_STACK" >> $GITHUB_OUTPUT
curl -X POST \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/opencrvs/e2e/actions/workflows/deploy.yml/dispatches \
-d '{
"ref": "mosip",
"inputs": {
"core-image-tag": "'"$CORE_COMMIT_HASH"'",
"countryconfig-image-tag": "'"$FARAJALAND_COMMIT_HASH"'",
"stack": "'"$MOSIP_STACK"'",
"dependencies": false,
"reset": "true"
}
}'
wait-for-completion:
needs: trigger
runs-on: ubuntu-latest
steps:
- name: Get run_id of triggered workflow
id: get-run-id
env:
GITHUB_TOKEN: ${{ secrets.OLLIE_BOT_GITHUB_TOKEN }}
run: |
# Wait for workflow to be created
sleep 10
RUN_ID=$(curl -H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/opencrvs/e2e/actions/workflows/deploy.yml/runs?branch=mosip&per_page=1" \
| jq -r '.workflow_runs[0].id')
echo "run_id=$RUN_ID" >> $GITHUB_OUTPUT
- name: Wait for E2E Workflow Completion
uses: actions/github-script@v7
env:
STACK_NAME: ${{ needs.trigger.outputs.stack }}
with:
github-token: ${{ secrets.OLLIE_BOT_GITHUB_TOKEN }}
script: |
const owner = 'opencrvs';
const repo = 'e2e';
const runId = ${{ steps.get-run-id.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');
}
}
- name: Add comment to PR
uses: actions/github-script@v7
with:
script: |
const commentBody = `Your environment is deployed to https://${process.env.STACK_NAME}.opencrvs.dev\n\nIf there is a problem, check the [opencrvs/e2e deploy-workflow](https://github.com/opencrvs/e2e/actions/workflows/deploy.yml)`;
await github.rest.issues.createComment({
owner,
repo,
issue_number: ${{ github.event.pull_request.number }},
body: commentBody
});