-
Notifications
You must be signed in to change notification settings - Fork 9
/
Jenkinsfile
111 lines (99 loc) · 3.49 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
#!/usr/bin/env groovy
import hudson.model.*
pipeline {
agent any
options {
disableConcurrentBuilds()
buildDiscarder(logRotator(numToKeepStr: '3'))
}
environment {
version = "${getVersion()}"
BINTRAY_USER = getParam('BINTRAY_USER')
BINTRAY_API_KEY = getParam('BINTRAY_API_KEY')
}
stages {
stage('Checkout') {
steps {
this.notifyBuild('STARTED', version)
git credentialsId: 'github', poll: false, url: '[email protected]:cityzendata/sensision.git'
echo "Building ${version}"
}
}
stage('Build') {
steps {
sh './gradlew clean build -x test'
}
}
stage('Test') {
steps {
sh './gradlew test'
junit allowEmptyResults: true, keepLongStdio: true, testResults: '**/build/test-results/**/*.xml'
step([$class: 'JUnitResultArchiver', allowEmptyResults: true, keepLongStdio: true, testResults: '**/build/test-results/**/*.xml'])
}
}
stage('Package') {
steps {
sh './gradlew sensisionLib sensisionService -x test'
archiveArtifacts "sensision/build/libs/*.jar"
}
}
stage('Tar') {
steps {
sh "./gradlew createTarArchive -x test"
archiveArtifacts allowEmptyArchive: true, artifacts: 'sensision/build/libs/*.tar.gz', fingerprint: true
}
}
}
post {
success {
this.notifyBuild('SUCCESSFUL', version)
}
failure {
this.notifyBuild('FAILURE', version)
}
aborted {
this.notifyBuild('ABORTED', version)
}
unstable {
this.notifyBuild('UNSTABLE', version)
}
}
}
void notifyBuild(String buildStatus, String version) {
// build status of null means successful
buildStatus = buildStatus ?: 'SUCCESSFUL'
String subject = "${buildStatus}: Job ${env.JOB_NAME} [${env.BUILD_DISPLAY_NAME}] | ${version}" as String
String summary = "${subject} (${env.BUILD_URL})" as String
// Override default values based on build status
if (buildStatus == 'STARTED') {
color = 'YELLOW'
colorCode = '#FFFF00'
} else if (buildStatus == 'SUCCESSFUL') {
color = 'GREEN'
colorCode = '#00FF00'
} else if (buildStatus == 'PUBLISHED') {
color = 'BLUE'
colorCode = '#0000FF'
} else {
color = 'RED'
colorCode = '#FF0000'
}
// Send notifications
this.notifySlack(colorCode, summary, buildStatus)
}
void notifySlack(String color, String message, String buildStatus) {
String slackURL = getParam('slackUrl')
String payload = "{\"username\": \"${env.JOB_NAME}\",\"attachments\":[{\"title\": \"${env.JOB_NAME} ${buildStatus}\",\"color\": \"${color}\",\"text\": \"${message}\"}]}" as String
sh "curl -X POST -H 'Content-type: application/json' --data '${payload}' ${slackURL}" as String
}
String getParam(String key) {
return params.get(key)
}
String getVersion() {
return sh(returnStdout: true, script: 'git describe --abbrev=0 --tags').trim()
}
boolean isItATagCommit() {
String lastCommit = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
String tag = sh(returnStdout: true, script: "git show-ref --tags -d | grep ^${lastCommit} | sed -e 's,.* refs/tags/,,' -e 's/\\^{}//'").trim()
return tag != ''
}