-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathJenkinsfile
More file actions
204 lines (181 loc) · 8.3 KB
/
Jenkinsfile
File metadata and controls
204 lines (181 loc) · 8.3 KB
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
class Globals {
// Pin mchbuild to stable version to avoid breaking changes
static String mchbuildPipPackage = 'mchbuild>=0.12.2,<0.13.0'
// static String mchbuildPipPackage = 'mchbuild==0.1.dev510+g9d58a9272'
// sets to abort the pipeline if the Sonarqube QualityGate fails
static boolean qualityGateAbortPipeline = false
// the default python version
static String pythonVersion = '3.10'
// Name of the container image
static String containerImageName= ''
// Semantic version of the artifact
static String semanticVersion = ''
static String PIP_INDEX_URL = 'https://hub.meteoswiss.ch/nexus/repository/python-all/simple'
}
String rebuild_cron = env.BRANCH_NAME == "main" ? "@midnight" : ""
@Library('dev_tools@main') _
pipeline {
agent { label 'podman' }
triggers { cron(rebuild_cron) }
options {
// 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')
gitLabConnection('CollabGitLab')
}
environment {
PATH = "$workspace/.venv-mchbuild/bin:$PATH"
HTTP_PROXY = 'http://proxy.meteoswiss.ch:8080'
HTTPS_PROXY = 'http://proxy.meteoswiss.ch:8080'
NO_PROXY = '.meteoswiss.ch,localhost'
SCANNER_HOME = tool name: 'Sonarqube-certs-PROD', type: 'hudson.plugins.sonar.SonarRunnerInstallation'
}
stages {
stage('Preflight') {
steps {
updateGitlabCommitStatus name: 'Build', state: 'running'
script {
echo '---- INSTALL MCHBUILD ----'
sh """
python -m venv .venv-mchbuild
PIP_INDEX_URL=${Globals.PIP_INDEX_URL} \
.venv-mchbuild/bin/pip install --upgrade "${Globals.mchbuildPipPackage}"
"""
echo '---- INITIALIZE PARAMETERS ----'
if (env.TAG_NAME) {
echo "Detected release build triggered from tag ${env.TAG_NAME}."
def isMajorMinorPatch = sh(
script: "mchbuild -s version=${env.TAG_NAME} -g isMajorMinorPatch build.checkGivenSemanticVersion",
returnStdout: true
)
if (isMajorMinorPatch != 'true') {
currentBuild.result = 'ABORTED'
error('Build aborted because release builds are only triggered for tags of the form <major>.<minor>.<patch>.')
}
Globals.semanticVersion = env.TAG_NAME
} else {
echo "Detected development build triggered from branch."
Globals.semanticVersion = sh(
script: 'mchbuild -g semanticVersion build.getSemanticVersion',
returnStdout: true
)
}
Globals.containerImageName = sh(
script: 'mchbuild -g containerImageName build.getImageName',
returnStdout: true
)
echo "Using semantic version: ${Globals.semanticVersion}"
echo "Using container image name: ${Globals.containerImageName}"
}
}
}
stage('Build') {
steps {
echo '---- BUILDING CONTAINER IMAGES ----'
sh """
mchbuild -s semanticVersion=${Globals.semanticVersion} -s containerImageName=${Globals.containerImageName} build.artifacts
"""
echo("---- RUNNING UNIT TESTS & COLLECTING COVERAGE ----")
sh """
mchbuild -s semanticVersion=${Globals.semanticVersion} -s containerImageName=${Globals.containerImageName} test.unit
"""
}
post {
always {
junit keepLongStdio: true, testResults: 'test_reports/junit*.xml'
}
}
}
stage('Scan') {
steps {
echo '---- LINT & TYPE CHECK ----'
sh "mchbuild -s semanticVersion=${Globals.semanticVersion} -s containerImageName=${Globals.containerImageName} test.lint"
script {
try {
recordIssues(qualityGates: [[threshold: 10, type: 'TOTAL', unstable: false]], tools: [myPy(pattern: 'test_reports/mypy.log')])
}
catch (err) {
error "Too many mypy issues, exiting now..."
}
}
echo("---- SONARQUBE ANALYSIS ----")
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' test_reports/coverage.xml"
sh "${SCANNER_HOME}/bin/sonar-scanner"
}
echo("---- SONARQUBE QUALITY GATE ----")
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: Globals.qualityGateAbortPipeline
}
}
}
stage('Publish Artifacts') {
environment {
REGISTRY_AUTH_FILE = "$workspace/.containers/auth.json"
}
steps {
echo "---- PUBLISHING CONTAINER IMAGES ----"
withCredentials([usernamePassword(credentialsId: 'openshift-nexus',
passwordVariable: 'NXPASS',
usernameVariable: 'NXUSER')]) {
sh """
mchbuild -s semanticVersion=${Globals.semanticVersion} -s containerImageName=${Globals.containerImageName} publish.artifacts
"""
}
}
}
stage('Release') {
when {
// This will only execute the stage if TAG_NAME is present
expression { return env.TAG_NAME != null }
}
steps {
script {
echo("---- PUBLISH SBOM ----")
withCredentials([string(
credentialsId: 'dependency-track-token-prod',
variable: 'DTRACK_TOKEN')]) {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
sh "mchbuild verify.publishSbom -s version=${Globals.semanticVersion}"
}
}
}
}
}
}
post {
cleanup {
sh """
mchbuild -s semanticVersion=${Globals.semanticVersion} -s containerImageName=${Globals.containerImageName} clean
"""
cleanWs()
}
aborted {
updateGitlabCommitStatus name: 'Build', state: 'canceled'
}
failure {
updateGitlabCommitStatus name: 'Build', state: 'failed'
echo 'Sending email'
sh 'df -h'
emailext(subject: "${currentBuild.fullDisplayName}: ${currentBuild.currentResult}",
attachLog: true,
attachmentsPattern: 'generatedFile.txt',
to: env.BRANCH_NAME == 'main' ?
sh(script: "mchbuild -g notifyOnNightlyFailure", returnStdout: true) : '',
body: "Job '${env.JOB_NAME} #${env.BUILD_NUMBER}': ${env.BUILD_URL}",
recipientProviders: [requestor(), developers()])
}
success {
echo 'Build succeeded'
updateGitlabCommitStatus name: 'Build', state: 'success'
}
}
}