1

Is there a way to disable a Jenkins job from inside a pipeline?
I use the Disable Failed Job plugin very heavily https://plugins.jenkins.io/disable-failed-job , so that when a Jenkins job fails, it is automatically disabled. This is handy for the workflow that I have.

This plugin unfortunately does not work with Jenkins Pipeline.

Craig Rodrigues
  • 771
  • 2
  • 7
  • 16

1 Answers1

3

Stuart Rowe helped me come up with the following:

pipeline {
    agent any
    stages {
        stage('disable build') {
            steps {
                script {
                   // See:
                   //    https://ci.jenkins.io/pipeline-syntax/globals#currentBuild
                   //    https://javadoc.jenkins.io/plugin/workflow-support/org/jenkinsci/plugins/workflow/support/steps/build/RunWrapper.html
                   //    https://javadoc.jenkins.io/jenkins/model/ParameterizedJobMixIn.ParameterizedJob.html#setDisabled-boolean-

               currentBuild.rawBuild.getParent().setDisabled(true)

                }
            }
        }
    }
}
Craig Rodrigues
  • 771
  • 2
  • 7
  • 16
  • 1
    Interesting - does this require any special permissions or security settings? – BitwiseMan Feb 07 '20 at 18:09
  • @BitwiseMan It works perfectly, however you need admin approval to run the pipeline outside the groovy sandbox. See https://plugins.jenkins.io/script-security/ for more info. – brk3 Feb 17 '21 at 16:10