简体   繁体   中英

How to manipulate the build result of a Jenkins pipeline job (back to 'SUCCESS')?

I'm having some trouble to manipulate the build result of a Jenkins pipeline. I've narrowed it down to the following issue: anyone know why the following Jenkins pipeline doesn't make the build result SUCCESS? Instead the build fails.

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

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

I guess this is by design, "result can only get worse" in 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);
}

That's a bummer

对于更简单的答案,只需获取原始构建,然后直接设置字段:

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

That's works and can be executed from another job!

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

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

我用这个解决了这个问题

currentBuild.result = hudson.model.Result.FAILURE.toString()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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