Disable Jenkins Job from Another Job
Asked Answered
O

9

14

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
Overdraw answered 28/12, 2011 at 21:9 Comment(0)
O
7

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.

Overdraw answered 29/12, 2011 at 2:32 Comment(0)
W
28

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.

Whipstall answered 29/12, 2011 at 8:24 Comment(4)
For this to work, you need import jenkins.model.Jenkins at the beginning of the Groovy script.Foulard
Didn't work! tried both import jenkins.model.Jenkins and hudson.model.* or jenkins.model.* or hudson.model.Hudson. It gives an error: " unable to resolve class hudson.model.Hudson" or " Caught: groovy.lang.MissingPropertyException: No such property: Jenkins for class: hudson8158411085491171644" or " unable to resolve class jenkins.model.Jenkins"Carefree
This works, you have to install groovy separately on jenkins host. Groovy built into jenkins not used for that groovy post-build plugin.Wolter
I got something very similar to this to work but it re-enables after every restart. Any clue?Unmannerly
O
7

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.

Overdraw answered 29/12, 2011 at 2:32 Comment(0)
T
4

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.

Train answered 28/12, 2011 at 21:12 Comment(1)
This is not exactly what I was looking for, but thanks for the response. Your answer pointed me to another post which explained how to use Locks and Latches. That will prevent two jobs from running at the same time, which isn't quite what I need in this case. Updating the original question with some more detailsOverdraw
M
3

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())
}
Mclain answered 8/4, 2016 at 19:27 Comment(0)
C
2

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).
Crusade answered 6/11, 2017 at 18:42 Comment(1)
This only works with templates. If you try disabling the job that is running it, it will delete all of your job configurations.Shrievalty
G
2

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)

Goering answered 24/9, 2019 at 11:6 Comment(1)
This solves my problem in enabling a pipeline job. Thanks!Argol
W
0

You can try out Build blocker plugin https://wiki.jenkins-ci.org/display/JENKINS/Build+Blocker+Plugin

Wurster answered 25/5, 2016 at 10:0 Comment(0)
S
0

This is what worked for me on Jenkins 2.235.5. I wanted to disable the currently running job.

Running a Execute system Groovy script:

import jenkins.model.Jenkins

def build = this.getProperty('binding').getVariable('build')
def listener = this.getProperty('binding').getVariable('listener')
def env = build.getEnvironment(listener)
String jobNameEnv = env.JOB_NAME
println("jobNameEnv: ${jobNameEnv}")

Jenkins.instance.getItemByFullName(jobNameEnv).disable()

https://javadoc.jenkins-ci.org/jenkins/model/Jenkins.html#getItemByFullName(java.lang.String,java.lang.Class)

Gets the Item object by its full name. Full names are like path names, where each name of Item is combined by '/'.

Returns: null if either such Item doesn't exist under the given full name, or it exists but it's no an instance of the given type.

Throws: org.springframework.security.access.AccessDeniedException - as per ItemGroup.getItem(java.lang.String)

Shrievalty answered 26/9, 2022 at 18:29 Comment(0)
P
0

It's simplified way, no need to install anything. But need scriptApproval

stage('Disable job remotely') {
    steps {
        script {
            Jenkins.instance.getItem("jobname").setDisabled(true)
        }
    }
}
Pinochle answered 24/7, 2024 at 3:23 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.