2

詹金斯:2.32.2

我正在尝试使用模板作业创建 Jenkins 作业,但收到此错误消息。

使用 python-jenkins API 使用自定义模板创建 Jenkins 作业的异常


用于创建詹金斯工作的 Python 代码

import jenkins
server = jenkins.Jenkins('http://<jenkins-ip>:8080', username='admin', password='admin',timeout=1000)
job = server.create_job(name="HelloWorld", config_xml='Template.xml')
print( job)

这显示:

--------------------------------------------
Exception
-------------------------------------------
C:\Users\SRI_14\AppData\Local\Programs\Python\Python36\python.exe "D:/MyWorkspace/Python Workspace/HelloWorld/Test.py"
Traceback (most recent call last):
File "C:\Users\SRI_14\AppData\Local\Programs\Python\Python36\lib\site-packages\jenkins_init_.py", line 431, in jenkins_open
response = urlopen(req, timeout=self.timeout).read()
File "C:\Users\SRI_14\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\SRI_14\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 532, in open
response = meth(req, response)
File "C:\Users\SRI_14\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 642, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Users\SRI_14\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 570, in error
return self._call_chain(*args)
File "C:\Users\SRI_14\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 504, in _call_chain
result = func(*args)
File "C:\Users\SRI_14\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 650, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 500: Server Error
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:/MyWorkspace/Python Workspace/HelloWorld/Test.py", line 4, in <module>
job = server.create_job(name="HelloWorld", config_xml='Template.xml')
File "C:\Users\SRI_14\AppData\Local\Programs\Python\Python36\lib\site-packages\jenkins_init_.py", line 992, in create_job
config_xml.encode('utf-8'), DEFAULT_HEADERS))
File "C:\Users\SRI_14\AppData\Local\Programs\Python\Python36\lib\site-packages\jenkins_init_.py", line 448, in jenkins_open
e.code, e.msg)
jenkins.JenkinsException: Error in request. Possibly authentication failed [500]: Server Error
Process finished with exit code 1
4

1 回答 1

1

通过将 XML 配置转换为字符串来传递它。

import jenkins

import xml.etree.ElementTree as ET

server=jenkins.Jenkins('http://10.1.1.192:8080',username='admin',password='password')

tree = ET.parse('Config.xml')

root = tree.getroot()

xml_str = ET.tostring(root, encoding='utf8', method='xml').decode()

server.create_job('test_Job',xmlstr)
于 2020-10-29T11:19:18.493 回答