Jenkins-如何在下游获取和使用上游信息 [英] Jenkins - How to get and use upstream info in downstream

查看:398
本文介绍了Jenkins-如何在下游获取和使用上游信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行称为"A"的上游作业. A成功执行下游项目"B"的测试用例.但是,当从B发送邮件时,我们必须在邮件中包含上游项目详细信息(上游项目名称,构建号).因此,我们可以轻松地将相应上游作业的测试运行图化/关联化.

Executing upstream job called "A". On success of A executing test cases which is downstream project "B". But while sending mail from B we have to incorporate upstream project details (upstream project name, build no) in mail. So we can easily map / corelate the test run with respective upstream job.

在下游项目仪表板中,下面显示了详细信息.

In downstream project dashboard below details are displaying.

Started by upstream project Dev_RM_3.0_CI_Test build number 10
originally caused by:

我签入了 https://wiki.jenkins-ci .org/display/JENKINS/Building + a + software + project .但找不到任何要在下游继承的东西.

I checked in https://wiki.jenkins-ci.org/display/JENKINS/Building+a+software+project. but couldnt find anything to inherit in downstream.

使用以下详细信息创建示例作业以显示当前作业详细信息.

Created sample job with below details to display the current job details.

echo $BUILD_NUMBER
echo $JOB_NAME
echo $BUILD_ID

但是输出是

Building on master in workspace /var/lib/jenkins/workspace/env
[env] $ /bin/sh -xe /tmp/hudson970280339057643719.sh
+ echo 1
1
+ echo env
env
+ echo 1
1
Finished: SUCCESS

  1. 对继承下游作业中的上游详细信息有帮助吗?
  2. 如何获取当前的工作详细信息?

推荐答案

例如,您引用问题"Started by upstream project "Chained/1-First"内部版本号34"的消息可在jenkins中找到

The message that you refer to your question "Started by upstream project "Chained/1-First" build number 34" for example, is available in the jenkins Cause.

Jenkins将上游构建信息保留在它的cause对象中.如果您使用的是内部DSL或管道,则可能会遇到麻烦.或者,您可以curl作业URL并使用jq获取原因

Jenkins keeps the upstream build info in it's cause object. If your are using build DSL or Pipelines you may get it in groovy. Alternatively you can curl the job url and use jq to get the Cause

例如curl http://localhost:8080/job /Chained/job/2-Second/17/api/json

"_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
"actions": [{
  "_class": "hudson.model.CauseAction",
  "causes": [{
    "_class": "hudson.model.Cause$UpstreamCause",
    "shortDescription": "Started by upstream project \"Chained/1-First\" build number 34",
    "upstreamBuild": 34,
    "upstreamProject": "Chained/1-First",
    "upstreamUrl": "job/Chained/job/1-First/"
  }]
}

或者例如通过管道:

node() {
    stage('downstream') {
        def upstream = currentBuild.rawBuild.getCause(hudson.model.Cause$UpstreamCause)
        echo upstream?.shortDescription
    }
}

您可以从原因中获取大量信息,以待所有脚本批准或全局共享步骤.如果其他原因触发此构建(例如,提交或用户),您将得到null.

You can get a bunch of information out of Cause, pending all the script approvals or a global shared step. You will get a null if a different cause triggers this build, eg commit, or user.

这篇关于Jenkins-如何在下游获取和使用上游信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆