-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathJenkinsfile
133 lines (126 loc) · 4.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
#!/usr/bin/env groovy
import hudson.tasks.test.AbstractTestResultAction
properties([
[
$class: 'ThrottleJobProperty',
categories: ['pipeline'],
throttleEnabled: true,
throttleOption: 'category'
]
])
pipeline {
agent any
options {
buildDiscarder(logRotator(numToKeepStr: '30'))
skipStagesAfterUnstable()
}
environment {
PATH = "/usr/local/bin/:$PATH"
COMPOSE_PROJECT_NAME = "contract-tests-${BRANCH_NAME}"
}
parameters {
string(name: 'serviceName', defaultValue: '', description: 'Name of the service. Used to determine which contract tests to run.')
text(name: 'customEnv', defaultValue: '', description: 'A list of environment variables that will be appended to the .env file. This parameter can be used to provide image versions.')
}
stages {
stage('Preparation') {
steps {
script {
if (params.serviceName) {
currentBuild.displayName += " - " + params.serviceName
}
}
checkout scm
dir('openlmis-config') {
git branch: 'master',
credentialsId: 'OpenLMISConfigKey',
url: '[email protected]:villagereach/openlmis-config.git'
}
sh 'set +x'
sh 'cp ./openlmis-config/contract_tests.env ./settings.env'
sh "echo \"${params.customEnv}\" >> .env"
}
post {
failure {
script {
notifyAfterFailure()
}
}
}
}
stage('Contract tests') {
when {
expression {
def exists = fileExists "docker-compose.${params.serviceName}.yml"
return exists
}
}
steps {
timeout(time: 60, unit: 'MINUTES') {
script {
try {
sh "sudo rm -rf test-results"
sh "./run_contract_tests.sh docker-compose.${params.serviceName}.yml"
currentBuild.result = processTestResults('SUCCESS')
}
catch (exc) {
currentBuild.result = processTestResults('FAILURE')
if (currentBuild.result == 'FAILURE') {
error(exc.toString())
}
}
}
}
}
post {
unstable {
script {
notifyAfterFailure()
}
}
failure {
script {
notifyAfterFailure()
}
}
cleanup {
sh "sudo rm -rf test-results"
}
}
}
}
post {
cleanup {
sh '''
rm -Rf ./openlmis-config
rm -f ./settings.env
'''
}
fixed {
slackSend color: 'good', message: "${env.JOB_NAME} - #${env.BUILD_NUMBER} - ${params.serviceName} Back to normal"
}
}
}
def notifyAfterFailure() {
messageColor = 'danger'
if (currentBuild.result == 'UNSTABLE') {
messageColor = 'warning'
}
slackSend color: "${messageColor}", message: "${env.JOB_NAME} - #${env.BUILD_NUMBER} - ${params.serviceName} ${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 healthScaleFactor: 1.0, testResults: 'test-results/cucumber-junit.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
}