I have around 100-120 jobs in one of the view called "Gradle Deploys" that I created in Jenkins. How can I disable all the jobs from Jenkins only from a given View / tab.
I tried the following groovy syntax to first just show all the jobs in a given view but it errors out.
jenkins = Hudson.instance
//The following works actually but gives a lot of info.
//println "----" + jenkins.instance.getView("Gradle Deploys").items
println "----" + jenkins.instance.getView("Gradle Deploys").items.each.getItems().print(it)
Once I get the list of just job names in a given view, I just have to use ".disable()" function in the above command and it'll work.
If I use the following code, it does what I want, but I'm looking for a one liner.
for (item in jenkins.instance.getView("Gradle Deploys").items) {
println("\nJob: $item.name")
item.disabled=true
}
Thanks to Tim for his solution. I'm adding/enhancing it a bit further:
jenkins.instance.getView("Gradle Deploys").items*.disabled = true
But if you want to print something out at the same time, you'll need an each
jenkins = Hudson.instance
jenkins.instance.getView("Gradle Deploys").items.each { item ->
println "\nJob: $item.name"
item.disabled = true
}
Now, the above examples works perfectly if you try to run them from "Script Console" but it errors out if you try to create/run it as a Scriptler script (as shown below).
See: The above code works in Script Console view in Jenkins (When you click Manage Jenkins > Script Console). To get this, you may need the plugin installed.
Now, the same script didn't work when I tried to create a Scripter Script and ran it that way. This requires Scriptler Plugin installed.
To Solve the above error message (as shown in the Scriptler Script - window), you need to enter another line (at top).
Final script looks like (NOTE: Value of viewName variable will be provided by the Scriptler parameter and it'll overwrite whatever you'll mention in the script itself):
//The following line is necessary if you are running the script/code via a "Scriptler Script" script way. //This way you can prompt a user to provide parameters (for ex: viewName) and use it to disable job in that view only.
import hudson.model.*
jenkins = Hudson.instance
println ""
println "--- Disabling all jobs in view: ${viewName}"
println ""
jenkins.instance.getView(viewName).items*.disabled = true
//Now the above will disable it but you still need to save it. Otherwise, you'll loose your changes (of disabling the jobs) after each Jenkins restart.
jenkins.instance.getView(viewName).items.each { item -> item.save() }