-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.gitlab-ci.yml
485 lines (374 loc) · 17 KB
/
.gitlab-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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# Note: expand yaml
# yaml merge-expand .gitlab-ci.yml _expandyml && cat _expandyml
#
# GITLAB LINTER
# https://gitlab.kitware.com/viame/bioharn/-/ci/lint
# This CI file has 4 types of jobs:
# (1) in the build stage we we build the wheels on a manylinux docker image
# (2) then in the test stage we install the wheels, run unit tests, and measure coverage
# (3) after testing we sign the wheels with the CI's GPG key
# (4) finally if we are on the release branch we will push the signed wheels to pypi
# Abuse YAML notation to make a heredoc. This will be ignored by the CI.
.__heredoc__: &__heredoc__
- |
__doc__="""
============
INSTRUCTIONS
============
This file was designed to be used as a template. You can adapt it to
new projects with a few simple changes. Namely perform the following
search and replaces.
```bash
cat .gitlab-ci.yml | \
sed 's|viame|<YOUR-GROUP>|g' | \
sed 's|bioharn|<YOUR-REPO>|g' | \
sed 's|travis-ci-Erotemic|<YOUR-GPG-ID>|g' | \
sed 's|CI_KITWARE_SECRET|<YOUR_CI_SECRET>|g' | \
sed 's|gitlab.kitware.com|gitlab.your-instance.com|g' | \
tee /tmp/repl && colordiff .gitlab-ci.yml /tmp/repl
```
To use this script you need the following configurations on your gitlab instance.
NOTES
-----
* This script will require matainence for new releases of Python
* Make sure you add Runners to your project
https://gitlab.kitware.com/viame/bioharn/-/settings/ci_cd
in Runners-> Shared Runners
and Runners-> Available specific runners
GITLAB SECRETS
--------------
Almost all of the stages in this pipeline can be performed on a local
machine (making it much easier to debug) as well as the gitlab-ci
machine. However, there are a handeful of required environment
variables which will contain sensitive information. These variables are
* TWINE_USERNAME - this is your pypi username
twine info is only needed if you want to automatically publish to pypi
* TWINE_PASSWORD - this is your pypi password
* CI_KITWARE_SECRET - We will use this as a secret key to encrypt/decrypt gpg secrets
This is only needed if you want to automatically sign published
wheels with a gpg key.
* GIT_PUSH_TOKEN -
This is only needed if you want to automatically git-tag release branches.
Create a GIT_PUSH_TOKEN in User->Settings->Tokens, give it api and write permissions
see: https://gitlab.kitware.com/profile/personal_access_tokens
Then add this as a secret variable here: https://gitlab.kitware.com/viame/bioharn/-/settings/ci_cd
Note the value of GIT_PUSH_TOKEN will look something like: "{token-name}:{token-password}"
For instance it may look like this: "git-push-token:62zutpzqga6tvrhklkdjqm"
References:
https://stackoverflow.com/questions/51465858/how-do-you-push-to-a-gitlab-repo-using-a-gitlab-ci-job
These should be added as secret variables in your Gitlab->CI->Variables settings.
Before you do this ensure that your master and release branch are marked as protected
and only editable by people that you trust.
https://gitlab.kitware.com/viame/bioharn/-/settings/repository#js-protected-branches-settings
WARNING: If an untrusted actor gains the ability to write to a
protected branch, then they will be able to exfiltrate your secrets.
Once you have protected the master and release branch, can add these
variables as the following url
https://gitlab.kitware.com/viame/bioharn/-/settings/ci_cd
WARNING: These variables contain secret information. Ensure that these
the protected and masked settings are enabled when you create them.
ENCRYPTING SECRETS
------------------
The following script demonstrates how to securely encrypt a secret GPG key. It is assumed that you have
a file secret_loader.sh that looks like this
```bash
source secretfile
```
and then a secretfile that looks like this
```bash
#!/bin/bash
echo /some/secret/file
export TWINE_USERNAME=<pypi-username>
export TWINE_PASSWORD=<pypi-password>
export CI_KITWARE_SECRET="<a-very-long-secret-string>"
export GIT_PUSH_TOKEN='git-push-token:<token-password>'
```
You should also make a secret_unloader.sh that points to a script that
unloads these secret variables from the environment.
Given this file-structure setup, you can then run the following
commands verbatim. Alternatively just populate the environment
variables and run line-by-line without creating the secret
loader/unloader scripts.
```bash
# THIS IS NOT EXECUTE ON THE CI, THIS IS FOR DEVELOPER REFERENCE
# ON HOW THE ENCRYPTED GPG KEYS ARE SETUP.
# Load or generate secrets
source $(secret_loader.sh)
echo $CI_KITWARE_SECRET
echo $TWINE_USERNAME
# ADD RELEVANT VARIABLES TO GITLAB SECRET VARIABLES
# https://gitlab.kitware.com/viame/bioharn/-/settings/ci_cd
# Note that it is important to make sure that these variables are
# only decrpyted on protected branches by selecting the protected
# and masked option. Also make sure you have master and release
# branches protected.
# https://gitlab.kitware.com/viame/bioharn/-/settings/repository#js-protected-branches-settings
# HOW TO ENCRYPT YOUR SECRET GPG KEY
IDENTIFIER="travis-ci-Erotemic"
GPG_KEYID=$(gpg --list-keys --keyid-format LONG "$IDENTIFIER" | head -n 2 | tail -n 1 | awk '{print $1}' | tail -c 9)
echo "GPG_KEYID = $GPG_KEYID"
# Export plaintext gpg public keys, private keys, and trust info
mkdir -p dev
gpg --armor --export-secret-keys $GPG_KEYID > dev/gpg/ci_secret_gpg_key.pgp
gpg --armor --export $GPG_KEYID > dev/gpg/ci_public_gpg_key.pgp
gpg --export-ownertrust > dev/gpg/gpg_owner_trust
# Encrypt gpg keys and trust with CI secret
GLKWS=$CI_KITWARE_SECRET openssl enc -aes-256-cbc -pbkdf2 -md SHA512 -pass env:GLKWS -e -a -in dev/gpg/ci_public_gpg_key.pgp > dev/gpg/ci_public_gpg_key.pgp.enc
GLKWS=$CI_KITWARE_SECRET openssl enc -aes-256-cbc -pbkdf2 -md SHA512 -pass env:GLKWS -e -a -in dev/gpg/ci_secret_gpg_key.pgp > dev/gpg/ci_secret_gpg_key.pgp.enc
GLKWS=$CI_KITWARE_SECRET openssl enc -aes-256-cbc -pbkdf2 -md SHA512 -pass env:GLKWS -e -a -in dev/gpg/gpg_owner_trust > dev/gpg/gpg_owner_trust.enc
echo $GPG_KEYID > dev/gpg/public_gpg_key
# Test decrpyt
cat dev/gpg/public_gpg_key
GLKWS=$CI_KITWARE_SECRET openssl enc -aes-256-cbc -pbkdf2 -md SHA512 -pass env:GLKWS -d -a -in dev/gpg/ci_public_gpg_key.pgp.enc
GLKWS=$CI_KITWARE_SECRET openssl enc -aes-256-cbc -pbkdf2 -md SHA512 -pass env:GLKWS -d -a -in dev/gpg/gpg_owner_trust.enc
GLKWS=$CI_KITWARE_SECRET openssl enc -aes-256-cbc -pbkdf2 -md SHA512 -pass env:GLKWS -d -a -in dev/gpg/ci_secret_gpg_key.pgp.enc
source $(secret_unloader.sh)
# Look at what we did, clean up, and add it to git
ls dev/*.enc
rm dev/gpg/gpg_owner_trust dev/*.pgp
git status
git add dev/*.enc
git add dev/gpg/public_gpg_key
```
TEST GIT_PUSH_TOKEN
-------------------
The following script tests if your GIT_PUSH_TOKEN environment variable is correctly setup.
```bash
docker run -it python:3.8 bash
apt update -y && apt install git -y
git clone https://gitlab.kitware.com/viame/bioharn.git
cd bioharn
# do sed twice to handle the case of https clone with and without a read token
git config user.email "[email protected]"
git config user.name "Gitlab-CI"
URL_HOST=$(git remote get-url origin | sed -e 's|https\?://.*@||g' | sed -e 's|https\?://||g')
echo "URL_HOST = $URL_HOST"
git tag "test-tag4"
git push --tags "https://${GIT_PUSH_TOKEN}@${URL_HOST}"
# Cleanup after you verify the tags shows up on the remote
git push --delete origin test-tag4
git tag --delete test-tag4
```
NOTES
-----
Base docker images are stored in the gitlab "container registry" of
https://gitlab.kitware.com/computer-vision/ci-docker repo. Using
these instead of standard images serves two purposes (1) prevent rate
limiting by docker.io and (2) add a simple extension to standard python
containers to include the libgl1-mesa-glx package.
""" # " # hack for vim yml syntax highlighter
stages:
- build
- test
- gpgsign
- deploy
### TEMPLATES ###
# Define common templates using YAML anchors
.common_template: &common_template
tags:
# Tags define which runners will accept which jobs
- docker
- linux
- build
variables:
# Change pip's cache directory to be inside the project directory since we can
# only cache local items.
PIP_CACHE_DIR: "$CI_PROJECT_DIR/mb_work/cache_pip"
except:
# Don't run the pipeline for new tags
- tags
.build_template: &build_template
# Tags define which runners will accept which jobs
<<:
- *common_template
stage:
build
before_script:
- python -V # Print out python version for debugging
script:
- python setup.py bdist_wheel --universal
cache:
paths:
- .cache/pip
artifacts:
paths:
- dist/*.whl
.test_full_template: &test_full_template
# Tags define which runners will accept which jobs
<<:
- *common_template
stage:
test
before_script:
- python -V # Print out python version for debugging
- export PYVER=$(python -c "import sys; print('{}{}'.format(*sys.version_info[0:2]))")
- pip install virtualenv
- virtualenv venv$PYVER
- source venv$PYVER/bin/activate
- pip install pip -U
- pip install pip setuptools -U
- python -V # Print out python version for debugging
- pip install cython scikit-build cmake ninja numpy torch # Hack installation for mmdet (not even sure if possible)
- pip install GDAL --find-links https://girder.github.io/large_image_wheels
- pip install lru-dict
- pip install -r requirements/runtime.txt
- pip install -r requirements/tests.txt
- pip install imgaug --no-deps
- pip install opencv-python-headless
- pip install six scipy Pillow matplotlib scikit-image shapely numba
- pip install .
script:
- ./run_tests.py
cache:
paths:
- .cache/pip
- venv/
# Coverage is a regex that will parse the coverage from the test stdout
coverage: '/TOTAL.+ ([0-9]{1,3}%)/'
.gpgsign_template: &gpgsign_template
<<:
- *common_template
stage:
gpgsign
script:
- export GPG_EXECUTABLE=gpg
- export GPG_KEYID=$(cat dev/gpg/public_gpg_key)
- echo "GPG_KEYID = $GPG_KEYID"
- $GPG_EXECUTABLE --version
- openssl version
- $GPG_EXECUTABLE --list-keys
- $GPG_EXECUTABLE --list-keys
# Decrypt and import GPG Keys / trust
# note CI_KITWARE_SECRET is a protected variables only available on master and release branch
- GLKWS=$CI_KITWARE_SECRET openssl enc -aes-256-cbc -pbkdf2 -md SHA512 -pass env:GLKWS -d -a -in dev/gpg/ci_public_gpg_key.pgp.enc | $GPG_EXECUTABLE --import
- GLKWS=$CI_KITWARE_SECRET openssl enc -aes-256-cbc -pbkdf2 -md SHA512 -pass env:GLKWS -d -a -in dev/gpg/gpg_owner_trust.enc | $GPG_EXECUTABLE --import-ownertrust
- GLKWS=$CI_KITWARE_SECRET openssl enc -aes-256-cbc -pbkdf2 -md SHA512 -pass env:GLKWS -d -a -in dev/gpg/ci_secret_gpg_key.pgp.enc | $GPG_EXECUTABLE --import
- $GPG_EXECUTABLE --list-keys || echo "first one fails for some reason"
- $GPG_EXECUTABLE --list-keys
- pip install requests[security] twine
# The publish script only builds wheels and does gpg signing if TAG_AND_UPLOAD is False
- MB_PYTHON_TAG=$MB_PYTHON_TAG DO_GPG=True GPG_KEYID=$GPG_KEYID TWINE_PASSWORD=$TWINE_PASSWORD TWINE_USERNAME=$TWINE_USERNAME GPG_EXECUTABLE=$GPG_EXECUTABLE DEPLOY_BRANCH=release DO_TAG=False DO_UPLOAD=False ./publish.sh
artifacts:
paths:
- dist/*.asc
- dist/*.tar.gz
- dist/*.whl
only:
refs:
# Gitlab will only expose protected variables on protected branches
# (which I've set to be master and release), so only run this stage
# there.
- master
- release
.deploy_template: &deploy_template
<<:
- *common_template
stage:
deploy
script:
- export GPG_EXECUTABLE=gpg
- export GPG_KEYID=$(cat dev/gpg/public_gpg_key)
- echo "GPG_KEYID = $GPG_KEYID"
- $GPG_EXECUTABLE --version
- openssl version
- $GPG_EXECUTABLE --list-keys
- $GPG_EXECUTABLE --list-keys
# Decrypt and import GPG Keys / trust
# note CI_KITWARE_SECRET is a protected variables only available on master and release branch
- GLKWS=$CI_KITWARE_SECRET openssl enc -aes-256-cbc -pbkdf2 -md SHA512 -pass env:GLKWS -d -a -in dev/gpg/ci_public_gpg_key.pgp.enc | $GPG_EXECUTABLE --import
- GLKWS=$CI_KITWARE_SECRET openssl enc -aes-256-cbc -pbkdf2 -md SHA512 -pass env:GLKWS -d -a -in dev/gpg/gpg_owner_trust.enc | $GPG_EXECUTABLE --import-ownertrust
- GLKWS=$CI_KITWARE_SECRET openssl enc -aes-256-cbc -pbkdf2 -md SHA512 -pass env:GLKWS -d -a -in dev/gpg/ci_secret_gpg_key.pgp.enc | $GPG_EXECUTABLE --import
- $GPG_EXECUTABLE --list-keys || echo "first one fails for some reason"
- $GPG_EXECUTABLE --list-keys
# Install twine
- pip install six pyopenssl ndg-httpsclient pyasn1 -U
- pip install requests[security] twine
# Execute the publish script for real this time
- MB_PYTHON_TAG=$MB_PYTHON_TAG DO_GPG=True GPG_KEYID=$GPG_KEYID TWINE_PASSWORD=$TWINE_PASSWORD TWINE_USERNAME=$TWINE_USERNAME GPG_EXECUTABLE=$GPG_EXECUTABLE CURRENT_BRANCH=release DEPLOY_BRANCH=release DO_TAG=True DO_UPLOAD=True ./publish.sh
# Have the server git-tag the release and push the tags
- VERSION=$(python -c "import setup; print(setup.VERSION)")
# do sed twice to handle the case of https clone with and without a read token
- URL_HOST=$(git remote get-url origin | sed -e 's|https\?://.*@||g' | sed -e 's|https\?://||g')
- echo "URL_HOST = $URL_HOST"
- git config user.email "[email protected]"
- git config user.name "Gitlab-CI"
- git tag $VERSION -m "tarball tag $VERSION"
- git push --tags "https://${GIT_PUSH_TOKEN}@${URL_HOST}"
only:
refs:
- release
.build_install_test: &build_install_test
- pip install -r requirements.txt -U
### JOBS ###
# Define the actual jobs
# ---------------
# Python 3.8 Jobs
build/cp38-cp38-linux:
<<:
- *build_template
image:
gitlab.kitware.com:4567/computer-vision/ci-docker/gl-python:3.8
test_full/cp38-cp38-linux:
<<:
- *test_full_template
image:
gitlab.kitware.com:4567/computer-vision/ci-docker/gl-python:3.8
# for universal builds we only need to gpg sign once
gpgsign/cp38-cp38-linux:
<<:
- *gpgsign_template
image:
gitlab.kitware.com:4567/computer-vision/ci-docker/gl-python:3.8
deploy/cp38-cp38-linux:
<<:
- *deploy_template
image:
gitlab.kitware.com:4567/computer-vision/ci-docker/gl-python:3.8
# ---------------
# Python 3.7 Jobs
#build/cp37-cp37m-linux:
# <<:
# - *build_template
# image:
# python:3.7
#test_full/cp37-cp37m-linux:
# <<:
# - *test_full_template
# image:
# python:3.7
# ---------------
# Python 3.6 Jobs
build/cp36-cp36m-linux:
<<:
- *build_template
image:
gitlab.kitware.com:4567/computer-vision/ci-docker/gl-python:3.6
test_full/cp36-cp36m-linux:
<<:
- *test_full_template
image:
gitlab.kitware.com:4567/computer-vision/ci-docker/gl-python:3.6
## ---------------
## Python 3.5 Jobs
#build/cp35-cp35m-linux:
# <<:
# - *build_template
# image:
# python:3.5
#test_full/cp35-cp35m-linux:
# <<:
# - *test_full_template
# image:
# python:3.5
## ---------------
## Python 2.7 Jobs
#build/cp27-cp27mu-linux:
# <<:
# - *build_template
# image:
# python:2.7
#test_full/cp27-cp27mu-linux:
# <<:
# - *test_full_template
# image:
# python:2.7