-
Notifications
You must be signed in to change notification settings - Fork 4
/
Jenkinsfile
178 lines (172 loc) · 6.56 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
import hudson.tasks.test.AbstractTestResultAction
pipeline {
agent any
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 {
PATH = "/usr/local/bin/:$PATH"
COMPOSE_PROJECT_NAME = "${env.JOB_NAME}-${BRANCH_NAME}"
}
stages {
stage('Preparation') {
steps {
checkout scm
withCredentials([usernamePassword(
credentialsId: "cad2f741-7b1e-4ddd-b5ca-2959d40f62c2",
usernameVariable: "USER",
passwordVariable: "PASS"
)]) {
sh 'set +x'
sh 'docker login -u $USER -p $PASS'
}
script {
def properties = readProperties file: 'project.properties'
if (!properties.version) {
error("version property not found")
}
VERSION = properties.version
currentBuild.displayName += " - " + VERSION
}
}
post {
failure {
script {
notifyAfterFailure()
}
}
}
}
stage('Build') {
steps {
withCredentials([file(credentialsId: '364a77e6-df3b-4012-8fa6-1c79ffe2171b', variable: 'ENV_FILE')]) {
script {
try {
sh '''
sudo rm -f .env
cp $ENV_FILE .env
if [ "$GIT_BRANCH" != "master" ]; then
sed -i '' -e "s#^TRANSIFEX_PUSH=.*#TRANSIFEX_PUSH=false#" .env 2>/dev/null || true
fi
docker-compose pull
docker-compose down --volumes
docker-compose run --entrypoint /dev-ui/build.sh cce-ui
docker-compose build image
docker-compose down --volumes
'''
currentBuild.result = processTestResults('SUCCESS')
}
catch (exc) {
currentBuild.result = processTestResults('FAILURE')
if (currentBuild.result == 'FAILURE') {
error(exc.toString())
}
}
}
}
}
post {
success {
archive 'build/styleguide/*, build/styleguide/**/*, build/docs/*, build/docs/**/*, build/messages/*'
}
unstable {
script {
notifyAfterFailure()
}
}
failure {
script {
notifyAfterFailure()
}
}
}
}
stage('Build reference-ui') {
when {
expression {
return "${env.GIT_BRANCH}" == 'master' && VERSION.endsWith("SNAPSHOT")
}
}
steps {
sh "docker tag openlmis/cce-ui:latest openlmis/cce-ui:${VERSION}"
sh "docker push openlmis/cce-ui:${VERSION}"
build job: 'OpenLMIS-reference-ui-pipeline/master', wait: false
}
post {
failure {
script {
notifyAfterFailure()
}
}
}
}
stage('Push image') {
when {
expression {
return env.GIT_BRANCH == 'master' || env.GIT_BRANCH =~ /rel-.+/
}
}
steps {
sh "docker tag openlmis/cce-ui:latest openlmis/cce-ui:${VERSION}"
sh "docker push openlmis/cce-ui:${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) {
junit '**/build/test/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
}