一、 安装python包
如果pip可以使用,请直接pip install python-jerkins, 因为我这里是rhel 6.8 手动将python 2.6.6 升级到了python 2.7.15。安装时使用了命了如下
============丑陋的分割线===========
Collecting python-jenkins
Collecting six>=1.3.0 (from python-jenkins)
Collecting requests (from python-jenkins)
Downloading
https://files.pythonhosted.org/packages/49/df/50aa1999ab9bde74656c2919d9c0c085fd2b3775fd3eca826012bef76d8c/requests-2.18.4-py2.py3-none-any.whl
(88kB)
100% |████████████████████████████████| 92kB 167kB/s
Collecting multi-key-dict (from python-jenkins)
Collecting pbr>=0.8.2 (from python-jenkins)
Downloading
https://files.pythonhosted.org/packages/2d/9d/7bfab757977067556c7ca5fe437f28e8b8843c95564fca504de79df63b25/pbr-4.0.3-py2.py3-none-any.whl
(98kB)
100% |████████████████████████████████| 102kB 106kB/s
Collecting idna<2.7,>=2.5 (from requests->python-jenkins)
Downloading
https://files.pythonhosted.org/packages/27/cc/6dd9a3869f15c2edfab863b992838277279ce92663d334df9ecf5106f5c6/idna-2.6-py2.py3-none-any.whl
(56kB)
100% |████████████████████████████████| 61kB 134kB/s
Collecting urllib3<1.23,>=1.21.1 (from requests->python-jenkins)
Downloading
https://files.pythonhosted.org/packages/63/cb/6965947c13a94236f6d4b8223e21beb4d576dc72e8130bd7880f600839b8/urllib3-1.22-py2.py3-none-any.whl
(132kB)
100% |████████████████████████████████| 133kB 161kB/s
Collecting certifi>=2017.4.17 (from requests->python-jenkins)
Downloading
https://files.pythonhosted.org/packages/7c/e6/92ad559b7192d846975fc916b65f667c7b8c3a32bea7372340bfe9a15fa5/certifi-2018.4.16-py2.py3-none-any.whl
(150kB)
100% |████████████████████████████████| 153kB 48kB/s
Collecting chardet<3.1.0,>=3.0.2 (from requests->python-jenkins)
Downloading
https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl
(133kB)
100% |████████████████████████████████| 143kB 98kB/s
Installing collected packages: six, idna, urllib3, certifi, chardet, requests, multi-key-dict, pbr, python-jenkins
Successfully installed certifi-2018.4.16 chardet-3.0.4 idna-2.6 multi-key-dict-2.0.3 pbr-4.0.3 python-jenkins-1.0.1 requests-2.18.4 six-1.11.0 urllib3-1.22
============丑陋的分割线===========
二、 测试
2.1 通过Jenkins API获取jekins的版本信息
import
jenkins
server
=
jenkins
.
Jenkins
(
'
http://localhost:8080/jenkins
'
,
username
=
'myuser'
,
password
=
'mypassword'
)
user
=
server
.
get_whoami
()
version
=
server
.
get_version
()
print
(
'Hello
%s
from Jenkins
%s
'
%
(
user
[
'fullName'
],
version
))
|
2.2 获取jenkins当前所有job的信息
jobs
=
server
.
get_jobs
(
view_name
=
'View Name’
)
node_dict = json.dumps(jobs, sort_keys=True, indent=4, separators=(',', ': '), encoding='utf8',ensure_ascii=True)
print node_dict
[
{
"_class": "hudson.model.FreeStyleProject",
"color": "blue",
"fullname": "AllUnit-MonitorTest",
"name": "AllUnit-MonitorTest",
},
{
"_class": "org.jenkinsci.plugins.workflow.job.WorkflowJob",
"color": "notbuilt",
"fullname": "AllUnit-Pipline",
"name": "AllUnit-Pipline",
},
{
"_class": "hudson.model.FreeStyleProject",
"color": "blue",
"fullname": "AllUnit-PTest",
"name": "AllUnit-PTest",
},
{
"_class": "hudson.model.FreeStyleProject",
"color": "blue",
"fullname": "empty",
"name": "empty",
}
]
|