-
Notifications
You must be signed in to change notification settings - Fork 41
/
Jenkinsfile
438 lines (410 loc) · 19.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
// https://jenkins.io/doc/book/pipeline/syntax/
// https://jenkins.io/doc/pipeline/steps/
// https://www.cloudbees.com/sites/default/files/declarative-pipeline-refcard.pdf
// https://vetlugin.wordpress.com/2017/01/31/guide-jenkins-pipeline-merge-requests/
// KWA TOOD :
// - estimate deviation from base branch (if relevant)
// - separate stage for the javadoc:aggregate-jar build (in order to -T 1C the packaging)
// - fix the partial build
pipeline {
agent {
label 'java11'
}
environment {
MVN_BASE = "/usr/local/maven/bin/mvn --settings ${pwd()}/.ci/settings.xml"
MVN_COMMAND = "${MVN_BASE} --show-version --batch-mode --errors --fail-at-end -DdeployAtEnd=true "
M2_REPO = "${HOME}/.m2"
CI = credentials("app-jenkins")
SERVICE_SONAR_URL = credentials("service-sonar-java11-url")
SERVICE_NEXUS_URL = credentials("service-nexus-url")
SERVICE_REPO_SSHURL = credentials("repository-connection-string")
SERVICE_GIT_URL = credentials("service-gitlab-url")
SERVICE_DOCKER_PULL_URL=credentials("SERVICE_DOCKER_PULL_URL")
SERVICE_REPOSITORY_URL=credentials("service-repository-url")
GITHUB_ACCOUNT_TOKEN = credentials("vitam-prg-token")
ES_VERSION="7.17.20"
MONGO_VERSION="7.0.8"
MINIO_VERSION="RELEASE.2020-04-15T00-39-01Z" // more precise than edge
OPENIO_VERSION="18.10"
}
options {
// disableConcurrentBuilds()
buildDiscarder(
logRotator(
artifactDaysToKeepStr: '',
artifactNumToKeepStr: '',
daysToKeepStr: '100',
numToKeepStr: '30'
)
)
}
stages {
stage("Tools configuration") {
steps {
// Maven : nothing to do, the settings.xml file is passed to maven by command arg & configured by env variables
// Npm : we could have chosen "npm config" command, but, using a file, we keep the same principle as for maven
// KWA Note : Awful outside docker...
// sh "cp -f .ci/.npmrc ~/"
// sh "rm -f ~/.m2/settings.xml"
echo "Workspace location : ${env.WORKSPACE}"
echo "Branch : ${env.GIT_BRANCH}"
}
}
stage("Detecting changes for build") {
steps {
script {
// OMA : to get info from scm checkout
env.GIT_REV=checkout(scm).GIT_COMMIT
env.GIT_PRECEDENT_COMMIT=checkout(scm).GIT_PREVIOUS_SUCCESSFUL_COMMIT
}
sh "git --git-dir .git rev-parse HEAD > vitam_commit.txt"
sh '''git diff --name-only ${GIT_REV} ${GIT_PRECEDENT_COMMIT} | grep -oE '^[^/]+' | sort | uniq > .changed_roots.txt'''
// GIT_PREVIOUS_SUCCESSFUL_COMMIT
script {
def changedRoots = readFile(".changed_roots.txt").tokenize('\n')
// KWA Caution bis : check if the file is empty before...
env.CHANGED_VITAM = changedRoots.contains("sources") || changedRoots.contains("doc")
env.CHANGED_VITAM_PRODUCT = changedRoots.contains("rpm") || changedRoots.contains("deb")
// KWA Caution : need to get check conditions twice
}
// OMA: evaluate project version ; write directly through shell as I didn't find anything else
sh "$MVN_BASE -q -f sources/pom.xml --non-recursive -Dexec.args='\${project.version}' -Dexec.executable=\"echo\" org.codehaus.mojo:exec-maven-plugin:1.3.1:exec > version_projet.txt"
echo "Changed VITAM : ${env.CHANGED_VITAM}"
echo "Changed VITAM : ${env.CHANGED_VITAM_PRODUCT}"
}
}
stage('Reinit host & containers') {
steps {
// Force termination / cleanup of containers
sh 'docker rm -f miniossl elasticsearch mongodb minionossl openio swift'
// Cleanup any remaining docker volumes
sh 'docker volume prune -f'
// Cleanup M2 repo
sh 'rm -fr ${M2_REPO}/repository/fr/gouv/vitam/'
// prepare storage for minIO SSL
dir("${pwd}/dataminiossl") {
// bad rustine, as minIO docker writes as root
// sh "sudo chmod -R 777 ${pwd}/dataminiossl"
deleteDir()
}
sh "mkdir ${pwd}/dataminiossl"
}
}
stage ("Prepare Docker containers for testing") {
steps {
dir('sources') {
script {
// openstack swift+keystone
sh 'docker run -d -m 1g -p 5000:5000 -p 35357:35357 -p 8080:8080 --name swift ${SERVICE_DOCKER_PULL_URL}/jeantil/openstack-keystone-swift:pike'
// minIO with SSL
sh "docker run -d -m 512m --name miniossl -p 127.0.0.1:9000:9000 --user \$(id -u):\$(id -g) -v ${pwd}/dataminiossl:/data -v ${WORKSPACE}/sources/common/common-storage/src/test/resources/s3/tls:/root/.minio/certs -e \"MINIO_ACCESS_KEY=MKU4HW1K9HSST78MDY3T\" -e \"MINIO_SECRET_KEY=aSyBSStwp4JDZzpNKeJCc0Rdn12hOTa0EFejFfkd\" ${SERVICE_DOCKER_PULL_URL}/minio/minio:${MINIO_VERSION} server /data"
// elasticsearch
sh 'docker run -d -m 1g --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e "cluster.name=elasticsearch-data" ${SERVICE_DOCKER_PULL_URL}/elasticsearch:${ES_VERSION}'
// mongodb
sh "docker run -d -m 1g --name mongodb -p 27017:27017 -v $WORKSPACE/vitam-conf-dev/tests/initdb.d/:/docker-entrypoint-initdb.d/ --health-cmd 'test \$(echo \"rs.status().ok\" | mongo --quiet) -eq 1' --health-start-period 30s --health-interval 10s $SERVICE_DOCKER_PULL_URL/mongo:$MONGO_VERSION mongod --bind_ip_all --replSet rs0"
// minIO without SSL
sh "docker run -d -m 512m --name minionossl -p 127.0.0.1:9999:9000 -e \"MINIO_ACCESS_KEY=MKU4HW1K9HSST78MDY3T\" -e \"MINIO_SECRET_KEY=aSyBSStwp4JDZzpNKeJCc0Rdn12hOTa0EFejFfkd\" ${SERVICE_DOCKER_PULL_URL}/minio/minio:${MINIO_VERSION} server /data"
// openio
sh 'docker run -d -m 512m --name openio -p 127.0.0.1:6007:6007 -e "REGION=us-west-1" ${SERVICE_DOCKER_PULL_URL}/openio/sds:${OPENIO_VERSION}'
// Configure elasticsearch
sh 'while ! curl -v http://localhost:9200; do sleep 2; done'
sh 'curl -X PUT http://localhost:9200/_template/default -H \'Content-Type: application/json\' -d \'{"index_patterns": ["*"],"order": -1,"settings": {"number_of_shards": "1","number_of_replicas": "0"}}\''
sh 'curl -X PUT -H \'Content-Type: application/json\' http://localhost:9200/_cluster/settings -d \'{ "transient": { "cluster.routing.allocation.disk.threshold_enabled": false } }\''
// Configure swift
sh 'while ! curl -f http://127.0.0.10:35357/v3; do sleep 2; done'
sh 'docker exec swift /swift/bin/register-swift-endpoint.sh http://127.0.0.1:8080'
}
}
}
}
stage ("Execute unit and integration tests on master branches") {
when {
anyOf {
branch "develop*"
branch "master_*"
branch "master"
tag pattern: "^[1-9]+(\\.rc)?(\\.[0-9]+)?\\.[0-9]+(-.*)?", comparator: "REGEXP"
}
}
options {
timeout(time: 3, unit: "HOURS")
}
environment {
LANG="fr_FR.UTF-8" // to bypass dateformat problem
}
steps {
dir('sources') {
script {
try {
// Build Vitam
sh '$MVN_COMMAND -f pom.xml clean verify org.owasp:dependency-check-maven:aggregate sonar:sonar -Dsonar.projectName=$GIT_BRANCH -Dsonar.projectKey=$(sed -E \'s/[^[:alnum:]]+/_/g\' <<< ${GIT_BRANCH#*/}) -Ddownloader.quick.query.timestamp=false'
} finally {
// Force termination / cleanup of containers
sh 'docker rm -f miniossl elasticsearch mongodb minionossl openio swift'
}
}
}
}
post {
always {
junit 'sources/**/target/surefire-reports/*.xml'
}
success {
archiveArtifacts (
artifacts: '**/dependency-check-report.html'
, fingerprint: true
, allowEmptyArchive: true
)
}
}
}
stage ("Execute unit and integration tests on merge requests") {
when {
not{
anyOf {
branch "develop*"
branch "master_*"
branch "master"
tag pattern: "^[1-9]+(\\.rc)?(\\.[0-9]+)?\\.[0-9]+(-.*)?", comparator: "REGEXP"
}
}
}
environment {
MVN_COMMAND = "${MVN_BASE} --show-version --batch-mode --errors --fail-at-end -DinstallAtEnd=true -DdeployAtEnd=true "
LANG="fr_FR.UTF-8" // to bypass dateformat problem
}
steps {
updateGitlabCommitStatus name: 'mergerequest', state: "running"
dir('sources') {
script {
try {
// Build Vitam
sh '$MVN_COMMAND -f pom.xml clean verify org.owasp:dependency-check-maven:aggregate sonar:sonar -Dsonar.projectName=$GIT_BRANCH -Dsonar.projectKey=$(sed -E \'s/[^[:alnum:]]+/_/g\' <<< ${GIT_BRANCH#*/}) -Ddownloader.quick.query.timestamp=false'
} finally {
// Force termination / cleanup of containers
sh 'docker rm -f miniossl elasticsearch mongodb minionossl openio swift'
}
}
}
}
post {
always {
junit 'sources/**/target/surefire-reports/*.xml'
}
success {
archiveArtifacts (
artifacts: '**/dependency-check-report.html'
, fingerprint: true
, allowEmptyArchive: true
)
updateGitlabCommitStatus name: 'mergerequest', state: "success"
addGitLabMRComment comment: "pipeline-job : [analyse sonar](https://sonar.dev.programmevitam.fr/dashboard?id=${gitlabSourceBranch}) de la branche"
}
failure {
updateGitlabCommitStatus name: 'mergerequest', state: "failed"
}
unstable {
updateGitlabCommitStatus name: 'mergerequest', state: "failed"
}
aborted {
updateGitlabCommitStatus name: 'mergerequest', state: "canceled"
}
}
}
stage("Build packages") {
when {
anyOf {
branch "develop*"
branch "master_*"
branch "master"
tag pattern: "^[1-9]+(\\.rc)?(\\.[0-9]+)?\\.[0-9]+(-.*)?", comparator: "REGEXP"
}
}
steps {
parallel(
"Package VITAM solution" : {
dir('sources') {
// Hack / workaround for javadoc build bug that causes locally built artefacts to be pulled from remote repository
// First clean / install artefacts to local repository without deploy
// Then rebuild with javadoc, and forcing usage of local artefacts
sh '$MVN_COMMAND -f pom.xml -Dmaven.test.skip=true -DskipTests=true clean install -T 1C'
sh '$MVN_COMMAND -f pom.xml -Dmaven.test.skip=true -DskipTests=true --no-snapshot-updates javadoc:aggregate-jar deploy rpm:attached-rpm jdeb:jdeb'
// -T 1C // Doesn't work with the javadoc:aggregate-jar goal, nor with jdeb plugin (works but not thread safe)
}
},
"Checkout publishing scripts" : {
checkout([$class: 'GitSCM',
branches: [[name: 'scaleway_j11']],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'vitam-build.git']],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: 'app-jenkins', url: "$SERVICE_GIT_URL"]]
])
}
)
}
}
stage("Build doc package") {
when {
anyOf {
branch "develop*"
branch "master_*"
branch "master"
tag pattern: "^[1-9]+(\\.rc)?(\\.[0-9]+)?\\.[0-9]+(-.*)?", comparator: "REGEXP"
}
}
steps {
dir('doc') {
// -T 1C does not work with jdeb:jdeb (works but not thread safe)
sh '$MVN_COMMAND -f pom.xml clean install jdeb:jdeb rpm:attached-rpm deploy'
}
}
post {
always {
junit 'doc/**/target/surefire-reports/*.xml'
}
}
}
stage("Prepare packages building") {
when {
anyOf {
branch "develop*"
branch "master_*"
branch "master"
tag pattern: "^[1-9]+(\\.rc)?(\\.[0-9]+)?\\.[0-9]+(-.*)?", comparator: "REGEXP"
}
}
steps {
sh 'rm -rf deb/vitam-external/target'
sh 'rm -rf deb/vitam-product/target'
sh 'rm -rf rpm/vitam-external/target'
sh 'rm -rf rpm/vitam-product/target'
}
}
stage("Build vitam-product & vitam-external packages") {
when {
anyOf {
branch "develop*"
branch "master_*"
branch "master"
tag pattern: "^[1-9]+(\\.rc)?(\\.[0-9]+)?\\.[0-9]+(-.*)?", comparator: "REGEXP"
}
}
steps {
parallel(
"Build vitam-product rpm": {
dir('rpm/vitam-product') {
sh './build-all-docker.sh'
}
},
"Build vitam-product deb": {
dir('deb/vitam-product') {
sh './build-all.sh'
}
},
"Download vitam-external rpm": {
dir('rpm/vitam-external') {
sh './build_repo.sh'
}
},
"Download vitam-external deb": {
dir('deb/vitam-external') {
sh './build_repo.sh'
}
}
)
}
}
stage("Publish packages") {
when {
anyOf {
branch "develop*"
branch "master_*"
branch "master"
tag pattern: "^[1-9]+(\\.rc)?(\\.[0-9]+)?\\.[0-9]+(-.*)?", comparator: "REGEXP"
}
}
steps {
parallel(
"Upload vitam-product packages": {
sshagent (credentials: ['jenkins_sftp_to_repository']) {
sh 'vitam-build.git/push_product_repo.sh commit $SERVICE_REPO_SSHURL'
}
},
"Upload vitam-external packages": {
sshagent (credentials: ['jenkins_sftp_to_repository']) {
sh 'vitam-build.git/push_external_repo.sh commit $SERVICE_REPO_SSHURL'
}
},
"Upload documentation": {
sshagent (credentials: ['jenkins_sftp_to_repository']) {
sh 'vitam-build.git/push_doc_repo.sh commit $SERVICE_REPO_SSHURL'
}
},
"Upload sources packages": {
sshagent (credentials: ['jenkins_sftp_to_repository']) {
sh 'vitam-build.git/push_sources_repo.sh commit $SERVICE_REPO_SSHURL'
}
},
"Upload deployment": {
sshagent (credentials: ['jenkins_sftp_to_repository']) {
sh 'vitam-build.git/push_deployment_repo.sh commit $SERVICE_REPO_SSHURL'
}
}
)
}
post {
success {
slackSend (color: '#00aa5b', message: "Build OK de la branche ${env.GIT_BRANCH}, commit: ${env.GIT_COMMIT}", channel: "#pic-ci")
}
unstable {
slackSend (color: '#ffaa00', message: "Build Unstable de la branche ${env.GIT_BRANCH}, commit: ${env.GIT_COMMIT}", channel: "#pic-ci")
}
failure {
slackSend (color: '#a30000', message: "Build KO de la branche ${env.GIT_BRANCH}, commit: ${env.GIT_COMMIT}", channel: "#pic-ci")
}
}
}
stage("Update symlink") {
when {
anyOf {
branch "develop*"
branch "master_*"
branch "master"
tag pattern: "^[1-9]+(\\.rc)?(\\.[0-9]+)?\\.[0-9]+(-.*)?", comparator: "REGEXP"
}
}
steps {
sshagent (credentials: ['jenkins_sftp_to_repository']) {
sh 'vitam-build.git/push_symlink_repo.sh commit $SERVICE_REPO_SSHURL'
}
}
}
stage("Information") {
steps {
script {
for (String i : readFile('vitam_commit.txt').split("\r?\n")) {
println i
}
sh 'rm vitam_commit.txt'
}
script {
dir('.ci') {
sh './git_commands.sh'
}
}
}
}
}
post {
// Clean after build
always {
// Cleanup any remaining docker volumes
sh 'docker volume prune -f'
// Cleanup M2 repo
sh 'rm -fr ${M2_REPO}/repository/fr/gouv/vitam/'
// Cleanup workspace
cleanWs()
}
}
}