-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jenkinsfile
201 lines (183 loc) · 7.15 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
class Globals {
// the library version
static String version = 'latest'
// the default python version
static String pythonVersion = '3.11'
// the tag used when publishing documentation
static String documentationTag = ''
}
@Library('dev_tools@main') _
pipeline {
agent {label 'podman'}
parameters {
booleanParam(name: 'RELEASE_BUILD', defaultValue: false, description: 'Creates and publishes a new release')
booleanParam(name: 'PUBLISH_DOCUMENTATION', defaultValue: false, description: 'Publishes the generated documentation')
}
environment {
PROJECT_NAME = 'cosmo-archive-retrieve'
PIP_USER = 'python-mch'
SCANNER_HOME = tool name: 'Sonarqube-certs-PROD', type: 'hudson.plugins.sonar.SonarRunnerInstallation';
HTTP_PROXY = 'http://proxy.meteoswiss.ch:8080'
HTTPS_PROXY = 'http://proxy.meteoswiss.ch:8080'
NO_PROXY = '.meteoswiss.ch,localhost'
}
options {
gitLabConnection('CollabGitLab')
// New jobs should wait until older jobs are finished
disableConcurrentBuilds()
// Discard old builds
buildDiscarder(logRotator(artifactDaysToKeepStr: '7', artifactNumToKeepStr: '1', daysToKeepStr: '45', numToKeepStr: '10'))
// Timeout the pipeline build after 1 hour
timeout(time: 1, unit: 'HOURS')
}
stages {
stage('Init') {
steps {
updateGitlabCommitStatus name: 'Build', state: 'running'
script {
Globals.documentationTag = env.BRANCH_NAME
}
}
}
stage('Regex') {
steps {
sh '''
sed -i "s/service.meteoswiss.ch/hub.meteoswiss.ch/g" pyproject.toml
'''
}
}
stage('Test') {
parallel {
stage('3.10') {
steps {
script {
runWithPodman.poetryPytest '3.10', false, false
}
}
}
// python 3.11 is the default version, used for executing pylint, mypy, sphinx etc.
// all libs. are kept in the .venv folder
stage('python 3.11') {
steps {
script {
runWithPodman.poetryPytest Globals.pythonVersion
}
}
}
}
post {
always {
junit keepLongStdio: true, testResults: 'junit*.xml'
}
}
}
stage('Run Pylint') {
steps {
script {
runWithPodman.pythonCmd Globals.pythonVersion,
'poetry run pylint -rn --output-format=parseable --output=pylint.log --exit-zero cosmo_archive_retrieve'
}
}
}
// myPy is treated inside Jenkins because it is not yet integrated with SonarQube (the rest of the CI results is published therein)
stage('Run Mypy') {
steps {
script {
runWithPodman.pythonCmd Globals.pythonVersion,
'poetry run mypy -p cosmo_archive_retrieve | grep error | tee mypy.log'
recordIssues(qualityGates: [[threshold: 10, type: 'TOTAL', unstable: false]], tools: [myPy(pattern: 'mypy.log')])
}
}
post {
failure {
script {
error "Too many mypy issues, exiting now..."
}
}
}
}
stage('SonarQube Analysis') {
steps {
withSonarQubeEnv("Sonarqube-PROD") {
// fix source path in coverage.xml
// (required because coverage is calculated using podman which uses a differing file structure)
// https://stackoverflow.com/questions/57220171/sonarqube-client-fails-to-parse-pytest-coverage-results
sh "sed -i 's/\\/src\\/app-root/.\\//g' coverage.xml"
sh "${SCANNER_HOME}/bin/sonar-scanner"
}
}
}
stage("Quality Gate") {
steps {
timeout(time: 1, unit: 'HOURS') {
// Parameter indicates whether to set pipeline to UNSTABLE if Quality Gate fails
// true = set pipeline to UNSTABLE, false = don't
waitForQualityGate abortPipeline: true
}
}
}
stage('Release') {
when { expression { params.RELEASE_BUILD } }
steps {
echo 'Build a wheel and publish'
script {
withCredentials([string(credentialsId: "python-mch-nexus-secret", variable: 'PIP_PWD')]) {
runDevScript("build/poetry-lib-release.sh ${env.PIP_USER} $PIP_PWD 3.11")
Globals.version = sh(script: 'git describe --tags --abbrev=0', returnStdout: true).trim()
Globals.documentationTag = Globals.version
env.TAG_NAME = Globals.documentationTag
}
}
}
}
stage('Build Documentation') {
when { expression { params.PUBLISH_DOCUMENTATION } }
steps {
script {
runWithPodman.pythonCmd Globals.pythonVersion,
'poetry install && poetry run sphinx-build doc doc/_build'
}
}
}
stage('Publish Documentation') {
when { expression { params.PUBLISH_DOCUMENTATION } }
environment {
PATH = "$HOME/tools/openshift-client-tools:$PATH"
KUBECONFIG = "$workspace/.kube/config"
}
steps {
withCredentials([string(credentialsId: "documentation-main-prod-token", variable: 'TOKEN')]) {
sh "oc login https://api.cp.meteoswiss.ch:6443 --token \$TOKEN"
publishDoc 'doc/_build/', env.PROJECT_NAME, Globals.version, 'python', Globals.documentationTag
}
}
post {
cleanup {
sh 'oc logout || true'
}
}
}
}
post {
aborted {
updateGitlabCommitStatus name: 'Build', state: 'canceled'
}
failure {
updateGitlabCommitStatus name: 'Build', state: 'failed'
echo 'Sending email'
emailext(subject: "${currentBuild.fullDisplayName}: ${currentBuild.currentResult}",
attachLog: true,
attachmentsPattern: 'generatedFile.txt',
body: "Job '${env.JOB_NAME} #${env.BUILD_NUMBER}': ${env.BUILD_URL}",
recipientProviders: [requestor(), developers()])
}
success {
echo 'Build succeeded'
updateGitlabCommitStatus name: 'Build', state: 'success'
}
always{
echo 'Cleaning up workspace'
deleteDir()
}
}
}