-
Notifications
You must be signed in to change notification settings - Fork 0
321 lines (291 loc) · 10.3 KB
/
deploy.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
name: Deploy (development)
run-name: Deploy ${{ inputs.stack }} with reset=${{ inputs.reset }} core=${{ inputs.core-image-tag }} country config=${{ inputs.countryconfig-image-tag }}
on:
workflow_call:
inputs:
core-image-tag:
type: string
description: Core DockerHub image tag
required: true
default: 'v1.4.1'
countryconfig-image-tag:
type: string
description: Your Country Config DockerHub image tag
required: true
stack:
type: string
description: Stack name
required: true
dependencies:
type: string
description: Redeploy dependencies
default: 'false'
required: false
reset:
type: string
description: Reset the environment
default: 'false'
required: false
workflow_dispatch:
inputs:
core-image-tag:
description: Core DockerHub image tag
required: true
default: 'v1.6.0'
countryconfig-image-tag:
description: Your Country Config DockerHub image tag
required: true
stack:
type: string
description: Stack name
required: true
dependencies:
type: boolean
description: Redeploy dependencies
default: false
required: false
reset:
type: boolean
description: Reset the environment
default: false
jobs:
download-images:
environment: ${{ inputs.stack }}
runs-on: ubuntu-22.04
timeout-minutes: 60
steps:
- name: Check if vars.BLOCK_NEW_DEPLOYMENTS is set to true
run: |
if [[ "${{ vars.BLOCK_NEW_DEPLOYMENTS }}" == "true" ]]; then
echo "BLOCK_NEW_DEPLOYMENTS in Github environment settings is set to true, exiting..."
echo "Set the variable to anything other than 'true' and redeploy"
echo "https://github.com/opencrvs/e2e/settings/environments"
exit 1
fi
- name: Clone country config resource package
uses: actions/checkout@v3
with:
fetch-depth: 1
path: './${{ github.event.repository.name }}'
- name: Checkout country branch
run: |
cd ${{ github.event.repository.name }}
cd ../
- name: Read known hosts
run: |
cd ${{ github.event.repository.name }}
echo "KNOWN_HOSTS<<EOF" >> $GITHUB_ENV
sed -i -e '$a\' ./infrastructure/known-hosts
cat ./infrastructure/known-hosts >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
- name: Install SSH Key
uses: shimataro/ssh-key-action@v2
with:
key: ${{ secrets.SSH_KEY }}
known_hosts: ${{ env.KNOWN_HOSTS }}
- name: Unset KNOWN_HOSTS variable
run: |
echo "KNOWN_HOSTS=" >> $GITHUB_ENV
- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}
- name: Wait for images to be available
run: |
while true; do
if docker manifest inspect opencrvs/ocrvs-auth:${{ inputs.core-image-tag }}; then
break
fi
sleep 10
done
while true; do
if docker manifest inspect ${{ vars.DOCKERHUB_ACCOUNT }}/${{ secrets.DOCKERHUB_REPO }}:${{ inputs.countryconfig-image-tag }}; then
break
fi
sleep 10
done
- name: Export all secrets and environment variables
run: |
cd ./${{ github.event.repository.name }}
SECRETS_JSON_WITH_NEWLINES=$(cat<<EOF
${{ toJSON(secrets) }}
EOF)
#
# Secrets & variables with newlines are filtered out automatically
# This includes SSH_KEY and KNOWN_HOSTS
#
while IFS= read -r secret; do
echo "$secret" >> .env.${{ inputs.stack }}
done < <(
jq -r '
to_entries |
map(
select(.value | test("\n") | not) |
"\(.key)=\"\(.value)\""
) |
.[]' <<< "$SECRETS_JSON_WITH_NEWLINES"
)
VARS_JSON_WITH_NEWLINES=$(cat<<EOF
${{ toJSON(vars) }}
EOF)
while IFS= read -r var; do
echo "$var" >> .env.${{ inputs.stack }}
done < <(
jq -r '
to_entries |
map(
select(.value | test("\n") | not) |
"\(.key)=\"\(.value)\""
) |
.[]' <<< "$VARS_JSON_WITH_NEWLINES"
)
- name: Download images for ${{ inputs.stack }}
run: |
cd ./${{ github.event.repository.name }}
bash infrastructure/deployment/download-images.sh \
--clear_data=no \
--environment=${{ inputs.stack }} \
--host=${{ vars.DOMAIN }} \
--stack=${{ inputs.stack }} \
--update-dependencies=${{ inputs.dependencies }} \
--ssh_host=${{ vars.SSH_HOST || secrets.SSH_HOST }} \
--ssh_port=${{ vars.SSH_PORT || secrets.SSH_PORT }} \
--ssh_user=${{ secrets.SSH_USER }} \
--version=${{ inputs.core-image-tag }} \
--country_config_version=${{ inputs.countryconfig-image-tag }} \
--replicas=${{ vars.REPLICAS }}
deploy:
environment: ${{ inputs.stack }}
needs: download-images
runs-on: ubuntu-22.04
concurrency:
group: deploy
cancel-in-progress: false
outputs:
outcome: ${{ steps.deploy.outcome }}
timeout-minutes: 60
steps:
- name: Check if vars.BLOCK_NEW_DEPLOYMENTS is set to true
run: |
if [[ "${{ vars.BLOCK_NEW_DEPLOYMENTS }}" == "true" ]]; then
echo "BLOCK_NEW_DEPLOYMENTS in Github environment settings is set to true, exiting..."
echo "Set the variable to anything other than 'true' and redeploy"
echo "https://github.com/opencrvs/e2e/settings/environments"
exit 1
fi
- name: Generate summary
run: |
echo "Deploying environment to https://${{ inputs.stack }}.${{ vars.DOMAIN }}" >> $GITHUB_STEP_SUMMARY
echo "Core image tag: ${{ inputs.core-image-tag }}" >> $GITHUB_STEP_SUMMARY
echo "Country config image tag: ${{ inputs.countryconfig-image-tag }}" >> $GITHUB_STEP_SUMMARY
echo "Reset environment: ${{ inputs.reset }}" >> $GITHUB_STEP_SUMMARY
echo "Redeploy dependencies: ${{ inputs.dependencies }}" >> $GITHUB_STEP_SUMMARY
- name: Clone core
uses: actions/checkout@v3
with:
fetch-depth: 0
repository: 'opencrvs/opencrvs-core'
path: './opencrvs-core'
- name: Clone country config resource package
uses: actions/checkout@v3
with:
fetch-depth: 1
path: './${{ github.event.repository.name }}'
- name: Checkout country branch
run: |
cd ${{ github.event.repository.name }}
cd ../
- name: Checkout core branch
run: |
cd opencrvs-core
git checkout ${{ inputs.core-image-tag }}
- name: Read known hosts
run: |
cd ${{ github.event.repository.name }}
echo "KNOWN_HOSTS<<EOF" >> $GITHUB_ENV
sed -i -e '$a\' ./infrastructure/known-hosts
cat ./infrastructure/known-hosts >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
- name: Install SSH Key
uses: shimataro/ssh-key-action@v2
with:
key: ${{ secrets.SSH_KEY }}
known_hosts: ${{ env.KNOWN_HOSTS }}
- name: Unset KNOWN_HOSTS variable
run: |
echo "KNOWN_HOSTS=" >> $GITHUB_ENV
- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}
- name: Export all secrets and environment variables
run: |
cd ./${{ github.event.repository.name }}
SECRETS_JSON_WITH_NEWLINES=$(cat<<EOF
${{ toJSON(secrets) }}
EOF)
#
# Secrets & variables with newlines are filtered out automatically
# This includes SSH_KEY and KNOWN_HOSTS
#
while IFS= read -r secret; do
echo "$secret" >> .env.${{ inputs.stack }}
done < <(
jq -r '
to_entries |
map(
select(.value | test("\n") | not) |
"\(.key)=\"\(.value)\""
) |
.[]' <<< "$SECRETS_JSON_WITH_NEWLINES"
)
VARS_JSON_WITH_NEWLINES=$(cat<<EOF
${{ toJSON(vars) }}
EOF)
while IFS= read -r var; do
echo "$var" >> .env.${{ inputs.stack }}
done < <(
jq -r '
to_entries |
map(
select(.value | test("\n") | not) |
"\(.key)=\"\(.value)\""
) |
.[]' <<< "$VARS_JSON_WITH_NEWLINES"
)
- name: Deploy to ${{ inputs.stack }}
id: deploy
run: |
cd ./${{ github.event.repository.name }}
yarn install
yarn deploy \
--clear_data=no \
--environment=${{ inputs.stack }} \
--host=${{ vars.DOMAIN }} \
--stack=${{ inputs.stack }} \
--update-dependencies=${{ inputs.dependencies }} \
--ssh_host=${{ vars.SSH_HOST || secrets.SSH_HOST }} \
--ssh_port=${{ vars.SSH_PORT || secrets.SSH_PORT }} \
--ssh_user=${{ secrets.SSH_USER }} \
--version=${{ inputs.core-image-tag }} \
--country_config_version=${{ inputs.countryconfig-image-tag }} \
--replicas=${{ vars.REPLICAS }}
reset:
needs: deploy
if: ${{ inputs.reset == 'true' && needs.deploy.outputs.outcome == 'success' }}
uses: ./.github/workflows/clear-environment.yml
with:
environment: ${{ inputs.stack }}
stack: ${{ inputs.stack }}
secrets: inherit
seed-data:
needs: reset
if: ${{ inputs.reset == 'true' && needs.reset.outputs.outcome == 'success' }}
uses: ./.github/workflows/seed-data.yml
with:
environment: ${{ inputs.stack }}
core-image-tag: ${{ inputs.core-image-tag }}
stack: ${{ inputs.stack }}
secrets: inherit