Python Jenkins模塊操作(持續集成-運維自動化)一

一、    安裝python包
如果pip可以使用,請直接pip install python-jerkins, 因爲我這裏是rhel 6.8 手動將python 2.6.6 升級到了python 2.7.15。安裝時使用了命瞭如下
pip install --trusted-host pypi.org--trusted-host files.pythonhosted.orgpython-jenkins
============醜陋的分割線===========
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

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",
    }
]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章