-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathJenkinsfile
147 lines (142 loc) · 5.42 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
#!/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 = "template${BRANCH_NAME}"
}
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()
}
}
cleanup {
script {
sh "sudo rm -rf ${WORKSPACE}/{*,.*} || true"
}
}
}
}
}
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
}