Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Python script to modify jenkins jobs using requests module
# I wrote this program after several attempts to work with jenkinsapi and python-jenkins packages.
# This script got the job done without any issues.
import requests
import sys
jenkins_host = "localhost:8080"
jenkins_user = "admin"
jenkins_api = "z01c688ffc46511f7e8dd95014ggaf05"
# Check command line args
if len(sys.argv) < 3:
print("Usage: python job_name action (enable or disable)")
jenkins_job = sys.argv[1]
action = sys.argv[2]
# First get a CRUMB from Jenkins
url = "http://{0}:{1}@{2}/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)".\
crumb = requests.get(url).content
# Now enable or disable the job
job_url = "http://{0}:{1}@{2}/job/{3}/{4}".format(jenkins_user, jenkins_api, jenkins_host, jenkins_job, action)
headers = dict()
headers[crumb.split(":")[0]] = crumb.split(":")[1]
print("{0}ing job {1} at jenkins server {2}".format(action[0:-1], jenkins_job, jenkins_host))
response =, headers=headers)
if response.status_code == 200:
print("Job {0} {1}ed successfully on the server {2}".format(jenkins_job, action[0:-1], jenkins_host))
print("Something went wrong. Check out the response while {0}ing the job {1}:\nResponse:{2}".format(action,
jenkins_job, response.content))

This comment has been minimized.

Copy link

saargrin commented Jul 12, 2018

thanks,that was very helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.