25

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'
3
  • Have you tried doing the whole script inside single node? Jul 8, 2016 at 18:34
  • Just tried that, but same behaviour.
    – Joost
    Jul 12, 2016 at 5:38
  • why don't you keep it in a variable, and then set it once you are ready?
    – janDro
    Jun 18, 2018 at 17:17

5 Answers 5

26

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

3
  • 3
    The idea is to ignore results of jobs inside pipeline by setting Propagate to false
    – MarcusUA
    Oct 21, 2016 at 11:21
  • 1
    @MarcusUA how can I ignore result of a phase, not external job? Jun 23, 2017 at 12:26
  • @VadimKotov ignore results for each job inside phase.
    – MarcusUA
    Jun 27, 2017 at 12:15
16

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

currentBuild.rawBuild.@result = hudson.model.Result.SUCCESS
3
  • This is the one which worked for me. A plugin (NUnit Publishing) was setting the stage result to "unstable" via the "build.setResult" method, so error catching wasn't working. Adding this to the end of the stage done the job.
    – Mr Chris
    Dec 6, 2019 at 10:25
  • yes, this is the one. it is a pitty that TextFinder plugin doesn't have this implemented, with it you can set build only to unstable, not_built or success...
    – YaP
    Mar 11, 2020 at 21:01
  • you just saved me hours of tricking around. Thanks
    – raddirad
    Feb 14 at 11:32
4

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
1

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

1

I resolved this by using this

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.