-
Notifications
You must be signed in to change notification settings - Fork 6
/
Jenkinsfile
254 lines (249 loc) · 8.7 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
#!/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 = "cce${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')]) {
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 {
archive 'build/libs/*.jar,build/resources/main/api-definition.html, build/resources/main/ version.properties'
}
unstable {
script {
notifyAfterFailure()
}
}
failure {
script {
notifyAfterFailure()
}
}
}
}
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-cce-deploy-to-test', wait: false
}
post {
failure {
script {
notifyAfterFailure()
}
}
}
}
stage('Parallel: Contract tests') {
when {
expression {
return VERSION.endsWith("SNAPSHOT")
}
}
parallel {
stage('Contract tests') {
steps {
build job: "OpenLMIS-contract-tests-pipeline/${params.contractTestsBranch}", propagate: true, wait: true,
parameters: [
string(name: 'serviceName', value: 'cce'),
text(name: 'customEnv', value: "OL_CCE_VERSION=${STAGING_VERSION}")
]
}
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-cce.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/cce:${STAGING_VERSION}"
sh "docker tag openlmis/cce:${STAGING_VERSION} openlmis/cce:${VERSION}"
sh "docker push openlmis/cce:${VERSION}"
}
post {
success {
script {
if (!VERSION.endsWith("SNAPSHOT")) {
currentBuild.setKeepLog(true)
}
}
}
failure {
script {
notifyAfterFailure()
}
}
}
}
}
post {
fixed {
script {
BRANCH = "${BRANCH_NAME}"
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 = "${BRANCH_NAME}"
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
}