Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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,

like image 667
Geist Avatar asked Apr 25 '18 21:04

Geist


People also ask

What is a trigger in Jenkins?

A trigger lets us execute a job on an event occurrence. This event is called a trigger. To see the list of build triggers, we need to login to Jenkins and click on any item already created and click on configure. Trigger build remotely: The job is usually triggered by accessing a specified URL. This is convenient for scripts.

How do I check how long a Jenkins job takes to run?

If you want to know how long a job takes to run, then you need to get down to the build level. To get build metadata, you need to call get_build_info (). This method takes in the job name and the build number and returns the metadata as a dictionary. You can do a lot more with the Python Jenkins package.

What are the different types of Jenkins jobs?

As shown in the above figure, different types of Jenkins Jobs are: (i) Freestyle Project: This is a regular and popular job in Jenkins which allows us to build our project, integrate our builds or source code management with Jenkins, poll the SCM, create triggers, and many more. (ii) Maven Project: Enables us to build our maven projects.

How do I get the build metadata in Jenkins?

To get build metadata, you need to call get_build_info (). This method takes in the job name and the build number and returns the metadata as a dictionary. You can do a lot more with the Python Jenkins package.


2 Answers

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.

like image 177
mkobit Avatar answered Oct 17 '22 02:10

mkobit


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/"
like image 25
Geist Avatar answered Oct 17 '22 04:10

Geist