How to manipulate the build result of a Jenkins pipeline job?

别说谁变了你拦得住时间么 提交于 2019-11-26 11:26:29

问题


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\'

回答1:


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




回答2:


For simplier answer, just get raw build, and set field directly:

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



回答3:


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



回答4:


Adding onto @metajiji's answer, you will need to approve the commands for hudson.model.result and project.getBuildByNumber in the main jenkins configuration



来源:https://stackoverflow.com/questions/38221836/how-to-manipulate-the-build-result-of-a-jenkins-pipeline-job

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!