Tash
Tash

Reputation: 1

How to cancel a running Jenkins pipeline in jenkinsfile

Trying to cancel a running pipeline once a new job instance is triggered.

Found Here that setResult('FAILURE') might do the trick.

But seems not possible as generated the error:

java.lang.SecurityException: can only set the result property on the current build

Here is a snipped I am using:

    stage('Cancel running job') {
        steps {
                script {
                    def buildToCancel 
                    buildToCancel = currentBuild.getPreviousBuildInProgress()
                    buildToCancel.setResult('FAILURE') 
                }
        }
    }

And yet, cancelling a job from Jenkins UI is possible, so question is how to make it done from a jenkinsfile.

Upvotes: 0

Views: 1718

Answers (1)

Franco
Franco

Reputation: 193

You should have a look at the milestone step. Sounds like it's what you are trying to achieve.

By default, Pipeline builds can run concurrently. The milestone step ensures an older build will not override a newer build, so the older build will never be allowed to pass a milestone (it is aborted) if a newer build already passed it.

See: https://plugins.jenkins.io/pipeline-milestone-step/

Upvotes: 1

Related Questions