Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins Pipeline script - return value of a build step

Is there a way to fetch URL of a build step (without waiting for completion) through Jenkins pipeline script?

Here is what I've tried but the return value of build is null.

def build_job = build job: 'dummy_job', wait: false

Trying to fetch URL as follows: build_job.absoluteUrl

like image 657
deebee Avatar asked Sep 26 '17 18:09

deebee


People also ask

Can we run a step conditionally in Jenkins?

Execution of the pipeline stages can be controlled with conditions. These conditions must be defined in the when block within each stage. Jenkins supports a set of significant conditions that can be defined to limit stage execution. Each when block must contain at least one condition.

What is returnStdout?

returnStdout : boolean (optional) If checked, standard output from the task is returned as the step value as a String , rather than being printed to the build log. (Standard error, if any, will still be printed to the log.) You will often want to call . trim() on the result to strip off a trailing newline.

What is sh command in Jenkins pipeline?

On Linux, BSD, and Mac OS (Unix-like) systems, the sh step is used to execute a shell command in a Pipeline. Jenkinsfile (Declarative Pipeline) pipeline { agent any stages { stage('Build') { steps { sh 'echo "Hello World"' sh ''' echo "Multiline shell steps works too" ls -lah ''' } } } }


1 Answers

You can get it by using the getRawBuild() method:

   def build_job=build(job:'dummy_job',propagate:false)
   echo build_job.getResult()
   echo build_job.getRawBuild().getAbsoluteUrl()

Don't use the wait: false since the function won't return the expected result. don't use the propagate: false so the job won't fail before the next step if the called job fails.

like image 191
yorammi Avatar answered Oct 09 '22 19:10

yorammi