Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a Jenkins job be aborted with success result?

In a declarative pipeline, using a multibranch job and a Jenkinsfile, can the job be aborted with a success exit code rather than a failure exit code?

In the below stage, I basically check if the commit that started the job contains "ci skip", and if it does I want to abort the job.

Using error aborts the job but also marks it as such (red row). I'd like this job to be marked with a green row.

stage ("Checkout SCM") {
   steps {
      script {
         checkout scm
         result = sh (script: "git log -1 | grep '.*\\[ci skip\\].*'", returnStatus: true) 
         if (result == 0) {
            error ("'ci skip' spotted in git commit. Aborting.")
         }
      }
   }
}

Edit:

Instead of the above, I am now trying to simply skip all stages in case the git commit contains "ci skip". My understanding is that if the result in expression is false, it should skip the stage...

pipeline {
    environment {
        shouldBuild = "true"
    }
    ...
    ...
    stage ("Checkout SCM") {
        steps {
            script {
                checkout scm
                result = sh (script: "git log -1 | grep '.*\\[ci skip\\].*'", returnStatus: true) 
                if (result == 0) {
                    echo ("'ci skip' spotted in git commit. Aborting.")
                    shouldBuild = "false"
                }
            }
        }
    }

    stage ("Unit tests") {
        when {
            expression {
                return shouldBuild
            }
        }
        steps {
            ...
        }
    }
    ...
    ...
}
like image 931
Idan Adar Avatar asked Mar 25 '17 13:03

Idan Adar


People also ask

How do I abort a Jenkins job?

Pipeline jobs can be stopped by sending an HTTP POST request to URL endpoints of a build. BUILD ID URL/stop - aborts a Pipeline. BUILD ID URL/term - forcibly terminates a build (should only be used if stop does not work). BUILD ID URL/kill - hard kill a pipeline.

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

You can follow the below steps to trigger a Jenkins pipeline in another Jenkins pipeline. 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.

How do I abort all builds in Jenkins?

The builds in the pending queue can be easily cancelled from the Jenkins UI by manually clicking on the red "x" icon next to each build.

How do I stop Jenkins pipeline if stage fails?

Alternatively you can call error(String message) step to stop the pipeline and set its status to FAILED . For example, if your stage 1 calls error(msg) step like: stage("Stage 1") { steps { script { error "This pipeline stops here!" } } }


1 Answers

Okay, so the way to get this to work is not with the environment directive but rather to use the parametersdirective.

That is, adding parameters at the top of the pipeline:

parameters {
      booleanParam(defaultValue: true, description: 'Execute pipeline?', name: 'shouldBuild')
   }

When checking for the git commit, if it contains "ci skip" I change the value of shouldBuild:

env.shouldBuild = "false"

Then in expression:

expression {
    return env.shouldBuild != "false" 
}

And that's it. In case the git commit contains "ci skip" the stages are skipped and the job finishes with SUCCESS.

like image 178
Idan Adar Avatar answered Nov 15 '22 07:11

Idan Adar