-
Notifications
You must be signed in to change notification settings - Fork 36
/
Jenkinsfile
137 lines (124 loc) · 3.93 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
pipeline {
agent any
tools {
maven 'Maven 3.5'
}
environment {
APP_NAME = 'wf'
DEPLOY_TO = '../../../../websites/demo/'
EMAIL_RECIPIENTS = 'zb@bndy.net'
HAS_DEPLOYMENT = 'false'
}
stages {
stage('Prepare') {
steps {
updateGithubStatus("PENDING", "Begin to build");
//sh 'printenv'
sh 'java -version'
sh 'mvn -v'
sh 'mkdir -p ../../email-templates/ && \\cp ./jenkins/my-groovy-html.template ../../email-templates/my-groovy-html.template'
}
}
stage('Build') {
steps {
echo 'Building...'
sh 'mvn -B -DskipTests clean package'
}
}
stage('Test') {
steps {
echo 'Skip tests'
}
}
stage('Deploy') {
when {
// following expression is just available for multi-branch project
branch 'master'
}
steps {
echo 'Deploying....'
sh 'rm -f ${DEPLOY_TO}/${APP_NAME}.war'
sh 'rm -Rf $DEPLOY_TO/${APP_NAME}'
sh 'mv ./target/wf-*.war ${DEPLOY_TO}/${APP_NAME}.war'
script {
HAS_DEPLOYMENT = 'true'
}
}
}
}
post {
success {
updateGithubStatus("SUCCESS", "Build complete");
sendEmail("SUCCESS");
}
unstable {
updateGithubStatus("UNSTABLE", "Build complete");
sendEmail("UNSTABLE");
}
failure {
updateGithubStatus("FAILURE", "Build complete");
sendEmail("FAILURE");
}
}
}
// get change log to be send over the mail
@NonCPS
def getChangeString() {
MAX_MSG_LEN = 100
def changeString = ""
echo "Gathering SCM changes"
def changeLogSets = currentBuild.changeSets
for (int i = 0; i < changeLogSets.size(); i++) {
def entries = changeLogSets[i].items
for (int j = 0; j < entries.length; j++) {
def entry = entries[j]
truncated_msg = entry.msg.take(MAX_MSG_LEN)
changeString += "<li>${truncated_msg} [${entry.author}]</li>"
}
}
if (!changeString) {
changeString = "<li>No new changes</li>"
}
return changeString
}
def sendEmail(status) {
def subject = "[" + status + "] ${currentBuild.fullDisplayName}"
if (HAS_DEPLOYMENT == 'true') {
subject += " - deployed"
}
// Default Email Notification Plugin: email(...), but below supports html
emailext(
mimeType: "text/html",
to: "$EMAIL_RECIPIENTS",
subject: "${subject}",
body: '''${SCRIPT, template="my-groovy-html.template"}''',
//body: "Changes:<ul>" + getChangeString() + "</ul><br />Check console output at: ${BUILD_URL}console" + "<br />",
recipientProviders: [[$class: 'CulpritsRecipientProvider'], [$class: 'RequesterRecipientProvider']]
)
}
// set GitHub status
def getRepoURL() {
sh "git config --get remote.origin.url > .git/remote-url"
return readFile(".git/remote-url").trim()
}
def getCommitSha() {
sh "git rev-parse HEAD > .git/current-commit"
return readFile(".git/current-commit").trim()
}
def updateGithubStatus(status, message) {
// status: pending, success, failure or error
repoUrl = getRepoURL()
commitSha = getCommitSha()
step ([
$class: 'GitHubCommitStatusSetter',
reposSource: [$class: "ManuallyEnteredRepositorySource", url: repoUrl],
commitShaSource: [$class: "ManuallyEnteredShaSource", sha: commitSha],
errorHandlers: [[$class: 'ShallowAnyErrorHandler']],
statusResultSource: [
$class: 'ConditionalStatusResultSource',
results: [
[$class: 'AnyBuildResult', state: status, message: message]
]
]
])
}