Jenkins声明式管道:找出触发工作 [英] Jenkins declarative pipeline: find out triggering job

查看:283
本文介绍了Jenkins声明式管道:找出触发工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个Jenkins作业,它使用声明性管道

We have a Jenkins job that uses a declarative pipeline.

此作业可以由其他不同版本触发.

This job can be triggered by different other builds.

在声明性管道中,如何找出触发该管道的构建?

In the declarative pipeline how can I find out which build has triggered the pipeline?

推荐答案

下面的代码示例


pipeline {
    agent any
    stages {
        stage('find upstream job') {
            steps {
                script {
                    def causes = currentBuild.rawBuild.getCauses()
                    for(cause in causes) {
                        if (cause.class.toString().contains("UpstreamCause")) {
                            println "This job was caused by job " + cause.upstreamProject
                        } else {
                            println "Root cause : " + cause.toString()
                        }
                    }
                }      
            }
        }
    }
}

您可以检查作业的REST API,以获取如下所示的其他信息

You can check the job's REST API to get extra information like below


{
  "_class" : "org.jenkinsci.plugins.workflow.job.WorkflowRun",
  "actions" : [
    {
      "_class" : "hudson.model.ParametersAction",
      "parameters" : [

      ]
    },
    {
      "_class" : "hudson.model.CauseAction",
      "causes" : [
        {
          "_class" : "hudson.model.Cause$UpstreamCause",
          "shortDescription" : "Started by upstream project \"larrycai-sto-46908390\" build number 7",
          "upstreamBuild" : 7,
          "upstreamProject" : "larrycai-sto-46908390",
          "upstreamUrl" : "job/larrycai-sto-46908390/"
        }
      ]
    },

参考:

  • https://jenkins.io/doc/pipeline/examples/#get-build-cause
  • Get Jenkins upstream jobs

这篇关于Jenkins声明式管道:找出触发工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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