Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable a Jenkins job via curl?

Tags:

post

curl

jenkins

I want to disable a Jenkins job by sending a post curl request to Jenkins.

I've tried doing that using:

  1. curl -X POST http://<server>:8080/<jobname>/disable
  2. curl -X POST http://<server>:8080/<jobname>/disable?token=<token>
  3. curl -u <username>:<token> POST http://<server>:8080/<jobname>/disable

but failed every time. The error i am getting is:

403 no valid crumb was included in the request

Is there a good curl based solution to this problem?

like image 764
rrawat Avatar asked Feb 18 '15 06:02

rrawat


People also ask

How can we stop a Jenkins job from being executed temporarily?

How can we stop a scheduled job from being executed temporarily? Disable the job from the job details page to temporarily stop all scheduled executions & other factors/events from triggering the job and enable it back to resume the job schedules/triggers.

What does disable project do in Jenkins?

Disables the job, so that no new builds will be executed until the project is re-enabled.


2 Answers

No valid crumb means your Jenkins installation has a security option enabled which prevent requests send in a standard way to avoid one-click attacks. You can't use Jenkins CLI either, because it doesn't work yet.

Here are the steps using curl (replace localhost with your Jenkins address):

  1. Note your user API Token (from /user/USER/configure).
  2. Get your crumb:

    CRUMB=$(curl -s 'http://USER:TOKEN@localhost:8080/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)')
    
  3. Now you can disable the job by sending the crumb in the headers:

    curl -X POST -H "$CRUMB" http://USER:TOKEN@localhost:8080/<jobname>/disable
    

    If the above won't work for some reason, you may try to use -u USER:TOKEN instead.

like image 143
kenorb Avatar answered Sep 19 '22 16:09

kenorb


The crumb error indicates you are using CSRF Protection. You need to include a proper crumb header in your request. The crumb can be obtained from the Jenkins API as described on the Jenkins wiki page linked above. The answer for "Trigger parameterized build with curl and crumb" shows the syntax to adding the crumb header in the curl request.

like image 22
Dave Bacher Avatar answered Sep 21 '22 16:09

Dave Bacher