-
Notifications
You must be signed in to change notification settings - Fork 102
400 lines (359 loc) · 14 KB
/
ci.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# This is a basic workflow to help in CI
name: Polycube CI
# Controls when the action will run. Triggers the workflow on schedule events
on:
push:
branches:
- master
tags:
- "v*"
paths-ignore:
- 'Documentation/**'
pull_request_target:
types:
- opened
- reopened
- synchronize
paths-ignore:
- 'Documentation/**'
env:
app-container: polycubed
app-registry: polycubenets
jobs:
# This job deal with retrieving variables for workflow configuration
configure:
name: Preliminary configuration
runs-on: ubuntu-latest
outputs:
ref: ${{ steps.configure.outputs.ref }}
repo: ${{ steps.configure.outputs.repo }}
state: ${{ steps.configure.outputs.state }}
version: ${{ steps.version.outputs.version }}
build-matrix: ${{ steps.setup-build-matrix.outputs.build-matrix }}
steps:
- name: If tag pushed get the version
id: version
run: echo "::set-output name=version::${GITHUB_REF/refs\/tags\//}"
if: |
github.event_name == 'push' &&
github.event.base_ref == 'refs/heads/master' &&
startsWith(github.ref, 'refs/tags/v')
- name: Configure ref and state
id: configure
run: |
# If the event is a pull request the repo to checkout is the PR repo, otherwise
# we checkout the current repo
if [[ "${{ github.event_name }}" == "pull_request_target" ]]; then
echo "::set-output name=ref::${{ github.event.pull_request.head.sha }}"
echo "::set-output name=repo::${{ github.event.pull_request.head.repo.full_name }}"
echo "::set-output name=state::dev"
elif [[ "${{ steps.version.outputs.version }}" != "" ]]; then
echo "::set-output name=ref::${{ steps.version.outputs.version }}"
echo "::set-output name=repo::${{ github.repository }}"
echo "::set-output name=state::release"
else
echo "::set-output name=ref::${{ github.sha }}"
echo "::set-output name=repo::${{ github.repository }}"
echo "::set-output name=state::master"
fi
- name: Setup matrix combinations
id: setup-build-matrix
run: |
if [[ "${{ steps.configure.outputs.state }}" == "dev" ]]; then
BUILD_MATRIX='
{"mode": "default", "name": "polycube-pr"},
'
else
BUILD_MATRIX='
{"mode": "default", "name": "polycube"},
{"mode": "pcn-k8s", "name": "polycube-k8s"},
{"mode": "pcn-iptables", "name": "polycube-iptables"},
'
fi
echo ::set-output name=build-matrix::{\"include\":[$BUILD_MATRIX]}
start-notify:
name: Start build notification with Slack
runs-on: ubuntu-latest
needs: [configure]
steps:
- name: Notify on slack that CI started
uses: 8398a7/action-slack@v3
with:
status: custom
fields: workflow,commit,repo,ref
custom_payload: |
{
"attachments":[
{
"title":`${process.env.AS_WORKFLOW}`,
"text": "All polycube builds started",
"fields":[{
"title":"Check suite",
"value":"https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
},
{
"title":"Git reference",
"value":`${process.env.AS_REPO}@${process.env.AS_REF}`,
},
{
"title":"Commit",
"value":`${process.env.AS_COMMIT}`,
}]
}]
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
if: always() # Pick up events even if the job fails or is canceled.
# This job deals with building the application
build:
name: Build & Push Polycube
# The type of runner that the job will run on
runs-on: ubuntu-latest
needs: [configure]
outputs:
default: ${{ steps.save.outputs.default }}
k8s: ${{ steps.save.outputs.pcn-k8s }}
iptables: ${{ steps.save.outputs.pcn-iptables }}
# Build strategies
strategy:
matrix: ${{ fromJson(needs.configure.outputs.build-matrix) }}
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository (default master)
- uses: actions/checkout@v2
with:
submodules: true
persist-credentials: false
ref: ${{ needs.configure.outputs.ref }}
repository: "${{ needs.configure.outputs.repo }}"
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v1
- name: Cache Docker layers
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Repo setup
id: setup
run: |
repo=${{ env.app-registry }}/${{ matrix.name }}
if [[ "${{ needs.configure.outputs.state }}" == "dev" ]]; then
echo "::set-output name=repo-tags::$repo:${{ needs.configure.outputs.ref }}"
else
echo "::set-output name=repo-tags::$repo:${{ needs.configure.outputs.ref }},$repo:latest"
fi
- name: Docker login
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}
- name: Build and push
id: docker_build
uses: docker/build-push-action@v2
with:
context: ./
file: ./Dockerfile
builder: ${{ steps.buildx.outputs.name }}
build-args: |
DEFAULT_MODE=${{ matrix.mode }}
push: true
tags: ${{ steps.setup.outputs.repo-tags }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache
- name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
- name: Save build result in output
id: save
if: always()
run: |
echo "::set-output name=${{ matrix.mode }}::${{ job.status }}"
# This job deals with tests
test:
name: Test Polycube
runs-on: ubuntu-latest
needs: [build, configure]
if: needs.configure.outputs.state == 'dev'
outputs:
clean: ${{ steps.clean.outputs.default-clean }}
same: ${{ steps.clean.outputs.default-same }}
# Test strategies
strategy:
fail-fast: false
matrix:
test: ['default-clean', 'default-same']
include:
- test: 'default-clean'
name: 'polycube-pr'
mode: default
test-script: run-tests.sh
relaunch-polycubed: true
test-mode: CleanInstance
- test: 'default-same'
name: 'polycube-pr'
mode: default
test-script: run-tests.sh
relaunch-polycubed: false
test-mode: SameInstance
steps:
- uses: actions/checkout@v2
with:
submodules: true
persist-credentials: false
ref: ${{ needs.configure.outputs.ref }}
repository: "${{ needs.configure.outputs.repo }}"
- name: Setup Python
uses: actions/setup-python@v1
- name: Setup VirtualEnv
run: python3 -m pip install --user virtualenv
- name: Setup JUnitParser
run: python3 -m pip install --user junitparser
- name: Setup XMLToDict
run: python3 -m pip install --user xmltodict
# Install additional tools that are required in the testing phase
# - nmap: some tests use 'nping', which is included in this package
- name: Setup additional testing tools
run: |
sudo apt-get update
sudo apt-get install nmap
- name: Docker login with bot credentials
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}
- name: Prepare tests log directory
run: sudo mkdir -p /var/log/polycube
- name: Set OS env
run: |
echo "KILL_COMMAND=docker stop ${{ env.app-container }}" >> $GITHUB_ENV
echo "polycubed=docker run -d --name ${{ env.app-container }} --rm --privileged \
--pid=host --cap-add ALL --network host \
-v /proc:/host/proc -v /lib/modules:/lib/modules:ro -v /var/run/netns/:/var/run/netns:rw \
-v /usr/src:/usr/src:ro -v /etc/localtime:/etc/localtime:ro \
${{ env.app-registry }}/${{ matrix.name }}:${{ needs.configure.outputs.ref }}" >> $GITHUB_ENV
- name: Extract from Docker image
run: |
$polycubed /bin/sleep infinity
./CI/extract_from_docker_image.sh ${{ env.app-container }}
$KILL_COMMAND
- name: Run tests
run: |
cd tests/
sudo ./${{ matrix.test-script }} ${{ matrix.relaunch-polycubed }} || true
export LC_ALL=C
python3 -m virtualenv venv -p python3
. venv/bin/activate
python3 -m pip install -r ./converter/requirements.txt
export TEST_RESULTS=`ls -1 test_results_*`
python3 ./converter/to_junit.py ${{ matrix.test-mode }}
- name: Upload tests result
if: always()
uses: actions/upload-artifact@v2.2.0
with:
name: test_results_${{ matrix.test }}
path: ./tests/output.xml
- name: Check job status
id: check
if: always()
run: python3 ./tests/check_tests.py ./tests/output.xml
- name: Save tests result in output and clean
id: clean
if: always()
run: |
echo "::set-output name=${{ matrix.test }}::${{ steps.check.outcome }}"
./CI/clean_slave.sh
end-notify:
name: End build notification with Slack
runs-on: ubuntu-latest
needs: [configure, build, test]
if: always()
steps:
- name: Build result check
id: result
if: |
needs.build.outputs.default == 'success' &&
((needs.configure.outputs.state == 'dev' && needs.test.outputs.clean == 'success' && needs.test.outputs.same == 'success') ||
(needs.configure.outputs.state != 'dev' && needs.build.outputs.k8s == 'success' && needs.build.outputs.iptables == 'success'))
run: echo "::set-output name=status::success"
- name: Notify on slack the build outcome
uses: 8398a7/action-slack@v3
with:
status: custom
fields: workflow,commit,repo,ref
custom_payload: |
{
"attachments":[
{
"title":`${process.env.AS_WORKFLOW}`,
"text": "Build finished",
"color": '${{ steps.result.outputs.status }}' === 'success' ? 'good' : 'danger',
"fields":[{
"title":"Check suite",
"value":"https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
},
{
"title":"Git reference",
"value":`${process.env.AS_REPO}@${process.env.AS_REF}`,
},
{
"title":"Commit",
"value":`${process.env.AS_COMMIT}`,
},
{
"title":"Build modes",
"value": '${{ needs.configure.outputs.state }}' == 'dev' ? "*default* ${{ needs.build.outputs.default }}\n*pcn-k8s* skipped\n*pcn-iptables* skipped\n" : "*default* ${{ needs.build.outputs.default }}\n*pcn-k8s* ${{ needs.build.outputs.k8s }}\n*pcn-iptables* ${{ needs.build.outputs.iptables }}\n",
"short": "true"
},
{
"title":"Test modes",
"value": '${{ needs.configure.outputs.state }}' != 'dev' ? "skipped" : "*default clean* ${{ needs.test.outputs.clean }}\n*default same* ${{ needs.test.outputs.same }}",
"short": "true"
}
]
}]
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
if: always() # Pick up events even if the job fails or is canceled.
release:
name: Release Polycube
runs-on: ubuntu-latest
needs: [build, configure]
if: needs.configure.outputs.state == 'release'
continue-on-error: true
steps:
- name: Checkout
uses: actions/checkout@v2
with:
# The changelog generation requires the entire history
fetch-depth: 0
submodules: true
persist-credentials: false
- name: Get the latest Polycube release
uses: pozetroninc/github-action-get-latest-release@v0.5.0
id: last-release
with:
repository: ${{ github.repository }}
- name: Generate changelog
id: changelog
uses: metcalfc/changelog-generator@v0.4.4
with:
myToken: ${{ secrets.GITHUB_TOKEN }}
- name: Save the CHANGELOG as a file
run: |
echo "${{ steps.changelog.outputs.changelog }}" > ./output.md
sed '/pull request/!d' output.md > output_pr.md
sed 's/\[\]/\[view commit\]/' output_pr.md > CHANGELOG.md
sed -i "1s/.*/## Merged PR since tag ${{ steps.last-release.outputs.release }}/" ./CHANGELOG.md
- name: Create the release
uses: actions/create-release@v1
with:
tag_name: ${{ needs.build.outputs.version }}
release_name: Release ${{ needs.build.outputs.version }}
body_path: ./CHANGELOG.md
draft: false
prerelease: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}