Groovy Script to Enable or Disable Jenkins Jobs

In Jenkins > build > Execute system Groovy script > Groovy command
Paste code like the below in the “Groovy Script” box:

f_disable ( 'My Jenkins Job One' )
f_disable ( 'My Jenkins Job Two' )
f_disable ( 'My Jenkins Job Three' )
f_disable ( 'My Jenkins Job Four' )
///////////////////////////////////////////////
import jenkins.*
import jenkins.model.*
def f_disable(i_jobname) {
    try {
        Jenkins.instance.getItem("$i_jobname").disable()
        println("$i_jobname disabled.")
    } catch (Exception ex) {
        println("Error disabling $i_jobname. " + ex.getMessage())
    }
}
println ( 'All done.' )

And for enabling Jenkins jobs would be:

f_enable ( 'My Jenkins Job One' )
f_enable ( 'My Jenkins Job Two' )
f_enable ( 'My Jenkins Job Three' )
f_enable ( 'My Jenkins Job Four' )
///////////////////////////////////////////////
import jenkins.*
import jenkins.model.*
def f_enable(i_jobname) {
    try {
        Jenkins.instance.getItem("$i_jobname").enable()
        println("$i_jobname enabled.")
    } catch (Exception ex) {
        println("Error enabling $i_jobname. " + ex.getMessage())
    }
}
println ( 'All done.' )
May 29, 2023

Leave a Reply

Your email address will not be published. Required fields are marked *