forked from OpenLMIS/openlmis-referencedata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
245 lines (240 loc) · 8.83 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
#!/usr/bin/env groovy
import hudson.tasks.test.AbstractTestResultAction
properties([
[
$class: 'ThrottleJobProperty',
categories: ['pipeline'],
throttleEnabled: true,
throttleOption: 'category'
]
])
pipeline {
agent none
options {
buildDiscarder(logRotator(
numToKeepStr: env.BRANCH_NAME.equals("master") ? '15' : '3',
daysToKeepStr: env.BRANCH_NAME.equals("master") || env.BRANCH_NAME.startsWith("rel-") ? '' : '7',
artifactDaysToKeepStr: env.BRANCH_NAME.equals("master") || env.BRANCH_NAME.startsWith("rel-") ? '' : '3',
artifactNumToKeepStr: env.BRANCH_NAME.equals("master") || env.BRANCH_NAME.startsWith("rel-") ? '' : '1'
))
disableConcurrentBuilds()
skipStagesAfterUnstable()
}
environment {
COMPOSE_PROJECT_NAME = "referencedata${BRANCH_NAME}"
}
parameters {
string(name: 'contractTestsBranch', defaultValue: 'master', description: 'The branch of contract tests to checkout')
}
stages {
stage('Preparation') {
agent any
steps {
withCredentials([usernamePassword(
credentialsId: "cad2f741-7b1e-4ddd-b5ca-2959d40f62c2",
usernameVariable: "USER",
passwordVariable: "PASS"
)]) {
sh 'set +x'
sh 'docker login -u $USER -p $PASS'
}
script {
CURRENT_BRANCH = env.GIT_BRANCH // needed for agent-less stages
def properties = readProperties file: 'gradle.properties'
if (!properties.serviceVersion) {
error("serviceVersion property not found")
}
VERSION = properties.serviceVersion
STAGING_VERSION = properties.serviceVersion
if (CURRENT_BRANCH != 'master' || (CURRENT_BRANCH == 'master' && !VERSION.endsWith("SNAPSHOT"))) {
STAGING_VERSION += "-STAGING"
}
currentBuild.displayName += " - " + VERSION
}
}
post {
failure {
script {
notifyAfterFailure()
}
}
}
}
stage('Build') {
agent any
environment {
PATH = "/usr/local/bin/:$PATH"
STAGING_VERSION = "${STAGING_VERSION}"
}
steps {
withCredentials([file(credentialsId: '8da5ba56-8ebb-4a6a-bdb5-43c9d0efb120', variable: 'ENV_FILE'),
file(credentialsId: 'b35ad1bd-ccca-437f-a2cd-3578c10da7bf', variable: 'SECRING_FILE'),
usernamePassword(
credentialsId: "70dd29d6-7990-4598-a2f8-aa3e1f038ac1",
usernameVariable: "SIGNING_KEYID",
passwordVariable: "SIGNING_PASSWORD"),
usernamePassword(
credentialsId: "79aa4a36-2c52-486f-bbca-1ed06b314a96",
usernameVariable: "OSSRH_USERNAME",
passwordVariable: "OSSRH_PASSWORD"
)]) {
script {
try {
sh(script: "./ci-buildImage.sh")
currentBuild.result = processTestResults('SUCCESS')
}
catch (exc) {
currentBuild.result = processTestResults('FAILURE')
if (currentBuild.result == 'FAILURE') {
error(exc.toString())
}
}
}
}
}
post {
success {
archiveArtifacts artifacts: 'build/libs/*.jar,' +
'build/resources/main/api-definition.html'
}
unstable {
script {
notifyAfterFailure()
}
}
failure {
script {
notifyAfterFailure()
}
}
cleanup {
script {
sh "sudo rm -rf ${WORKSPACE}/{*,.*} || true"
}
}
}
}
stage('Build demo-data') {
when {
expression {
return CURRENT_BRANCH == 'master'
}
}
steps {
build job: "OpenLMIS-3.x-build-demo-data-pipeline"
}
post {
failure {
script {
notifyAfterFailure()
}
}
}
}
stage('Deploy to test') {
when {
expression {
return CURRENT_BRANCH == 'master' && VERSION.endsWith("SNAPSHOT")
}
}
steps {
build job: 'OpenLMIS-referencedata-deploy-to-test', wait: false
}
post {
failure {
script {
notifyAfterFailure()
}
}
}
}
stage('ERD generation') {
agent {
node {
label 'master'
}
}
environment {
PATH = "/usr/local/bin/:$PATH"
}
steps {
dir('erd') {
sh(script: "../ci-erdGeneration.sh")
archiveArtifacts artifacts: 'erd-referencedata.zip'
}
}
post {
failure {
script {
notifyAfterFailure()
}
}
}
}
stage('Push image') {
agent any
when {
expression {
env.GIT_BRANCH =~ /rel-.+/ || (env.GIT_BRANCH == 'master' && !VERSION.endsWith("SNAPSHOT"))
}
}
steps {
sh "docker pull openlmis/referencedata:${STAGING_VERSION}"
sh "docker tag openlmis/referencedata:${STAGING_VERSION} openlmis/referencedata:${VERSION}"
sh "docker push openlmis/referencedata:${VERSION}"
}
post {
success {
script {
if (!VERSION.endsWith("SNAPSHOT")) {
currentBuild.setKeepLog(true)
}
}
}
failure {
script {
notifyAfterFailure()
}
}
}
}
}
post {
fixed {
script {
BRANCH = "${env.GIT_BRANCH}".trim()
if (BRANCH.equals("master") || BRANCH.startsWith("rel-")) {
slackSend color: 'good', message: "${env.JOB_NAME} - #${env.BUILD_NUMBER} Back to normal"
}
}
}
}
}
def notifyAfterFailure() {
messageColor = 'danger'
if (currentBuild.result == 'UNSTABLE') {
messageColor = 'warning'
}
BRANCH = "${env.GIT_BRANCH}".trim()
if (BRANCH.equals("master") || BRANCH.startsWith("rel-")) {
slackSend color: "${messageColor}", message: "${env.JOB_NAME} - #${env.BUILD_NUMBER} ${env.STAGE_NAME} ${currentBuild.result} (<${env.BUILD_URL}|Open>)"
}
emailext subject: "${env.JOB_NAME} - #${env.BUILD_NUMBER} ${env.STAGE_NAME} ${currentBuild.result}",
body: """<p>${env.JOB_NAME} - #${env.BUILD_NUMBER} ${env.STAGE_NAME} ${currentBuild.result}</p><p>Check console <a href="${env.BUILD_URL}">output</a> to view the results.</p>""",
recipientProviders: [[$class: 'CulpritsRecipientProvider'], [$class: 'DevelopersRecipientProvider']]
}
def processTestResults(status) {
checkstyle pattern: '**/build/reports/checkstyle/*.xml'
pmd pattern: '**/build/reports/pmd/*.xml'
junit '**/build/test-results/*/*.xml'
AbstractTestResultAction testResultAction = currentBuild.rawBuild.getAction(AbstractTestResultAction.class)
if (testResultAction != null) {
failuresCount = testResultAction.failCount
echo "Failed tests count: ${failuresCount}"
if (failuresCount > 0) {
echo "Setting build unstable due to test failures"
status = 'UNSTABLE'
}
}
return status
}