-
Notifications
You must be signed in to change notification settings - Fork 295
352 lines (317 loc) · 13.4 KB
/
testserver.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
name: Deploy to Testserver
on:
pull_request:
types: [labeled]
concurrency: test-servers
env:
RAW_URL: https://raw.githubusercontent.com/${{ github.repository }}/${{ github.sha }}
jobs:
# Get an up to date version of the label list. github.event.pull_request.labels seems to sometimes be outdated
# if the run was waiting for a while, which can cause duplicate deployments
get-labels:
runs-on: ubuntu-latest
outputs:
labels: ${{ steps.get-labels.outputs.result }}
steps:
- name: Get PR labels
id: get-labels
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const response = await github.rest.issues.listLabelsOnIssue({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number
})
const labels = response.data
return labels.map(label => label.name)
# Check that the build job has run successfully before deploying
check-build-status:
needs: [ get-labels ]
runs-on: ubuntu-latest
# Only run workflow if the added label is a deploy label
if: contains(needs.get-labels.outputs.labels, 'deploy:artemis-test')
steps:
- name: Get latest successful build for branch
id: check_build
uses: octokit/request-action@v2.x
with:
route: GET /repos/${{ github.repository }}/actions/workflows/build.yml/runs?event=pull_request&status=success&head_sha=${{ github.event.pull_request.head.sha }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Remove deployment-error label if new run is started
- uses: actions-ecosystem/action-remove-labels@v1
if: fromJSON(steps.check_build.outputs.data).total_count > 0
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
labels: |
deployment-error
# In case of invalid build status, remove deploy labels
- uses: actions-ecosystem/action-remove-labels@v1
if: fromJSON(steps.check_build.outputs.data).total_count == 0
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
labels: |
deploy:artemis-test1
deploy:artemis-test2
deploy:artemis-test3
deploy:artemis-test4
deploy:artemis-test5
deploy:artemis-test6
deploy:artemis-test7
deploy:artemis-test8
deploy:artemis-test9
deploy:artemis-test10
- name: Check if latest push had successful build
if: fromJSON(steps.check_build.outputs.data).total_count == 0
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: '### ⚠️ Unable to deploy to test servers ⚠️\nThe docker build needs to run through before deploying.'
})
core.setFailed('The build needs to run through first. Please wait for the build to finish and then try again.')
# Check which test server to deploy to based on the label
filter-matrix:
needs: [ get-labels, check-build-status ]
runs-on: ubuntu-latest
strategy:
matrix:
include:
# Commented out environments are not yet available and will be enabled in the future
- environment: artemis-test1.artemis.cit.tum.de
label-identifier: artemis-test1
url: https://artemis-test1.artemis.cit.tum.de
user: deployment
hosts: artemis-test1.artemis.cit.tum.de
folder: /opt/artemis
- environment: artemis-test2.artemis.cit.tum.de
label-identifier: artemis-test2
url: https://artemis-test2.artemis.cit.tum.de
user: deployment
hosts: artemis-test2.artemis.cit.tum.de
folder: /opt/artemis
- environment: artemis-test3.artemis.cit.tum.de
label-identifier: artemis-test3
url: https://artemis-test3.artemis.cit.tum.de
user: deployment
hosts: artemis-test3.artemis.cit.tum.de
folder: /opt/artemis
- environment: artemis-test4.artemis.cit.tum.de
label-identifier: artemis-test4
url: https://artemis-test4.artemis.cit.tum.de
user: deployment
hosts: artemis-test4.artemis.cit.tum.de
folder: /opt/artemis
- environment: artemis-test5.artemis.cit.tum.de
label-identifier: artemis-test5
url: https://artemis-test5.artemis.cit.tum.de
user: deployment
hosts: artemis-test5.artemis.cit.tum.de
folder: /opt/artemis
- environment: artemis-test6.artemis.cit.tum.de
label-identifier: artemis-test6
url: https://artemis-test6.artemis.cit.tum.de
user: deployment
hosts: artemis-test6.artemis.cit.tum.de
folder: /opt/artemis
host_keys: |
#- environment: artemis-test7.artemis.cit.tum.de
# label-identifier: artemis-test7
# url: https://artemis-test7.artemis.cit.tum.de
# user: deployment
# hosts: artemis-test7.artemis.cit.tum.de
# folder: /opt/artemis
#- environment: artemis-test8.artemis.cit.tum.de
# label-identifier: artemis-test8
# url: https://artemis-test8.artemis.cit.tum.de
# user: deployment
# hosts: artemis-test8.artemis.cit.tum.de
# folder: /opt/artemis
- environment: artemis-test9.artemis.cit.tum.de
label-identifier: artemis-test9
url: https://artemis-test9.artemis.cit.tum.de
user: deployment
hosts: artemis-test9.artemis.cit.tum.de
folder: /opt/artemis
host_keys: |
#- environment: artemis-test10.artemis.cit.tum.de
# label-identifier: artemis-test10
# url: https://artemis-test10.artemis.cit.tum.de
# user: deployment
# hosts: artemis-test10.artemis.cit.tum.de
# folder: /opt/artemis
outputs:
TS1: ${{ steps.filter.outputs.artemis-test1 || '' }}
TS2: ${{ steps.filter.outputs.artemis-test2 || '' }}
TS3: ${{ steps.filter.outputs.artemis-test3 || '' }}
TS4: ${{ steps.filter.outputs.artemis-test4 || '' }}
TS5: ${{ steps.filter.outputs.artemis-test5 || '' }}
TS6: ${{ steps.filter.outputs.artemis-test6 || '' }}
#TS7: ${{ steps.filter.outputs.artemis-test7 || '' }}
#TS8: ${{ steps.filter.outputs.artemis-test8 || '' }}
TS9: ${{ steps.filter.outputs.artemis-test9 || '' }}
#TS10: ${{ steps.filter.outputs.artemis-test10 || '' }}
steps:
- run: |
echo "$DEPLOY_LABEL"
echo '${{ contains(fromJSON(needs.get-labels.outputs.labels), format('deploy:{0}', matrix.label-identifier)) }}'
- id: filter
env:
MATRIX_JSON: ${{ toJSON(matrix) }}
if: ${{ contains(fromJSON(needs.get-labels.outputs.labels), format('deploy:{0}', matrix.label-identifier)) }}
run: |
MATRIX_JSON=${MATRIX_JSON//$'\n'/}
echo "${{ matrix.label-identifier }}=$MATRIX_JSON" >> $GITHUB_OUTPUT
# Process the output of the filter step to create a valid matrix for the deploy step
process-matrix:
needs: [ filter-matrix ]
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.process.outputs.matrix }}
steps:
- id: process
env:
MATRIX_JSON: ${{ toJSON(needs.filter-matrix.outputs.*) }}
run: |
MATRIX_JSON=${MATRIX_JSON//$'\n'/}
MATRIX_JSON=${MATRIX_JSON//$'"{'/'{'}
MATRIX_JSON=${MATRIX_JSON//$'}"'/'}'}
MATRIX_JSON=${MATRIX_JSON//$'\\"'/'"'}
echo "$MATRIX_JSON"
echo "matrix=$MATRIX_JSON" >> $GITHUB_OUTPUT
# Deploy to the test servers
deploy:
needs: [ process-matrix ]
runs-on: ubuntu-latest
concurrency: test-servers-deploy
strategy:
fail-fast: false
matrix:
include: ${{ fromJSON(needs.process-matrix.outputs.matrix) }}
environment:
name: ${{ matrix.environment }}
url: ${{ matrix.url }}
env:
DEPLOYMENT_USER: ${{ matrix.user }}
DEPLOYMENT_HOSTS: ${{ matrix.hosts }}
DEPLOYMENT_FOLDER: ${{ matrix.folder }}
GATEWAY_USER: "jump"
GATEWAY_HOST: "gateway.artemis.in.tum.de:2010"
GATEWAY_HOST_PUBLIC_KEY: "[gateway.artemis.in.tum.de]:2010 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKtTLiKRILjKZ+Qg4ReWKsG7mLDXkzHfeY5nalSQUNQ4"
steps:
- uses: actions-ecosystem/action-remove-labels@v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
labels: |
deploy:${{ matrix.label-identifier }}
- name: Check "lock:${{ matrix.environment }}" label
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const opts = github.rest.issues.listForRepo.endpoint.merge({
owner: context.repo.owner,
repo: context.repo.repo,
labels: ['lock:${{ matrix.label-identifier }}']
})
const issues = await github.paginate(opts)
if (issues.length == 1 && (!context.issue || issues[0].number != context.issue.number)) {
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `#### ⚠️ Unable to deploy to test servers ⚠️\nTestserver "${{ matrix.environment }}" is already in use by PR #${issues[0].number}.`
})
core.setFailed(`Testserver "${{ matrix.environment }}" is already in use by PR #${issues[0].number}.`);
} else if (issues.length > 1) {
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: '#### ⚠️ Unable to deploy to test servers ⚠️\nTestserver "${{ matrix.environment }}" is already in use by multiple PRs. Check PRs with label "lock:${{ matrix.label-identifier }}"!'
})
core.setFailed('Testserver "${{ matrix.environment }}" is already in use by multiple PRs. Check PRs with label "lock:${{ matrix.label-identifier }}"!');
}
- name: Compute Tag
uses: actions/github-script@v7
id: compute-tag
with:
result-encoding: string
script: |
if (context.eventName === "pull_request") {
return "pr-" + context.issue.number;
}
if (context.eventName === "release") {
return "latest";
}
if (context.eventName === "push") {
if (context.ref.startsWith("refs/tags/")) {
return context.ref.slice(10);
}
if (context.ref === "refs/heads/develop") {
return "develop";
}
}
return "FALSE";
# Download artemis-server-cli from GH without cloning the Repo
- name: Fetch Artemis CLI
run: |
wget ${{ env.RAW_URL }}/artemis-server-cli
chmod +x artemis-server-cli
# Configure SSH Key
- name: Setup SSH Keys and known_hosts
env:
SSH_AUTH_SOCK: /tmp/ssh_agent.sock
GATEWAY_SSH_KEY: "${{ secrets.DEPLOYMENT_GATEWAY_SSH_KEY }}"
DEPLOYMENT_SSH_KEY: "${{ secrets.DEPLOYMENT_SSH_KEY }}"
run: |
mkdir -p ~/.ssh
ssh-agent -a $SSH_AUTH_SOCK > /dev/null
ssh-add - <<< $GATEWAY_SSH_KEY
ssh-add - <<< $DEPLOYMENT_SSH_KEY
cat - <<< $GATEWAY_HOST_PUBLIC_KEY >> ~/.ssh/known_hosts
- name: Deploy Artemis with Docker
env:
SSH_AUTH_SOCK: /tmp/ssh_agent.sock
TAG: ${{ steps.compute-tag.outputs.result }}
run: |
for host in $DEPLOYMENT_HOSTS; do
./artemis-server-cli docker-deploy "$DEPLOYMENT_USER@$host" -g "$GATEWAY_USER@$GATEWAY_HOST" -t $TAG -b $GITHUB_HEAD_REF -d $DEPLOYMENT_FOLDER -y
done
- name: Add "lock:${{ matrix.environment }}" label
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
if (context.issue && context.issue.number) {
await github.rest.issues.addLabels({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: ['lock:${{ matrix.label-identifier }}']
})
}
- name: Update badge
uses: RubbaBoy/BYOB@v1.3.0
with:
NAME: ${{ matrix.label-identifier }}
LABEL: ${{ matrix.environment }}
STATUS: ${{ github.event.pull_request.head.ref }}
COLOR: red
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Check that the build job has run successfully before deploying
add-error-label:
needs: [ get-labels, check-build-status, filter-matrix, process-matrix, deploy ]
runs-on: ubuntu-latest
if: ${{ failure() }}
steps:
- name: Add error label
uses: actions-ecosystem/action-add-labels@v1
with:
labels: deployment-error