【问题标题】:Jenkins: get the build number of a Job triggered inside pipeline詹金斯:获取在管道内触发的作业的内部版本号
【发布时间】:2018-04-25 21:40:44
【问题描述】:

我有一个管道,它在一个阶段内运行另一堆作业:

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

如何恢复这些作业的内部版本号或 URL?我只想通过邮件发送 URL(例如:http://localhost:8080/job/job1/25/last-changes/,我将添加 last-changes 部分)
谢谢,

【问题讨论】:

    标签: jenkins jenkins-pipeline


    【解决方案1】:

    只要等待运行完成(默认为true),您就可以从build 步骤的返回值中访问结果。返回值的类型为org.jenkinsci.plugins.workflow.support.steps.build.RunWrapperJavadocsource code)。您可以使用snippet generator 查看build 步骤的帮助。

    以你的部分代码为例:

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

    这使用getNumber() 方法来获取执行的运行次数。

    【讨论】:

    • 如果我同时触发两次相同的作业怎么办?即我的下游工作具有相同的名称
    【解决方案2】:

    如果它对某人有用:

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

    在 j1EnvVariables 中存在环境变量 BUILD URL:BUILD_URL:http://localhost:8080/job/job1/26/ 以及 BUILD_NUMBER:26 和另一个有用的信息 访问:

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

    【讨论】:

    • def job = build job: '' EnvVariable = job.getBuildVariables(); echo "${EnvVariable}" 给了我一个空数组。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-30
    • 2018-04-16
    • 2018-01-29
    • 2014-01-21
    • 2014-07-10
    相关资源
    最近更新 更多