13
votes

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 same Jenkins instance to do some other stuff like install new versions of our product automatically (which is needed for some of the automated tests). When we are running some of the "extra" jobs, we want to disable the "standard" jobs temporarily (and then automatically enable them later).

Ideally there would be some extra build step on a job to say "Disable XYZ job". Or if there is a way through ANT or something to tell Jenkins to disable a job, that would work too.

Update: It looks like there are a couple plugins that will prevent two jobs from running at the same time, but I really need to:

  1. Run job A which disables job 1
  2. Do some stuff outside of Jenkins based on output of job A
  3. Run job B which which enable job 1 again
7

7 Answers

28
votes

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.

7
votes

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.

4
votes

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.

3
votes

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())
}
1
votes

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)

0
votes

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)
}
  1. This is for Freestyle jobs. If you need to disable, for example, a Pipeline Job, use pipelineJob instead of job.
  2. Usually, "jobname" needs to include folders, using the Jenkins root as reference.
  3. To reenable a job, use disabled(false).