-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathJenkinsfile
179 lines (159 loc) · 6.93 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
class Globals {
// sets to abort the pipeline if the Sonarqube QualityGate fails
static boolean qualityGateAbortPipeline = false
// the reference (image name + tag) of the container image
static String imageReference = ''
// the service version
static String version = ''
}
String rebuild_cron = env.BRANCH_NAME == "main" ? "@midnight" : ""
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'
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=https://hub.meteoswiss.ch/nexus/repository/python-all/simple \
.venv-mchbuild/bin/pip install --upgrade mchbuild
'''
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.version = env.TAG_NAME
} else
{
echo "Detected development build triggered from branch."
Globals.version= sh(
script: 'mchbuild -g semanticVersion build.getSemanticVersion',
returnStdout: true
)
}
def imageName = sh(
script: 'mchbuild -g containerImageName build.getImageName',
returnStdout: true
)
Globals.imageReference = imageName + ':' + Globals.version
echo "Using version ${Globals.version} and image reference ${Globals.imageReference}"
}
}
}
stage('Build') {
steps {
echo '---- BUILD IMAGE ----'
sh """
mchbuild -s version=${Globals.version} -s image=${Globals.imageReference} build.imageTester test.unit
"""
}
post {
always {
junit keepLongStdio: true, testResults: 'test_reports/junit*.xml'
}
}
}
stage('Scan') {
steps {
echo '---- LINT & TYPE CHECK ----'
sh "mchbuild -s image=${Globals.imageReference} 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('Create Artifacts') {
steps {
script {
echo '---- CREATE IMAGE ----'
sh """
mchbuild -s version=${Globals.version} -s image=${Globals.imageReference} build.imageAwsRunner
"""
}
}
}
stage('Publish Artifacts') {
environment {
REGISTRY_AUTH_FILE = "$workspace/.containers/auth.json"
}
steps {
echo "---- PUBLISH IMAGE ----"
withCredentials([usernamePassword(credentialsId: 'openshift-nexus',
passwordVariable: 'NXPASS',
usernameVariable: 'NXUSER')]) {
sh "mchbuild publish.image -s fullImageName=${Globals.imageReference}"
}
}
}
}
post {
cleanup {
sh """
mchbuild -s version=${Globals.version} clean
"""
}
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'
}
}
}