如何操作Jenkins管道作业的构建结果?

时间:2016-07-06 10:29:48

标签: jenkins jenkins-pipeline

我在操作Jenkins管道的构建结果时遇到了一些麻烦。我已将其缩小到以下问题:任何人都知道为什么以下Jenkins管道不会使构建结果成功?相反,构建失败。

print "Setting result to FAILURE"
currentBuild.result = 'FAILURE'

print "Setting result to SUCCESS"
currentBuild.result = 'SUCCESS'

5 个答案:

答案 0 :(得分:16)

我想这是设计的,“结果只会变得更糟”setResult()

// result can only get worse
if (result==null || r.isWorseThan(result)) {
    result = r;
    LOGGER.log(FINE, this + " in " + getRootDir() + ": result is set to " + r, LOGGER.isLoggable(Level.FINER) ? new Exception() : null);
}

那真是个无赖

答案 1 :(得分:3)

为得到更简单的答案,只需获取原始构建,然后直接设置字段即可:

currentBuild.rawBuild.@result = hudson.model.Result.SUCCESS

答案 2 :(得分:1)

这是有效的,可以从另一份工作中执行!

import com.cloudbees.groovy.cps.NonCPS
import jenkins.model.*
import hudson.model.Result

@NonCPS
def getProject(projectName) {
    // CloudBees folder plugin is supported, you can use natural paths:
    // in a postbuild action use `manager.hudson`
    // in the script web console use `Jenkins.instance`
    def project = jenkins.model.Jenkins.instance.getItemByFullName(projectName)
    if (!project) {error("Project not found: $projectName")}
    return project
}

project = getProject('foo/bar')
build = project.getBuildByNumber(2443)
// build = project.getBuild(project, '2443')

build.@result = hudson.model.Result.SUCCESS
// build.@result = hudson.model.Result.NOT_BUILT
// build.@result = hudson.model.Result.UNSTABLE
// build.@result = hudson.model.Result.FAILURE
// build.@result = hudson.model.Result.ABORTED

答案 3 :(得分:0)

在@metajiji的答案上,您需要批准主要jenkins配置中的hudson.model.resultproject.getBuildByNumber的命令

答案 4 :(得分:0)

我通过使用此方法解决了

currentBuild.result = hudson.model.Result.FAILURE.toString()
相关问题