How to change build status from abort to failure

Is it possible to change the build status from an abort to a failure?
This is what I have tried so far and none work:

@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
}

pipeline {
    agent { label "test_builder" }


    stages {
        stage('sleep') {
            steps {
                script {
                    sleep 100000 // abort the build in the build of sleep
                }
            }
        }

    }
    post {
        aborted {
            script {
                // approach 1
                currentBuild.rawBuild.result = hudson.model.Result.FAILURE

                // approach 2
                def project = getProject('test_abort')
                def build = project.getBuildByNumber(BUILD_NUMBER as int)
                build.@result = hudson.model.Result.FAILURE
                
                // approach 3
               currentBuild.rawBuild.setResult(hudson.model.Result.FAILURE)
            }
        }
    }
}

After doing some digging, it looks like the build status can only get worse, and aborted seems to be considered worse than failure? Any ideas on how to get around this?

Another option I’ve been looking into is if it’s possible to fail a build from a completely separate build.
Example: Job A is running and sends an error to any builds that are running for Job B.

That example is similar to what I’m currently doing, except I can only figure out how to abort the build using doTerm() instead of causing a failure; hence the original question about changing from an abort status to a failure.

I wonder why does one even want to do this…

I have a parallel pipeline where most of the time I want fail fast disabled, but in special circumstances I want to be able to “fail fast” all of the parallel builds.

1 Like

I was able to change the build result, but it looks like something else in the pipeline is holding onto that ABORTED status and changing it back to ABORTED after the build, no matter what. Here is what I tried again:

pipeline {

    agent { label "Test_Builder" }

    stages {
        stage('Clean Workspace') {
            steps {
                cleanWs()
            }
        }

        stage('test') {
            steps{
                script {
                    println(currentBuild.rawBuild.getResult())
                    currentBuild.rawBuild.@result = hudson.model.Result.FAILURE
                    println(currentBuild.rawBuild.getResult())
                    sleep 10000 // call an abort at this point
                }
            }
        }
    }
    post {
        aborted {
            script {
                println(currentBuild.rawBuild.getResult())
                currentBuild.rawBuild.@result = hudson.model.Result.FAILURE
                println(currentBuild.rawBuild.getResult())
            }
        }
        cleanup {
            script {
                println(currentBuild.rawBuild.getResult())
                currentBuild.rawBuild.@result = hudson.model.Result.SUCCESS
                println(currentBuild.rawBuild.getResult())
            }
        }
    }
}

And you can see by this output that the build result does change, but it reverts back after it leaves each of the post step stages:

00:00:06.630  [Pipeline] // stage
00:00:06.720  [Pipeline] stage
00:00:06.738  [Pipeline] { (Declarative: Post Actions)
00:00:06.834  [Pipeline] script
00:00:06.853  [Pipeline] {
00:00:06.926  [Pipeline] echo
00:00:06.943  ABORTED
00:00:06.984  [Pipeline] echo
00:00:07.006  FAILURE
00:00:07.028  [Pipeline] }
00:00:07.109  [Pipeline] // script
00:00:07.174  [Pipeline] script
00:00:07.193  [Pipeline] {
00:00:07.270  [Pipeline] echo
00:00:07.287  ABORTED
00:00:07.325  [Pipeline] echo
00:00:07.345  SUCCESS
00:00:07.367  [Pipeline] }
00:00:07.450  [Pipeline] // script
00:00:07.485  [Pipeline] }
00:00:07.559  [Pipeline] // stage
00:00:07.595  [Pipeline] }
00:00:07.693  [Pipeline] // node
00:00:07.781  [Pipeline] End of Pipeline
00:00:07.979  Finished: ABORTED

This is so outside my knowledge level, but I’m thinking the build looks at currentBuild.result, sees its not set, and sets it to whatever it needs to be.

What happens if you do

        currentBuild.result = 'ABORTED'

or currentBuild.rawBuild.save(), though that makes me nervious. rawBuild is pretty low level api and I don’t know what the impacts are about changing it during runs.

1 Like

I get an error when I try to access currentBuild.result telling me it doesn’t exist. It seems to only exist on currentBuild.rawBuild.
I tried running save() as well, but no effect.

I’ve been browsing through the java docs for something that might help, but I can’t find anything. I’m starting to get the feeling this is impossible
https://javadoc.jenkins.io/plugin/workflow-job/org/jenkinsci/plugins/workflow/job/WorkflowRun.html#save--

I have a parallel pipeline where most of the time I want fail fast disabled, but in special circumstances I want to be able to “fail fast” all of the parallel builds.

do you know this ahead of time, or only based on the failure of one of the branches?

there is no set the result from ABORTED > FAILED as you can only set a worse result.
in scripted you can wrap the parts with a try / catch and then handle the aborted there and instead set the status to ERROR (if need be), or ABORTED.

1 Like