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

二次信任 提交于 2019-12-02 04:33:45

问题


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,


回答1:


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.




回答2:


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/"


来源:https://stackoverflow.com/questions/50031744/jenkins-get-the-build-number-of-a-job-triggered-inside-pipeline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!