Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'
like image 407
Joost Avatar asked Jul 06 '16 10:07

Joost


People also ask

How do I rerun a failed Jenkins job?

To retry a Jenkins job, you can use the Naginator plugin. Simply install the plugin, and then check the Post-Build action "Retry build after failure" on your project's configuration page. If the build fails, it will be rescheduled to run again after the time you specified.

How do you run the job in Jenkins after successful execution of the another job?

Select a job that triggers a remote one and then go to Job Configuration > Build section > Add Build Step > Trigger builds on remote/local projects option. This configuration allows you to trigger another exciting job on a different CM (remote). The downstream job name part will autocomplete.

How does Mark Jenkins build a success?

-The if statement simply does the following: if I get the expected exit code, exit with 0 so that the build is marked as SUCCESS , else exit with 1 so that the build is marked as FAILURE . Note there is no need to call 'bash', as you are already in a shell.

How do you skip failed stage in Jenkins pipeline?

To ignore a failed step in declarative pipeline you basically have two options: Use script step and try-catch block (similar to previous proposition by R_K but in declarative style)


4 Answers

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

like image 172
Joost Avatar answered Oct 16 '22 13:10

Joost


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

currentBuild.rawBuild.@result = hudson.model.Result.SUCCESS
like image 21
Cheng Bao Avatar answered Oct 16 '22 12:10

Cheng Bao


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
like image 4
METAJIJI Avatar answered Oct 16 '22 11:10

METAJIJI


I resolved this by using this

currentBuild.result = hudson.model.Result.FAILURE.toString()
like image 1
AnilS Avatar answered Oct 16 '22 12:10

AnilS