Geist
Geist

Reputation: 197

Jenkins: get the build number of a Job triggered inside pipeline

I have a pipeline which runs another bunch of jobs inside a stage:

node{
 stage("building_other_components") {
  build 'job1' 
  build 'job2' }}

How can I recover the build number or URL of these jobs? I just want to send the URL by mail ( Example: http://localhost:8080/job/job1/25/last-changes/ , I will add the last-changes part)
Thanks,

Upvotes: 8

Views: 18491

Answers (2)

mkobit
mkobit

Reputation: 47239

As long as you wait for the run to complete (defaults true), you can access the result from the returned value of the build step. The returned value is of the type of org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper (Javadoc, source code). You can see the help for the build step by using the snippet generator.

Using part of your code as an example:

final job1Result = build('job1')
echo "Job 1 number: ${job1Result.number}"
final job2Result = build('job2')
echo "Job 2 number: ${job2Result.number}"

This uses the getNumber() method to get the number of the executed run.

Upvotes: 9

Geist
Geist

Reputation: 197

In case of it is useful for someone:

def job1_props = build 'job1'
def j1EnvVariables = job1_props.getBuildVariables();
print "${j1EnvVariables}" 

inside the j1EnvVariables the environment variable BUILD URL is present: BUILD_URL:http://localhost:8080/job/job1/26/ and the BUILD_NUMBER:26 and another useful information to access:

def path1 =" ${j1EnvVariables1.BUILD_URL}last-changes/"

Upvotes: 3

Related Questions