1

I'm trying to figure out how to determine what caused a build to run from inside a scripted Jenkinsfile. The reason is that I have a script in a docker container that I want to run on a cron job, so when the cron job triggers, I just want it to run the container, but when I push changes, I want it check out the code, rebuild the container, run static code analysis, run tests, etc. There's no need for all of that on a cron run.

How can I get the cause? I tried currentBuild.getCauses(), but I get

groovy.lang.MissingMethodException: No signature of method: org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper.getCauses() is applicable for argument types: () values: []

I tried println currentBuild.getRawBuild().getCauses(), but got

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use method org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper getRawBuild

How cna i get the cause of a build in my jenkinsfile?

4

3 回答 3

2

此 Groovy 代码将获取触发项目的名称(它在 Groovy 沙箱中运行良好):

String getTriggeringProjectName() {
    if (currentBuild.upstreamBuilds) {
        return currentBuild.upstreamBuilds[0].projectName
    } else {
        return ""
    }
}
于 2020-10-13T22:49:55.337 回答
1

触发构建的项目名称 ( getUpstreamProject ) 和构建号 ( getUpstreamBuild ):

currentBuild.rawBuild.getCauses().get(0).getUpstreamProject()
currentBuild.rawBuild.getCauses().get(0).getUpstreamBuild()

或者我认为可能不需要更改权限的另一种方式: currentBuild.getUpstreamBuilds().get(0).getProjectName()

我正在做的事情中需要上游项目的分支:

currentBuild.getUpstreamBuilds().get(0).getRawBuild().getEnvVars().get("BRANCH_NAME", "")

这是关于那个的文档:getEnvVars()

顺便说一句,您必须启用混乱的权限。

于 2018-10-12T22:58:24.450 回答
1
manager.build.causes

要使用,您需要批准这些签名

method org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildRecorder$BadgeManager getBuild
method hudson.model.Run getCauses

希望能帮助到你

于 2018-05-22T14:04:13.140 回答