简体   繁体   English

Jenkins Groovy:触发构建的原因

[英]Jenkins Groovy: What triggered the build

I was thinking of using a Groovy script for my build job in Jenkins because I have some conditions to check for that might need access to Jenkins API. 我正在考虑在Jenkins中使用Groovy脚本进行构建作业,因为我有一些条件要检查可能需要访问Jenkins API。

Is it possible to find out who or what triggered the build from a Groovy script? 是否有可能从Groovy脚本中找出触发构建的人或者是什么? Either an SCM change, another project or user. SCM更改,另一个项目或用户。 I have just begun reading a little about Groovy and the Jenkins API. 我刚刚开始阅读一些关于Groovy和Jenkins API的内容。

I want to check for the following conditions and build accordingly. 我想检查以下条件并进行相应的构建。 Some Pseudocode: 一些伪代码:

def buildTrigger JenkinsAPI.thisBuild.Trigger
if (buildTrigger == scm) {
   execute build_with_automake
   def new_version = check_git_and_look_up_tag_for_version
   if (new_version) {
      execute git tag new_release_candidate
      publish release_candidate
   }
} else if (buildTrigger == "Build other projects") {
  execute build_with_automake
}

The project should build on every SCM change, but only tag and publish if version has been increased. 该项目应建立在每个SCM变更的基础上,但仅在版本增加时才标记和发布。 It should also build when a build has been triggered by another project. 它还应该在另一个项目触发构建时构建。

I have something similar - I wanted to get the user who triggered the build, this is my code: 我有类似的东西 - 我想让触发构建的用户,这是我的代码:

for (cause in bld.getCauses()) {
    if (cause instanceof Cause.UserIdCause) {
        return cause.getUserName()
    }
}

(bld is subtype of Run) (bld是Run的子类型)

So, you can get the causes for your build, and check for their type. 因此,您可以获取构建的原因,并检查其类型。

See the different types at Cause javadoc http://javadoc.jenkins-ci.org/hudson/model/Cause.html 请参阅原因javadoc中的不同类型http://javadoc.jenkins-ci.org/hudson/model/Cause.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM