try{ BuildResults = build job: 'testJob'; currentBuild.result='SUCCESS'; } catch(e){ currentBuild.result = 'FAILURE'; } finally { notify_email(BuildResults); }
def buildResults = [:]
void nofify_email(Map results) {
echo "TEST SIMULATE notify: ${results.toString()}"
}
pipeline {
agent any
stages {
stage('Build testJob') {
steps {
script {
def jobBuild = build job: 'testJob', propagate: false
def jobResult = jobBuild.getResult()
echo "Build of 'testJob' returned result: ${jobResult}"
buildResults['testJob'] = jobResult
if (jobResult != 'SUCCESS') {
error("testJob failed with result: ${jobResult}")
}
}
}
}
}
post {
always {
echo "Build results: ${buildResults.toString()}"
}
success {
echo "All builds completed OK"
}
failure {
echo "A job failed"
script {
nofify_email(buildResults)
}
}
}
}Here is what the output would look like for success and failure:Started by user anonymous [Pipeline] node Running on master in /var/jenkins_home/workspace/foo [Pipeline] { [Pipeline] stage [Pipeline] { (Build testJob) [Pipeline] script [Pipeline] { [Pipeline] build (Building testJob) Scheduling project: testJob Starting building: testJob #1 [Pipeline] echo Build of 'testJob' returned result: SUCCESS [Pipeline] } [Pipeline] // script [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Declarative: Post Actions) [Pipeline] echo Build results: [testJob:SUCCESS] [Pipeline] echo All builds completed OK [Pipeline] } [Pipeline] // stage [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline Finished: SUCCESSStarted by user anonymous [Pipeline] node Running on master in /var/jenkins_home/workspace/foo [Pipeline] { [Pipeline] stage [Pipeline] { (Build testJob) [Pipeline] script [Pipeline] { [Pipeline] build (Building testJob) Scheduling project: testJob Starting building: testJob #2 [Pipeline] echo Build of 'testJob' returned result: FAILURE [Pipeline] error [Pipeline] } [Pipeline] // script [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Declarative: Post Actions) [Pipeline] echo Build results: [testJob:FAILURE] [Pipeline] echo A job failed [Pipeline] script [Pipeline] { [Pipeline] echo TEST SIMULATE notify: [testJob:FAILURE] [Pipeline] } [Pipeline] // script [Pipeline] } [Pipeline] // stage [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline ERROR: testJob failed with result: FAILURE Finished: FAILUREI recommend using declarative pipeline!
Map buildResults = [:]
Boolean failedJobs = false
void nofify_email(Map results) {
echo "TEST SIMULATE notify: ${results.toString()}"
}
Boolean buildJob(String jobName, Map results) {
    def jobBuild = build job: jobName, propagate: false
    def jobResult = jobBuild.getResult()
    echo "Build of '${jobName}' returned result: ${jobResult}"
    results[jobName] = jobResult
    return jobResult == 'SUCCESS'
}
pipeline {
    agent any
    stages {
        stage('Parallel Builds') {
            steps {
                parallel(
                        "testJob1": {
                            script {
                                if (!buildJob('testJob1', buildResults)) {
                                    failedJobs = true
                                }
                            }
                        },
                        "testJob2": {
                            script {
                                if (!buildJob('testJob2', buildResults)) {
                                    failedJobs = true
                                }
                            }
                        },
                )
            }
        }
        stage('Completion') {
            steps {
                script {
                    if (failedJobs) {
                        error("One or more jobs have failed")
}
}
}
}
}
post {
always {
echo "Build results: ${buildResults.toString()}"
}
success {
echo "All builds completed OK"
}
failure {
echo "A job failed"
script {
nofify_email(buildResults)
}
}
}
}
Started by user anonymous
[Pipeline] node
Running on master in /var/jenkins_home/workspace/foo
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Parallel Builds)
[Pipeline] parallel
[Pipeline] [testJob1] { (Branch: testJob1)
[Pipeline] [testJob2] { (Branch: testJob2)
[Pipeline] [testJob1] script
[Pipeline] [testJob1] {
[Pipeline] [testJob2] script
[Pipeline] [testJob2] {
[Pipeline] [testJob1] build (Building testJob1)
[testJob1] Scheduling project: testJob1
[Pipeline] [testJob2] build (Building testJob2)
[testJob2] Scheduling project: testJob2
[testJob1] Starting building: testJob1 #8
[testJob2] Starting building: testJob2 #4
[Pipeline] [testJob2] echo
[testJob2] Build of 'testJob2' returned result: SUCCESS
[Pipeline] [testJob2] }
[Pipeline] [testJob2] // script
[Pipeline] [testJob2] }
[testJob1] Build of 'testJob1' returned result: FAILURE
[Pipeline] [testJob1] echo
[Pipeline] [testJob1] }
[Pipeline] [testJob1] // script
[Pipeline] [testJob1] }
[Pipeline] // parallel
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Completion)
[Pipeline] script
[Pipeline] {
[Pipeline] error
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] echo
Build results: [testJob2:SUCCESS, testJob1:FAILURE]
[Pipeline] echo
A job failed
[Pipeline] script
[Pipeline] {
[Pipeline] echo
TEST SIMULATE notify: [testJob2:SUCCESS, testJob1:FAILURE]
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: One or more jobs have failed
Finished: FAILURE