Last active
October 16, 2020 04:06
-
-
Save akshayamaldhure/c32e5432f263e5e85221a2fd53cccbad to your computer and use it in GitHub Desktop.
This is a sample Jenkins client code snippet that helps authenticate Jenkins REST API calls by getting crumb from Jenkins's crumbIssuer using valid credentials.
This file contains 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
import os | |
import requests | |
class Jenkins: | |
def __init__(self): | |
self.api_server_url = "your_jenkins_url_goes_here" | |
self.username = os.environ["JENKINS_USERNAME"] | |
self.password = os.environ["JENKINS_PASSWORD"] | |
self.credentials = (self.username, self.password) | |
print("Obtaining crumb") | |
self.session = requests.Session() | |
crumb_response = self.session.get(url="{0:s}/crumbIssuer/api/json".format(self.api_server_url), | |
auth=self.credentials) | |
print("Obtained crumb response: " + crumb_response.text) # for debugging purposes only, remove this line if you do not want the crumb to be printed to console | |
cookie = "" | |
for key in self.session.cookies.get_dict().keys(): | |
cookie = key + "=" + self.session.cookies.get_dict()[key] | |
self.headers = {crumb_response.json()['crumbRequestField']: crumb_response.json()['crumb'], | |
"Content-Type": "application/json", | |
"Cookie": cookie} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment