简体   繁体   中英

I am not able to use the server.delete_build() command in python for Jenkins. I am using python-jenkins wrapper for this

I was using python-jenkins wrapper to operate on jenkins from Python. I want to delete a build using server.delete_build command, but whenever I try to use this command:

import jenkins

def build_job(para_value):
    server = jenkins.Jenkins('http://localhost:8080', username='username', password='password')
    if not server.job_exists('job_name'):
        server.create_job('job_name', jenkins.EMPTY_CONFIG_XML)
    server.build_job('job_name', parameters={'para1': para_value})
def delete_build(jobID):
    print(jobID)
    server = jenkins.Jenkins('http://localhost:8080', username='username', password='password')
    server.delete_build('job_name', jobID)

This is the error that I get:

File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/jenkins/__init__.py", line 1415, in delete_build
self.jenkins_open(requests.Request('POST',
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/jenkins/__init__.py", line 557, in jenkins_open
return self.jenkins_request(req, add_crumb, resolve_auth).text
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/jenkins/__init__.py", line 573, in jenkins_request
self.maybe_add_crumb(req)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/jenkins/__init__.py", line 379, in maybe_add_crumb
req.headers[self.crumb['crumbRequestField']] = self.crumb['crumb']

Can someone suggest any solution for this?

There is not much info or solutions regarding this error, so I changed the if condition in the __init__.py of Jenkins module. Go to /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/jenkins/__init__.py (this address is for Mac users):

Do this:

Remove: if self.crumb:

Add: if self.crumb and isinstance(req.headers, dict):

This will make your code work.

This answer is inspired from: https://www.mail-archive.com/python-jenkins-developers@lists.launchpad.net/msg00597.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM