41

In this integration pipeline in Jenkins, I am triggering different builds in parallel using the build step, as follows:

stage('trigger all builds')
{
  parallel
  {
    stage('componentA')
    {
      steps
      {
        script 
        {
          def myjob=build job: 'componentA', propagate: true, wait: true
        }
      }
    }
    stage('componentB')
    {
      steps 
      {
        script
        {
          def myjob=build job: 'componentB', propagate: true, wait: true
        }
      }
    }
  }
}

I would like to access the return value of the build step, so that I can know in my Groovy scripts what job name, number was triggered.

I have found in the examples that the object returned has getters like getProjectName() or getNumber() that I can use for this.

But how do I know the exact class of the returned object and the list of methods I can call on it? This seems to be missing from the Pipeline documentation. I am asking for this case in particular, but generally speaking, how can I know the class of the returned object and its documentation?

4 Answers 4

66

The step documentation is generated based on some files that are bundled with the plugin, which sometimes isn't enough. One easy way would be to just print out the class of the result object by calling getClass:

def myjob=build job: 'componentB', propagate: true, wait: true
echo "${myjob.getClass()}"

This output would tell you that the result (in this case) is a org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper which has published Javadoc.

For other cases, I usually have to dive into the Jenkins source code. Here is my general strategy:

2
  • 5
    is there a possibility for the triggered job to return a return code that indicates whether it was successfully finished or whether it has failed? If yes, please give an example Commented Dec 23, 2020 at 20:23
  • @anarchipur Please see my answer, which gives such an example.
    – not2savvy
    Commented Feb 13, 2023 at 20:01
6

The result of the downstream job is given in the result attribute of the returned object.

I recommend to use propagate: false to get control of how the result of the downstream job affects the current build.

Example:

pipeline{
    [...]
    stages {
        stage('Dummy') {
            steps {
                echo "Hello world #1"
            }
        }
        stage('Fails') {
            steps {
                script {
                    downstream = build job: 'Pipeline Test 2', propagate: false
                    if (downstream.result != 'SUCCESS') {
                        unstable(message: "Downstream job result is ${downstream.result}")
                    }
                }
 
            }
        }
    }
    [...]
}    

In this example, the current build is set to UNSTABLE whenever the downstream build has not been successful.

The result can be: SUCCESS, FAILURE, UNSTABLE, or ABORTED.

For your other question see Is there a built-in function to print all the current properties and values of an object? and Groovy / grails how to determine a data type?

0

I got the class path from build log: 13:20:52 org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper@5fe8d20f

and then I found the doc from https://javadoc.jenkins.io/, you can get all you need from this page link: https://javadoc.jenkins.io/plugin/workflow-support/org/jenkinsci/plugins/workflow/support/steps/build/RunWrapper.html

0

Thanks @jinzhi-li.

I tried the link https://javadoc.jenkins.io/plugin/workflow-support/org/jenkinsci/plugins/workflow/support/steps/build/RunWrapper.html

I was able to get the output. Samples:

def builder = build job: "TEST-JOBS/Test2", wait: true, propagate: false
println(builder.getAbsoluteUrl())
println(builder.getResult())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.