-
Notifications
You must be signed in to change notification settings - Fork 9
/
changed-notify-success.groovy
67 lines (57 loc) · 1.7 KB
/
changed-notify-success.groovy
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
//
// Pipeline to handle build result changing from failure to success. For example, if it needs to send an email when it recovers.
//
// ** Trying to notify changed only for success from fail, not report twice when changed to fail **
//
// Used to track failure any stages using a stage level post section
Boolean buildFailed = false
pipeline {
agent any
parameters {
booleanParam(name: 'FAIL', defaultValue: false, description: 'Check to make it fail')
}
stages {
stage('Test') {
steps {
script {
if(params.FAIL == true) {
echo "This build will fail"
error("Build has failed")
}
else {
echo "This build is a success"
}
}
}
// Do this on every stage to detect failures
post { failure { script { buildFailed = true } } }
}
stage('Another') {
steps { echo 'another stage..' }
post { failure { script { buildFailed = true } } }
}
}
post {
always {
echo "Build completed"
}
changed {
echo 'Build result changed'
// Notify only on change to success
script {
if(!buildFailed) {
echo 'NOTIFY: Build changed to SUCCESS'
}
}
}
failure {
echo 'NOTIFY: Build failed'
}
success {
echo 'Build was a success'
}
unstable {
echo 'Build has gone unstable'
}
}
}