-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
613 lines (607 loc) · 34.4 KB
/
Jenkinsfile
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
library identifier: 'JenkinsPythonHelperLibrary@2024.1.2', retriever: modernSCM(
[$class: 'GitSCMSource',
remote: 'https://github.com/UIUCLibrary/JenkinsPythonHelperLibrary.git',
])
def get_sonarqube_unresolved_issues(report_task_file){
script{
def props = readProperties file: '.scannerwork/report-task.txt'
def response = httpRequest url : props['serverUrl'] + "/api/issues/search?componentKeys=" + props['projectKey'] + "&resolved=no"
def outstandingIssues = readJSON text: response.content
return outstandingIssues
}
}
pipeline {
agent none
parameters {
string defaultValue: 'speedcloud', name: 'DOCKER_IMAGE_NAME'
booleanParam defaultValue: true, description: 'Run checks', name: 'RUN_CHECKS'
booleanParam(name: 'USE_SONARQUBE', defaultValue: true, description: 'Send data test data to SonarQube')
credentials(name: 'SONARCLOUD_TOKEN', credentialType: 'org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl', defaultValue: 'sonarcloud_token', required: false)
booleanParam(name: 'TEST_RUN_TOX', defaultValue: false, description: 'Run Tox Tests')
booleanParam defaultValue: false, description: 'Build Docker container', name: 'BUILD_DOCKER'
booleanParam defaultValue: false, description: 'Package', name: 'PACKAGE'
booleanParam defaultValue: true, description: 'Include x86_64 in building and testing', name: 'INCLUDE-x86_64'
booleanParam defaultValue: false, description: 'Include ARM64 in building and testing', name: 'INCLUDE-arm64'
booleanParam defaultValue: false, description: 'Publish Docker Image to registry', name: 'PUBLISH_DOCKER'
}
stages {
stage('Test'){
stages{
stage('Code Quality'){
when{
equals expected: true, actual: params.RUN_CHECKS
beforeInput true
beforeAgent true
}
agent {
dockerfile {
filename 'ci/docker/jenkins/Dockerfile'
label 'linux && docker && x86'
additionalBuildArgs '--build-arg PIP_EXTRA_INDEX_URL --build-arg PIP_INDEX_URL --build-arg SONAR_INSTALL_PATH=/opt/sonar'
args '--mount source=sonar-cache-cloud,target=/opt/sonar/.sonar/cache --mount source=pip-audit-cache-speedcloud,target=/tmp/pip-audit-cache'
}
}
options {
retry(conditions: [agent()], count: 3)
timeout(time: 1, unit: 'DAYS')
}
environment {
npm_config_cache = '/tmp/npm-cache'
PIP_CACHE_DIR='/tmp/pipcache'
UV_PYTHON = '3.11'
UV_CACHE_DIR='/tmp/uvcache'
UV_PYTHON_INSTALL_DIR='/tmp/uvpython'
UV_TOOL_DIR='/tmp/uvtools'
UV_INDEX_STRATEGY='unsafe-best-match'
}
stages{
stage('Set up Tests') {
steps{
mineRepository()
sh(
label: 'Create virtual environment',
script: '''python3 -m venv bootstrap_uv
bootstrap_uv/bin/pip install --disable-pip-version-check uv
bootstrap_uv/bin/uv venv venv
. ./venv/bin/activate
bootstrap_uv/bin/uv pip install uv
rm -rf bootstrap_uv
uv pip install -r requirements-dev.txt
'''
)
sh(
label: 'Install package in development mode',
script: '''. ./venv/bin/activate
uv pip install -e .
'''
)
cache(maxCacheSize: 1000, caches: [
arbitraryFileCache(
path: 'node_modules',
includes: '**/*',
cacheName: 'npm',
cacheValidityDecidingFile: 'package-lock.json',
compressionMethod: 'TARGZ'
)
]) {
sh 'npm install'
}
sh 'mkdir -p logs'
sh 'mkdir -p main && ln -s $PWD/src/frontend/src main/src'
}
}
stage('Perform Tests'){
parallel{
stage('PyTest'){
steps{
catchError(buildResult: 'UNSTABLE', message: 'Did not pass all pytest tests', stageResult: "UNSTABLE") {
sh(
script: '''. ./venv/bin/activate
coverage run --parallel-mode -m pytest --junitxml=./reports/tests/pytest/pytest-junit.xml
'''
)
}
}
post {
always {
junit 'reports/tests/pytest/pytest-junit.xml'
}
}
}
stage('Audit Requirement Freeze File'){
steps{
catchError(buildResult: 'UNSTABLE', message: 'pip-audit found issues', stageResult: 'UNSTABLE') {
sh './venv/bin/uvx --python-preference=only-managed --with-requirements requirements.txt pip-audit --cache-dir=/tmp/pip-audit-cache --local'
}
}
}
stage('Flake8') {
steps{
catchError(buildResult: 'SUCCESS', message: 'Flake8 found issues', stageResult: "UNSTABLE") {
sh(label: 'Run Flake8',
script: '''. ./venv/bin/activate
flake8 src/backend/speedcloud --tee --output-file=logs/flake8.log
''')
}
}
post {
always {
recordIssues(tools: [flake8(pattern: 'logs/flake8.log')])
}
}
}
stage('MyPy') {
steps{
timeout(10){
tee('logs/mypy.log') {
catchError(buildResult: 'SUCCESS', message: 'MyPy found issues', stageResult: 'UNSTABLE') {
sh(
label: "Running MyPy",
script: '''. ./venv/bin/activate
mypy --version
mkdir -p reports/mypy/html
mkdir -p logs
mypy -p speedcloud --html-report reports/mypy/html --linecoverage-report reports/mypy/linecoverage
'''
)
}
}
}
}
post {
always {
publishHTML([allowMissing: true, alwaysLinkToLastBuild: false, keepAll: false, reportDir: "reports/mypy/html/", reportFiles: 'index.html', reportName: 'MyPy HTML Report', reportTitles: ''])
recordIssues(
filters: [excludeFile('/stubs/*')],
tools: [myPy(name: 'MyPy', pattern: 'logs/mypy.log')]
)
}
}
}
stage("PyDocStyle"){
steps{
catchError(buildResult: 'SUCCESS', message: 'Did not pass all pyDocStyle tests', stageResult: 'UNSTABLE') {
tee('reports/pydocstyle-report.txt'){
sh(
label: 'Run pydocstyle',
script: '''. ./venv/bin/activate
pydocstyle src/backend/speedcloud
'''
)
}
}
}
post {
always{
recordIssues(tools: [pyDocStyle(pattern: 'reports/pydocstyle-report.txt')])
}
}
}
stage('Pylint') {
steps{
withEnv(['PYLINTHOME=.']) {
catchError(buildResult: 'SUCCESS', message: 'Pylint found issues', stageResult: 'UNSTABLE') {
tee('reports/pylint_issues.txt'){
sh(
label: 'Running pylint',
script: '''. ./venv/bin/activate
pylint src/backend/speedcloud -r n --msg-template="{path}:{module}:{line}: [{msg_id}({symbol}), {obj}] {msg}"
''',
)
}
}
sh(
label: 'Running pylint for sonarqube',
script: '''. ./venv/bin/activate
pylint src/backend/speedcloud -d duplicate-code --output-format=parseable | tee reports/pylint.txt
''',
returnStatus: true
)
}
}
post{
always{
recordIssues(tools: [pyLint(pattern: 'reports/pylint_issues.txt')])
}
}
}
stage('Task Scanner'){
steps{
recordIssues(tools: [taskScanner(highTags: 'FIXME', includePattern: 'src/backend/**/*.py,src/frontend/**/*.tsx', normalTags: 'TODO')])
}
}
stage('Hadolint'){
steps{
catchError(buildResult: 'SUCCESS', message: 'hadolint found issues', stageResult: "UNSTABLE") {
sh 'hadolint --format json src/backend/Dockerfile src/frontend/Dockerfile > logs/hadolint.json'
}
}
post{
always{
recordIssues(tools: [hadoLint(pattern: 'logs/hadolint.json')])
}
}
}
stage('Jest'){
environment {
HOME = '/tmp/'
JEST_JUNIT_OUTPUT_NAME='js-junit.xml'
JEST_JUNIT_ADD_FILE_ATTRIBUTE='true'
JEST_JUNIT_OUTPUT_DIR="${WORKSPACE}/reports"
npm_config_cache = '/tmp/npm-cache'
}
steps{
sh 'npm run test -- --reporters=default --reporters=jest-junit --collectCoverage --watchAll=false --coverageDirectory=../../reports/ --coverageReporters=cobertura --coverageReporters=lcov --detectOpenHandles'
}
post{
always{
junit 'reports/*.xml'
}
}
}
stage('ESlint'){
steps{
timeout(10){
catchError(buildResult: 'SUCCESS', message: 'ESlint found issues', stageResult: 'UNSTABLE') {
sh(
label: "Running ESlint",
script: 'npm run lint -- src/frontend/src --format=checkstyle -o reports/eslint_report.xml'
)
}
sh(
label: "Running ESlint for sonar",
script: 'npm run lint -- src/frontend/src --format=json -o reports/eslint_report.json',
returnStatus: true
)
}
}
post{
always{
recordIssues(tools: [esLint(pattern: 'reports/eslint_report.xml')])
}
}
}
}
post{
unsuccessful{
sh '''. ./venv/bin/activate
uv pip list
'''
}
always{
sh(label: 'combining coverage data',
script: '''. ./venv/bin/activate
coverage combine
coverage xml -o ./reports/python-coverage.xml
'''
)
recordCoverage(tools: [[parser: 'COBERTURA', pattern: 'reports/coverage.xml']])
archiveArtifacts( allowEmptyArchive: true, artifacts: 'reports/')
}
}
}
stage('Run Sonarqube Analysis'){
options{
lock('cloudwagon-sonarscanner')
}
environment{
PACKAGE_VERSION="${readTOML( file: 'pyproject.toml')['project'].version}"
PACKAGE_NAME="${readTOML( file: 'pyproject.toml')['project'].name}"
SONAR_USER_HOME='/tmp/sonar'
}
when{
allOf{
equals expected: true, actual: params.USE_SONARQUBE
expression{
try{
withCredentials([string(credentialsId: params.SONARCLOUD_TOKEN, variable: 'dddd')]) {
echo 'Found credentials for sonarqube'
}
} catch(e){
return false
}
return true
}
}
}
steps{
script{
withSonarQubeEnv(installationName:'sonarcloud', credentialsId: params.SONARCLOUD_TOKEN) {
sh(
label: 'Running Sonar Scanner',
script: "./venv/bin/uvx pysonar-scanner -Dsonar.projectVersion=$PACKAGE_VERSION -Dsonar.buildString=\"$BUILD_TAG\" ${env.CHANGE_ID? '-Dsonar.pullrequest.key=$CHANGE_ID -Dsonar.pullrequest.base=$BRANCH_NAME': '-Dsonar.branch.name=$BRANCH_NAME'}"
)
}
timeout(time: 1, unit: 'HOURS') {
def sonarqube_result = waitForQualityGate(abortPipeline: false)
if (sonarqube_result.status != 'OK') {
unstable "SonarQube quality gate: ${sonarqube_result.status}"
}
def outstandingIssues = get_sonarqube_unresolved_issues('.scannerwork/report-task.txt')
writeJSON file: 'reports/sonar-report.json', json: outstandingIssues
}
milestone label: 'sonarcloud'
}
}
post {
always{
recordIssues(tools: [sonarQube(pattern: 'reports/sonar-report.json')])
}
}
}
}
post{
cleanup{
cleanWs(
deleteDirs: true,
patterns: [
[pattern: 'venv/', type: 'INCLUDE'],
[pattern: 'main/', type: 'INCLUDE'],
[pattern: 'coverage/', type: 'INCLUDE'],
[pattern: 'reports/', type: 'INCLUDE'],
[pattern: '**/node_modules/', type: 'INCLUDE'],
]
)
}
}
}
stage('Tox'){
when{
equals expected: true, actual: params.TEST_RUN_TOX
}
environment{
PIP_CACHE_DIR='/tmp/pipcache'
UV_INDEX_STRATEGY='unsafe-best-match'
UV_TOOL_DIR='/tmp/uvtools'
UV_PYTHON_INSTALL_DIR='/tmp/uvpython'
UV_CACHE_DIR='/tmp/uvcache'
}
steps{
script{
def envs = []
node('docker && linux'){
docker.image('python').inside('--mount source=python-tmp-uvcache-cloudwagon,target=/tmp'){
try{
checkout scm
sh(script: 'python3 -m venv venv && venv/bin/pip install --disable-pip-version-check uv')
envs = sh(
label: 'Get tox environments',
script: './venv/bin/uvx --quiet --with tox-uv tox list -d --no-desc',
returnStdout: true,
).trim().split('\n')
} finally{
cleanWs(
patterns: [
[pattern: 'venv/', type: 'INCLUDE'],
[pattern: '.tox', type: 'INCLUDE'],
[pattern: '**/__pycache__/', type: 'INCLUDE'],
]
)
}
}
}
parallel(
envs.collectEntries{toxEnv ->
def version = toxEnv.replaceAll(/py(\d)(\d+)/, '$1.$2')
[
"Tox Environment: ${toxEnv}",
{
node('docker && linux'){
docker.image('python').inside('--mount source=python-tmp-cloudwagon,target=/tmp'){
checkout scm
try{
sh( label: 'Running Tox',
script: """python3 -m venv venv
trap "rm -rf venv" EXIT
venv/bin/pip install --disable-pip-version-check uv
venv/bin/uv python install cpython-${version}
venv/bin/uvx -p ${version} --with tox-uv tox run -e ${toxEnv}
"""
)
} catch(e) {
sh(script: '''. ./venv/bin/activate
uv python list
'''
)
throw e
} finally{
cleanWs(
patterns: [
[pattern: 'venv/', type: 'INCLUDE'],
[pattern: '.tox', type: 'INCLUDE'],
[pattern: '**/__pycache__/', type: 'INCLUDE'],
]
)
}
}
}
}
]
}
)
}
}
}
}
}
stage('Packaging'){
when{
equals expected: true, actual: params.PACKAGE
}
parallel{
stage('Create Production Build'){
agent {
docker {
image 'node'
label 'linux && docker'
}
}
options{
retry(2)
}
environment {
npm_config_cache = '/tmp/npm-cache'
}
steps{
cache(maxCacheSize: 1000, caches: [
arbitraryFileCache(path: 'node_modules', includes: '**/*', cacheName: 'npm', cacheValidityDecidingFile: 'frontend/package-lock.json')
]) {
sh 'npm install'
}
// todo: make this into a webpack package
sh 'npx --yes browserslist@latest --update-db'
catchError(buildResult: 'SUCCESS', message: 'There are issues with building production build', stageResult: 'UNSTABLE') {
sh(label: 'Creating production build', script: 'npm run build')
}
}
}
stage('Build wheel'){
agent {
docker {
image 'python'
label 'linux && docker'
}
}
steps{
sh '''python -m venv venv
venv/bin/python -m pip install pip --upgrade
venv/bin/pip install wheel
venv/bin/pip install build
venv/bin/python -m build --outdir dist
'''
}
post{
success{
stash includes: 'dist/*,whl', name: 'wheel'
}
}
}
}
}
stage('Build'){
when{
equals expected: true, actual: params.BUILD_DOCKER
beforeInput true
}
matrix {
axes {
axis {
name 'ARCH'
values 'arm64', 'x86_64'
}
}
when{
equals expected: true, actual: params["INCLUDE-${ARCH}"]
beforeAgent true
}
stages{
stage('Build for Architecture'){
agent {
label "linux && docker && ${ARCH}"
}
environment {
DOCKER_IMAGE_TEMP_NAME = UUID.randomUUID().toString()
}
stages{
stage('Building Docker Container'){
steps{
echo "DOCKER_IMAGE_TEMP_NAME = ${env.DOCKER_IMAGE_TEMP_NAME}"
withCredentials([file(credentialsId: 'private_pypi', variable: 'NETRC')]) {
configFileProvider([configFile(fileId: 'pypi_props', variable: 'PYPI_PROPS')]) {
script{
withEnv(["PIP_EXTRA_INDEX_URL=${readProperties(file: PYPI_PROPS)['PYPI_URL']}"]) {
docker.build(
env.DOCKER_IMAGE_TEMP_NAME,
'-f src/backend/Dockerfile --build-arg UV_EXTRA_INDEX_URL .'
).withRun('-p 8000:80'){ c->
docker.image('python').inside("--link ${c.id}:db") {
withEnv(['PIP_NO_CACHE_DIR=off']) {
sh '''
python -m venv venv --upgrade-deps
. ./venv/bin/activate
pip install --no-cache-dir pytest requests
pytest tests/test_integration.py --server-url=http://db
rm -rf venv
'''
}
}
}
}
}
}
}
}
}
}
post{
cleanup{
sh(returnStatus: true, script:"docker image rm ${env.DOCKER_IMAGE_TEMP_NAME}")
}
}
}
}
}
}
stage('Publish'){
when{
allOf{
equals expected: true, actual: params.PUBLISH_DOCKER
equals expected: true, actual: params.BUILD_DOCKER
}
beforeInput true
}
input {
message 'Push to docker registry?'
parameters {
string defaultValue: 'latest', name: 'DOCKER_TAG', trim: true
}
}
matrix {
axes {
axis {
name 'ARCH'
// values 'x86'
values 'arm', 'x86'
}
}
stages{
stage('Publish docker Image'){
agent {
label "linux && docker && ${ARCH}"
}
steps{
unstash 'wheel'
withCredentials([file(credentialsId: 'private_pypi', variable: 'NETRC')]) {
configFileProvider([configFile(fileId: 'pypi_props', variable: 'PYPI_PROPS')]) {
script{
docker.build(
params.DOCKER_IMAGE_NAME,
"-f backend/Dockerfile --secret id=netrc,src=\$NETRC --build-arg PIP_EXTRA_INDEX_URL=${readProperties(file: PYPI_PROPS)['PYPI_URL']} ."
).inside{
sh 'pip list'
}
}
}
}
configFileProvider([configFile(fileId: 'docker_props', variable: 'CONFIG_FILE')]) {
script{
def deploySettings = readProperties(file: CONFIG_FILE)
def registryUrl = "https://${deploySettings['registry']}"
echo "Using Docker registry: ${registryUrl} "
docker.withRegistry(registryUrl, deploySettings['credentialsId']){
docker.image(params.DOCKER_IMAGE_NAME).push(DOCKER_TAG)
}
}
}
}
post{
cleanup{
sh "docker image rm ${params.DOCKER_IMAGE_NAME}"
}
}
}
}
}
}
}
}