Quantcast

How to get build results from a build job in a pipeline

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to get build results from a build job in a pipeline

Jesse Kinross-Smith
How can I do this right - I want the results from a job I run (I need to run a dozen of these in succession and will email devs if one of them fails) 

try{ BuildResults = build job: 'testJob'; currentBuild.result='SUCCESS'; } catch(e){ currentBuild.result = 'FAILURE'; } finally { notify_email(BuildResults); }

if i do the above I only get a valid BuildResults in notify_email IF the job is successful, 
if it fails it causes an exception saying No such property: BuildResults

currentBuild is useless as it's the pipeline results, not the job results which is what I want

I need the try/catch so I can continue to run my other jobs - otherwise it'll stop immediately once one job fails

I'm sure there's some syntax I'm missing here, but I'm struggling to find it.

Any help you can provide is appreciated.

Regards,

Jesse

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/d11f9974-8a71-474a-9927-44cb0ab06a42%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to get build results from a build job in a pipeline

Bill Dennis
You could build the downstream jobs without propagating the error to the top level job calling them.

Then you could get the results from each downstream job and handle it to do the notifications according to SUCCESS/FAILURE/UNSTABLE etc.

I do this sort of thing using declarative pipeline then I do all the notifications in post { failure {}} sections in one place for the job or a stage (you can have post handling in declarative at the stage or job level).

I would recommend doing something like this:

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: SUCCESS


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 #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: FAILURE

I recommend using declarative pipeline!

--Bill

On Wednesday, 17 May 2017 03:45:27 UTC+1, Jesse Kinross-Smith wrote:
How can I do this right - I want the results from a job I run (I need to run a dozen of these in succession and will email devs if one of them fails) 

try{ BuildResults = build job: 'testJob'; currentBuild.result='SUCCESS'; } catch(e){ currentBuild.result = 'FAILURE'; } finally { notify_email(BuildResults); }

if i do the above I only get a valid BuildResults in notify_email IF the job is successful, 
if it fails it causes an exception saying No such property: BuildResults

currentBuild is useless as it's the pipeline results, not the job results which is what I want

I need the try/catch so I can continue to run my other jobs - otherwise it'll stop immediately once one job fails

I'm sure there's some syntax I'm missing here, but I'm struggling to find it.

Any help you can provide is appreciated.

Regards,

Jesse

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/db320dcc-c6ed-4373-b56b-9efcef78d960%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to get build results from a build job in a pipeline

Bill Dennis
In reply to this post by Jesse Kinross-Smith
Ah just saw you need the job to call all builds even if one fails. You can do it with a parallel section like this:

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)
}
}
}
}


And output looks like this:

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

--Bill

On Wednesday, 17 May 2017 03:45:27 UTC+1, Jesse Kinross-Smith wrote:
How can I do this right - I want the results from a job I run (I need to run a dozen of these in succession and will email devs if one of them fails) 

try{ BuildResults = build job: 'testJob'; currentBuild.result='SUCCESS'; } catch(e){ currentBuild.result = 'FAILURE'; } finally { notify_email(BuildResults); }

if i do the above I only get a valid BuildResults in notify_email IF the job is successful, 
if it fails it causes an exception saying No such property: BuildResults

currentBuild is useless as it's the pipeline results, not the job results which is what I want

I need the try/catch so I can continue to run my other jobs - otherwise it'll stop immediately once one job fails

I'm sure there's some syntax I'm missing here, but I'm struggling to find it.

Any help you can provide is appreciated.

Regards,

Jesse

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/8472bcdc-1c99-436a-91e7-00390111fb82%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to get build results from a build job in a pipeline

Jesse Kinross-Smith
In reply to this post by Jesse Kinross-Smith
So I was trying too hard - the try/catch isnt' needed at all.
All I needed was:

BuildResults = build job: 'testJob', propagate: false; notify_email(BuildResults); 


On Wednesday, May 17, 2017 at 10:45:27 AM UTC+8, Jesse Kinross-Smith wrote:
How can I do this right - I want the results from a job I run (I need to run a dozen of these in succession and will email devs if one of them fails) 

try{ BuildResults = build job: 'testJob'; currentBuild.result='SUCCESS'; } catch(e){ currentBuild.result = 'FAILURE'; } finally { notify_email(BuildResults); }

if i do the above I only get a valid BuildResults in notify_email IF the job is successful, 
if it fails it causes an exception saying No such property: BuildResults

currentBuild is useless as it's the pipeline results, not the job results which is what I want

I need the try/catch so I can continue to run my other jobs - otherwise it'll stop immediately once one job fails

I'm sure there's some syntax I'm missing here, but I'm struggling to find it.

Any help you can provide is appreciated.

Regards,

Jesse

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/d7b265b8-6152-4c6b-a4a8-1ed8ced20df6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to get build results from a build job in a pipeline

Jesse Kinross-Smith
In reply to this post by Bill Dennis
Thanks Bill - appreciate the response.

I found that not propagating the errors was the key - it would continue onto further tasks nicely if I did that.  Only issue really is that if you watch the pipeline in Jenkins then it appears to show all green even if a step has failed.
My build summary email works a treat though - each job I collate the results as it goes through each step and then email at the end indicates which jobs failed and which jobs succeeded.

I've actually put my script up here - https://github.com/Radix999/jenkins-pipeline-scripts/blob/master/Nightlybuild.groovy in case it's useful for others to look at.

I might make some tweaks to how I do things based on your input though - I like how you've broken buildJob into it's own function and I've never used the post{} sections so might separate the build email into that.

Regards,

Jesse

On Wednesday, May 17, 2017 at 5:55:33 PM UTC+8, Bill Dennis wrote:
Ah just saw you need the job to call all builds even if one fails. You can do it with a parallel section like this:

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)
}
}
}
}


And output looks like this:

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

--Bill

On Wednesday, 17 May 2017 03:45:27 UTC+1, Jesse Kinross-Smith wrote:
How can I do this right - I want the results from a job I run (I need to run a dozen of these in succession and will email devs if one of them fails) 

try{ BuildResults = build job: 'testJob'; currentBuild.result='SUCCESS'; } catch(e){ currentBuild.result = 'FAILURE'; } finally { notify_email(BuildResults); }

if i do the above I only get a valid BuildResults in notify_email IF the job is successful, 
if it fails it causes an exception saying No such property: BuildResults

currentBuild is useless as it's the pipeline results, not the job results which is what I want

I need the try/catch so I can continue to run my other jobs - otherwise it'll stop immediately once one job fails

I'm sure there's some syntax I'm missing here, but I'm struggling to find it.

Any help you can provide is appreciated.

Regards,

Jesse

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/27a9669f-fba1-467b-9f60-b42e71e27013%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...