jenkinsgroovyjenkins-pipeline

Jenkins pipeline: return value of build step


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?


Solution

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