-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJenkinsfile
More file actions
164 lines (147 loc) · 7.43 KB
/
Jenkinsfile
File metadata and controls
164 lines (147 loc) · 7.43 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
pipeline {
agent {
label 'DS agent'
}
options {
disableConcurrentBuilds()
timeout(time: 30, unit: 'MINUTES')
buildDiscarder(logRotator(numToKeepStr: '5'))
}
environment {
PROJECT = 'ds-storage'
BUILD_TO_TRIGGER = 'ds-license'
}
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-storage", 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") {
sh "mvn versions:set -DnewVersion=${params.ORIGINAL_BRANCH}-${params.ORIGINAL_JOB}-${env.PROJECT}-SNAPSHOT"
echo "Changing MVN version to: ${params.ORIGINAL_BRANCH}-${params.ORIGINAL_JOB}-${env.PROJECT}-SNAPSHOT"
}
}
}
stage('Change dependencies') {
when {
expression {
params.ORIGINAL_BRANCH ==~ "PR-[0-9]+"
}
}
steps {
script {
withMaven(options: [artifactsPublisher(fingerprintFilesDisabled: true, archiveFilesDisabled: true)], traceability: true, mavenLocalRepo: "${WORKSPACE}/repository") {
switch (params.ORIGINAL_JOB) {
case ['ds-parent']:
sh "mvn versions:use-dep-version -Dincludes=dk.kb.dsparent:* -DdepVersion=${params.ORIGINAL_BRANCH}-${params.ORIGINAL_JOB}-ds-parent-SNAPSHOT -DforceVersion=true"
sh "mvn versions:use-dep-version -Dincludes=dk.kb.dsshared:* -DdepVersion=${params.ORIGINAL_BRANCH}-${params.ORIGINAL_JOB}-ds-shared-SNAPSHOT -DforceVersion=true"
echo "Changing MVN dependency ds-parent to: ${params.ORIGINAL_BRANCH}-${params.ORIGINAL_JOB}-ds-parent-SNAPSHOT"
echo "Changing MVN dependency ds-shared to: ${params.ORIGINAL_BRANCH}-${params.ORIGINAL_JOB}-ds-shared-SNAPSHOT"
break
case ['ds-shared']:
sh "mvn versions:use-dep-version -Dincludes=dk.kb.dsshared:* -DdepVersion=${params.ORIGINAL_BRANCH}-${params.ORIGINAL_JOB}-ds-shared-SNAPSHOT -DforceVersion=true"
echo "Changing MVN dependency ds-shared to: ${params.ORIGINAL_BRANCH}-${params.ORIGINAL_JOB}-ds-shared-SNAPSHOT"
break
}
}
}
}
}
stage('Build') {
steps {
withMaven(options: [artifactsPublisher(fingerprintFilesDisabled: true, archiveFilesDisabled: true)], traceability: true, mavenLocalRepo: "${WORKSPACE}/repository") {
// Execute Maven build
sh "mvn clean package"
}
}
}
stage('Analyze build results') {
steps {
recordIssues(
aggregatingResults: true,
tools: [
java(),
javaDoc(),
mavenConsole(),
taskScanner(
highTags: 'FIXME',
normalTags: 'TODO',
includePattern: '**/*.java',
excludePattern: 'target/**/*'
)
]
)
}
}
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") {
sh "mvn clean deploy -DskipTests=true"
}
}
}
stage('Trigger ds-license 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
}
}
}
}
}
}