一、 安装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)
100% |████████████████████████████████| 92kB 167kB/s
Collecting multi-key-dict (from python-jenkins)
Collecting pbr>=0.8.2 (from python-jenkins)
100% |████████████████████████████████| 102kB 106kB/s
Collecting idna<2.7,>=2.5 (from requests->python-jenkins)
100% |████████████████████████████████| 61kB 134kB/s
Collecting urllib3<1.23,>=1.21.1 (from requests->python-jenkins)
100% |████████████████████████████████| 133kB 161kB/s
Collecting certifi>=2017.4.17 (from requests->python-jenkins)
100% |████████████████████████████████| 153kB 48kB/s
Collecting chardet<3.1.0,>=3.0.2 (from requests->python-jenkins)
100% |████████████████████████████████| 143kB 98kB/s
Installing collected packages: six, idna, urllib3, certifi, chardet, requests, multi-key-dict, pbr, python-jenkins
Running setup.pyinstall for multi-key-dict ... done 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
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", } ] |