Is there a way in Jenkins (Hudson) to disable a job from another job?
Basically we have a build environment that does the standard building/testing. We also use the
Recent version of Jenkins (2 and over) change the method name.
Just replace disable()
with setDisabled(boolean)
Found that this worked for me:
Jenkins.instance.getItem("jobname").setDisabled(false)
We've used the Port Allocator Plugin to do this. It lets your jobs "grab" a TCP port for the duration of their run, meaning that no other job can grab that port for the duration, effectively blocking parallel execution of two jobs that try to get the same port.
There is also the Locks and Latches Plugin which sounds perfect but we found it to be difficult to use, and it doesn't seem to be maintained any more.
Update after question clarified:
You can remotely enable/disable a job using Jenkins' HTTP api. If you browse to any job then put api
on the end of the URL you will see instructions about the HTTP requests you need to make to do so. For example: http://ci.jruby.org/job/activerecord-jdbc-master/api (look at the end of that page - basically POST to <job_url>/disable
).
It should be a simple matter to use a build step in a job to call curl or similar with the appropriate request data.
It's possible to do this using the Job DSL Plugin, without specifying credentials.
The DSL script to use would be:
job("jobname"){
using("jobname")
disabled(true)
}
pipelineJob
instead of job
."jobname"
needs to include folders, using the Jenkins root as reference.disabled(false)
.You can execute a Groovy script as a pre build step. I have done this a couple of times at my company. You have to install the Groovy plugin. Then you can add a pre build step "Execute system groovy script" and there you can execute something like that:
Jenkins.instance.getItem("jobname").disable()
For enabling of jobs after the build add a post build step and execute
Jenkins.instance.getItem("jobname").enable()
If you want a job, that only enables or disables other build jobs, then you don't choose pre or post build steps, but a normal build step with a system Groovy script.
You can do many more thinks with Groovy scripts. It is worth to read more about it.
I had this same issue and solved it using dunni's answer, I need to point out that it worked only when I choose "Execute system Groovy script" as my build step, if I choose "Execute Groovy script", it gives the error reported by Arun Sangal: "Caught: groovy.lang.MissingPropertyException: No such property: Jenkins for class: hudson8158411085491171644".
My Groovy code looks like this:
import jenkins.*
import jenkins.model.*
try {
Jenkins.instance.getItem("jobname").disable()
} catch (Exception ex) {
printf("Error disabling jobname. " + ex.getMessage())
}
I found one possible approach using the jenkins-cli (command-line interface). I added this build step (for me it was a Windows batch command, though I may make it a script or something more elegant):
java -jar C:\my\jenkins\path\jenkins-cli.jar -s %JENKINS_URL% disable-job "Job 1"
I haven't looked into how to get the path to Jenkins, but that should not be too hard.
Here is some information on the CLI. You can also go to http://your-jenkins:8080/cli for some of the available commands.