-
-
Save eugenestarchenko/82b6bc7054d83351e20476a4f2858d2f to your computer and use it in GitHub Desktop.
Python script to modify jenkins jobs using requests module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment