-
Notifications
You must be signed in to change notification settings - Fork 0
/
submit_script.py
47 lines (38 loc) · 1.13 KB
/
submit_script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import requests
# Jenkins settings
jenkins_url = ''
username = ''
password = ''
groovy_script_path = ''
# Read the Groovy script from the file
with open(groovy_script_path, 'r') as file:
groovy_script = file.read()
# Jenkins script endpoint
script_endpoint = f'{jenkins_url}/manage/script'
# Creating a single session
s = requests.Session()
s.auth = (username, password)
# Getting crumb for Session from jenkins as it is needed for interaction with Jenkins
crumb_json = (s.get(jenkins_url + "/crumbIssuer/api/json")).json()
crumb_value = crumb_json.get('crumb')
# Request headers
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
"Jenkins-Crumb": crumb_value
}
# Data payload
data = {
'script': groovy_script,
'Submit': '',
'Jenkins-Crumb': crumb_value
}
# Make the POST request
response = s.post(script_endpoint, headers=headers, data=data)
# Check the response
if response.status_code == 200:
print("Script executed successfully")
print(response.status_code)
else:
print(f"Failed to execute script. Status code: {response.status_code}")
print(response.text)
print(response.status_code)