-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
120 lines (107 loc) · 5.76 KB
/
Jenkinsfile
File metadata and controls
120 lines (107 loc) · 5.76 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
pipeline {
agent {
label 'DS agent'
}
options {
disableConcurrentBuilds()
timeout(time: 40, unit: 'MINUTES')
buildDiscarder(logRotator(numToKeepStr: '5'))
}
environment {
PROJECT = 'ds-parent'
BUILD_TO_TRIGGER = 'ds-shared'
}
triggers {
// This triggers the pipeline when a PR is opened or updated or so I hope
githubPush()
}
parameters {
string(name: 'ORIGINAL_BRANCH', defaultValue: "${env.BRANCH_NAME}", description: 'Branch of first job to run, will also be PI_ID for a PR')
string(name: 'ORIGINAL_JOB', defaultValue: "ds-parent", description: 'What job was the first to build?')
string(name: 'TARGET_BRANCH', defaultValue: "${env.CHANGE_TARGET}", description: 'Target branch if PR')
string(name: 'SOURCE_BRANCH', defaultValue: "${env.CHANGE_BRANCH}", description: 'Source branch if PR')
}
stages {
stage('Echo Environment Variables') {
steps {
echo "PROJECT: ${env.PROJECT}"
echo "BUILD_TO_TRIGGER: ${env.BUILD_TO_TRIGGER}"
echo "ORIGINAL_BRANCH: ${params.ORIGINAL_BRANCH}"
echo "ORIGINAL_JOB: ${params.ORIGINAL_JOB}"
echo "TARGET_BRANCH: ${params.TARGET_BRANCH}"
echo "SOURCE_BRANCH: ${params.SOURCE_BRANCH}"
}
}
stage('Change version if part of PR') {
when {
expression {
params.ORIGINAL_BRANCH ==~ 'PR-[0-9]+'
}
}
steps {
withMaven(options: [artifactsPublisher(fingerprintFilesDisabled: true, archiveFilesDisabled: true)], traceability: true, mavenLocalRepo: "${WORKSPACE}/repository") {
// We don't want to build child modules so we use this flag to disable it --non-recursive https://maven.apache.org/guides/mini/guide-multiple-modules.html#command-line-options
sh "mvn versions:set -DnewVersion=${params.ORIGINAL_BRANCH}-${params.ORIGINAL_JOB}-${env.PROJECT}-SNAPSHOT --non-recursive"
echo "Changing MVN version to: ${params.ORIGINAL_BRANCH}-${params.ORIGINAL_JOB}-${env.PROJECT}-SNAPSHOT"
}
}
}
stage('Build') {
steps {
withMaven(options: [artifactsPublisher(fingerprintFilesDisabled: true, archiveFilesDisabled: true)], traceability: true, mavenLocalRepo: "${WORKSPACE}/repository") {
// Execute Maven build
// We don't want to build child modules so we use this flag to disable it --non-recursive https://maven.apache.org/guides/mini/guide-multiple-modules.html#command-line-options
sh "mvn clean package --non-recursive"
}
}
}
stage('Push to Nexus') {
when {
// Check if Build was successful
expression {
currentBuild.currentResult == "SUCCESS" && params.ORIGINAL_BRANCH ==~ "master|release_v[0-9]+|PR-[0-9]+"
}
}
steps {
withMaven(options: [artifactsPublisher(fingerprintFilesDisabled: true, archiveFilesDisabled: true)], traceability: true, mavenLocalRepo: "${WORKSPACE}/repository") {
// We don't want to build child modules so we use this flag to disable it --non-recursive https://maven.apache.org/guides/mini/guide-multiple-modules.html#command-line-options
sh "mvn clean deploy -DskipTests=true --non-recursive"
}
}
}
stage('Trigger ds-shared Build') {
when {
expression {
currentBuild.currentResult == "SUCCESS" && params.ORIGINAL_BRANCH ==~ "master|release_v[0-9]+|PR-[0-9]+"
}
}
steps {
script {
if (params.ORIGINAL_BRANCH ==~ "PR-[0-9]+") {
// Check the next job to trigger, if there is a branch with same name as the source branch (if a task has involved multiple repositories)
def EMPTY_IF_NO_BRANCH = sh(script: "git ls-remote --heads https://github.com/kb-dk/${env.BUILD_TO_TRIGGER}.git | grep 'refs/heads/${params.SOURCE_BRANCH}' || echo empty", returnStdout: true).trim()
echo "EMPTY_IF_NO_BRANCH: ${EMPTY_IF_NO_BRANCH}"
if ("${EMPTY_IF_NO_BRANCH}" == "empty") {
BRANCH_TO_USE = "${params.TARGET_BRANCH}"
} else {
BRANCH_TO_USE = "${params.SOURCE_BRANCH}"
}
echo "Triggering: DS-GitHub/${env.BUILD_TO_TRIGGER}/${BRANCH_TO_USE}"
build job: "DS-GitHub/${env.BUILD_TO_TRIGGER}/${BRANCH_TO_USE}",
parameters: [
string(name: 'ORIGINAL_BRANCH', value: params.ORIGINAL_BRANCH),
string(name: 'ORIGINAL_JOB', value: params.ORIGINAL_JOB),
string(name: 'TARGET_BRANCH', value: params.TARGET_BRANCH),
string(name: 'SOURCE_BRANCH', value: params.SOURCE_BRANCH)
],
wait: true // Wait for the pipeline to finish
} else if (params.ORIGINAL_BRANCH ==~ "master|release_v[0-9]+") {
echo "Triggering: DS-GitHub/${env.BUILD_TO_TRIGGER}/${params.ORIGINAL_BRANCH}"
build job: "DS-GitHub/${env.BUILD_TO_TRIGGER}/${params.ORIGINAL_BRANCH}",
wait: false // Don't wait for the pipeline to finish
}
}
}
}
}
}