-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
204 lines (188 loc) · 9.01 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
def workerNode = 'devel12'
def dockerRepository = 'https://docker-de.artifacts.dbccloud.dk'
if (env.BRANCH_NAME == 'master') {
properties([
disableConcurrentBuilds(),
pipelineTriggers([
triggers: [
[
$class: 'jenkins.triggers.ReverseBuildTrigger',
upstreamProjects: "Docker-payara6-bump-trigger, ../pg-queue/master, ../dbc-commons", threshold: hudson.model.Result.SUCCESS
]
]
]),
])
}
pipeline {
agent { label workerNode }
environment {
MAVEN_OPTS = "-XX:+TieredCompilation -XX:TieredStopAtLevel=1"
DOCKER_PUSH_TAG = "${env.BUILD_NUMBER}"
GITLAB_PRIVATE_TOKEN = credentials("metascrum-gitlab-api-token")
}
triggers {
pollSCM("H/3 * * * *")
}
options {
buildDiscarder(logRotator(artifactDaysToKeepStr: "", artifactNumToKeepStr: "", daysToKeepStr: "30", numToKeepStr: "30"))
timestamps()
}
stages {
stage("build") {
steps {
script {
def version = readMavenPom().version.replace('-SNAPSHOT', '')
def label = imageLabel()
def status = sh returnStatus: true, script: """
rm -rf \$WORKSPACE/.repo/dk/dbc
mvn -B -Dmaven.repo.local=\$WORKSPACE/.repo dependency:resolve dependency:resolve-plugins >/dev/null 2>&1 || true
mvn -B -Dmaven.repo.local=\$WORKSPACE/.repo clean
mvn -B -Dmaven.repo.local=\$WORKSPACE/.repo -Ddocker.extra.args="--pull" -Ddocker.image.version=${version} -Ddocker.image.label=${label} --fail-at-end org.jacoco:jacoco-maven-plugin:prepare-agent install -Dsurefire.useFile=false
"""
// We want code-coverage and pmd/spotbugs even if unittests fails
status += sh returnStatus: true, script: """
mvn -B -Dmaven.repo.local=\$WORKSPACE/.repo pmd:pmd pmd:cpd spotbugs:spotbugs -Dspotbugs.excludeFilterFile=src/test/spotbugs/spotbugs-exclude.xml
"""
junit testResults: '**/target/*-reports/TEST-*.xml'
def java = scanForIssues tool: [$class: 'Java']
publishIssues issues:[java]
def pmd = scanForIssues tool: [$class: 'Pmd'], pattern: '**/target/pmd.xml'
publishIssues issues:[pmd], unstableTotalAll:1
def cpd = scanForIssues tool: [$class: 'Cpd'], pattern: '**/target/cpd.xml'
publishIssues issues:[cpd]
def spotbugs = scanForIssues tool: [$class: 'SpotBugs'], pattern: '**/target/spotbugsXml.xml'
publishIssues issues:[spotbugs], unstableTotalAll:1
step([$class: 'JacocoPublisher',
execPattern: 'target/*.exec,**/target/*.exec',
classPattern: 'target/classes,**/target/classes',
sourcePattern: 'src/main/java,**/src/main/java',
exclusionPattern: 'src/test*,**/src/test*,**/*?Request.*,**/*?Response.*,**/*?Request$*,**/*?Response$*,**/*?DTO.*,**/*?DTO$*'
])
warnings consoleParsers: [
[parserName: "Java Compiler (javac)"],
[parserName: "JavaDoc Tool"]],
unstableTotalAll: "0",
failedTotalAll: "0"
if ( status != 0 ) {
currentBuild.result = Result.FAILURE
}
}
}
}
stage('Docker') {
steps {
script {
def pom = readMavenPom()
def version = pom.version.replace('-SNAPSHOT', '')
def label = imageLabel()
if (currentBuild.resultIsBetterOrEqualTo('SUCCESS')) {
docker.withRegistry(dockerRepository, 'docker') {
for(def image : ["holdings-items-content-service", "holdings-items-kafka-bridge", "holdings-items-monitor", "holdings-items-postgres", "holdings-items-postgres-content", "holdings-items-purge-tool", "holdings-items-solr-indexer", "holdings-items-update-facade"]) {
def app = docker.image("${image}-${version}:${label}")
app.push()
if (env.BRANCH_NAME == "master") {
app.push "latest"
}
}
}
}
}
}
}
stage("upload") {
steps {
script {
if (env.BRANCH_NAME ==~ /master|trunk/) {
sh """
mvn -Dmaven.repo.local=\$WORKSPACE/.repo -DskipTests -DskipITs package deploy:deploy
"""
}
}
}
}
stage("Update DIT") {
agent {
docker {
label workerNode
image "docker-dbc.artifacts.dbccloud.dk/build-env:latest"
alwaysPull true
}
}
when {
expression {
(currentBuild.result == null || currentBuild.result == 'SUCCESS') && env.BRANCH_NAME == 'master'
}
}
steps {
script {
dir("deploy") {
sh "set-new-version databases/holdings-items-db.yml ${env.GITLAB_PRIVATE_TOKEN} metascrum/dit-gitops-secrets ${DOCKER_PUSH_TAG} -b master"
sh "set-new-version services/search/holdings-items-content-service.yml ${env.GITLAB_PRIVATE_TOKEN} metascrum/dit-gitops-secrets ${DOCKER_PUSH_TAG} -b master"
sh "set-new-version services/search/holdings-items-update-facade.yml ${env.GITLAB_PRIVATE_TOKEN} metascrum/dit-gitops-secrets ${DOCKER_PUSH_TAG} -b master"
}
}
}
}
}
post {
fixed {
script {
if ("${env.BRANCH_NAME}" == 'master') {
emailext(
recipientProviders: [developers(), culprits()],
to: "[email protected]",
subject: "[Jenkins] ${env.JOB_NAME} #${env.BUILD_NUMBER} back to normal",
mimeType: 'text/html; charset=UTF-8',
body: "<p>The master is back to normal.</p><p><a href=\"${env.BUILD_URL}\">Build information</a>.</p>",
attachLog: false)
slackSend(channel: 'de-notifications',
color: 'good',
message: "${env.JOB_NAME} #${env.BUILD_NUMBER} back to normal: ${env.BUILD_URL}",
tokenCredentialId: 'slack-global-integration-token')
}
}
}
failure {
script {
if ("${env.BRANCH_NAME}" == 'master') {
emailext(
recipientProviders: [developers(), culprits()],
to: "[email protected]",
subject: "[Jenkins] ${env.JOB_NAME} #${env.BUILD_NUMBER} failed",
mimeType: 'text/html; charset=UTF-8',
body: "<p>The master build failed. Log attached. </p><p><a href=\"${env.BUILD_URL}\">Build information</a>.</p>",
attachLog: true,
)
slackSend(channel: 'de-notifications',
color: 'warning',
message: "${env.JOB_NAME} #${env.BUILD_NUMBER} failed and needs attention: ${env.BUILD_URL}",
tokenCredentialId: 'slack-global-integration-token')
} else {
// this is some other branch, only send to developer
emailext(
recipientProviders: [developers()],
subject: "[Jenkins] ${env.BUILD_TAG} failed and needs your attention",
mimeType: 'text/html; charset=UTF-8',
body: "<p>${env.BUILD_TAG} failed and needs your attention. </p><p><a href=\"${env.BUILD_URL}\">Build information</a>.</p>",
attachLog: false,
)
}
}
}
success {
archiveArtifacts artifacts: '**/target/*-jar-with-dependencies.jar', fingerprint: true
}
}
}
def imageLabel() {
def label = env.BRANCH_NAME.toLowerCase()
if (env.CHANGE_BRANCH) {
label = env.CHANGE_BRANCH.toLowerCase()
}
if (label == "master") {
label = env.BUILD_NUMBER
} else {
label = label.split(/\//)[-1]
}
return label
}