Skip to content

Instantly share code, notes, and snippets.

Embed
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 jenkins_modify_jobs.py job_name action (enable or disable)")
exit()
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)".\
format(jenkins_user,jenkins_api,jenkins_host)
crumb = requests.get(url).content
print(crumb)
# 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 = requests.post(job_url, headers=headers)
print(response)
if response.status_code == 200:
print("Job {0} {1}ed successfully on the server {2}".format(jenkins_job, action[0:-1], jenkins_host))
else:
print("Something went wrong. Check out the response while {0}ing the job {1}:\nResponse:{2}".format(action,
jenkins_job, response.content))
@saargrin

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.