JenkinsAPI Python - 尝试获取构建信息

时间:2014-02-12 00:03:40

标签: python python-2.7 jenkins jenkins-plugins

以下是我使用JenkinsAPI for Python进行的操作

# Python scipt to get build information

# Import jenkins API and check Jenkins Version

import jenkinsapi
from jenkinsapi.jenkins import Jenkins
from jenkinsapi.build import Build

J = Jenkins('http://test.com')
job = J['MY_JOB_NAME']

print "Jenkins version:", J.version, "\n"

print jenkinsapi.api.get_latest_test_results('http://test.com','MY_JOB_NAME'), "\n"


B= Build('http://test.com',22,job)
print B.get_resultset()

我一直收到这个我无法理解的错误:

Traceback (most recent call last):
  File "myscript.py", line 19, in <module>
    print B
  File "/usr/local/lib/python2.7/dist-packages/jenkinsapi-0.2.18-py2.7.egg/jenkinsapi/build.py", line 49, in __str__
    return self._data['fullDisplayName']
KeyError: 'fullDisplayName'
svikani@myunix:~$ python myscript.py
Traceback (most recent call last):
  File "myscript.py", line 19, in <module>
    print B.get_resultset()
  File "/usr/local/lib/python2.7/dist-packages/jenkinsapi-0.2.18-py2.7.egg/jenkinsapi/build.py", line 331, in get_resultset
    result_url = self.get_result_url()
  File "/usr/local/lib/python2.7/dist-packages/jenkinsapi-0.2.18-py2.7.egg/jenkinsapi/build.py", line 325, in get_result_url
    return url_tpl % (self._data["url"], config.JENKINS_API)
KeyError: 'url'

1 个答案:

答案 0 :(得分:5)

看起来 Build 实例中有未初始化的变量。

您自己正在构建 Build 实例,但是您应该通过 Jenkins 实例的API来获取您感兴趣的作业和构建。

import jenkinsapi
j = jenkinsapi.jenkins.Jenkins('http://jenkins:8080/')
job = j.get_job(JOB_NAME)  # or j[JOB_NAME]
build = job.get_build(BUILD_ID)
print build.get_resultset()