<Python模块>python-jenkins

时间:Aug. 30, 2017 分类:

目录:

Python-jenkins

python的jenkins api包含了绝大多数的Jenkins操作。

使用Python-jenkins

安装

  • python版本为Python 2.7.9
  • 中国区亚马逊云环境

直接通过pip安装

[root@yunwei-002 18:34:15 ec2-user]$/usr/local/bin/pip2.7 install python-jenkins
Collecting python-jenkins
  Downloading python_jenkins-0.4.15-py2-none-any.whl
Collecting multi-key-dict (from python-jenkins)
  Downloading multi_key_dict-2.0.3.tar.gz
Requirement already satisfied: six>=1.3.0 in /usr/lib/python2.7/dist-packages (from python-jenkins)
Collecting pbr>=0.8.2 (from python-jenkins)
  Downloading pbr-3.1.1-py2.py3-none-any.whl (99kB)
    100% |████████████████████████████████| 102kB 14kB/s 
Installing collected packages: multi-key-dict, pbr, python-jenkins
  Running setup.py install for multi-key-dict ... done
Successfully installed multi-key-dict-2.0.3 pbr-3.1.1 python-jenkins-0.4.15

创建对象

导入jenkins模块

import jenkins

定义远程连接jenkins master server的url:port,不写端口默认为80端口

jenkins_server_url = 'http://ci.culiu.org:8088'

用户id

user_id = 'wanghongyu'

API_TOKEN

api_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

API_TOKEN获取办法

实例化jenkins对象,远程连接Jenkins

server = jenkins.Jenkins(jenkins_server_url, username=user_id, password=api_token)

源码中定义

class Jenkins(object):
    _timeout_warning_issued = False

    def __init__(self, url, username=None, password=None,
                 timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
        '''Create handle to Jenkins instance.

        All methods will raise :class:`JenkinsException` on failure.

        :param username: Server username, ``str``
        :param password: Server password, ``str``
        :param url: URL of Jenkins server, ``str``
        :param timeout: Server connection timeout in secs (default: not set), ``int``
        '''

实例化完成就能执行对应的方法了。

Python-jenkins方法

jobs相关信息

方法详细介绍

def get_jobs(self, folder_depth=0, view_name=None):
        """Get list of jobs.

        Each job is a dictionary with 'name', 'url', 'color' and 'fullname'
        keys.

        If the ``view_name`` parameter is present, the list of
        jobs will be limited to only those configured in the
        specified view. In this case, the job dictionary 'fullname' key
        would be equal to the job name.

        :param folder_depth: Number of levels to search, ``int``. By default
            0, which will limit search to toplevel. None disables the limit.
        :param view_name: Name of a Jenkins view for which to
            retrieve jobs, ``str``. By default, the job list is
            not limited to a specific view.
        :returns: list of jobs, ``[{str: str, str: str, str: str, str: str}]``

        Example::

            >>> jobs = server.get_jobs()
            >>> print(jobs)
            [{
                u'name': u'all_tests',
                u'url': u'http://your_url.here/job/all_tests/',
                u'color': u'blue',
                u'fullname': u'all_tests'
            }]

        """

获取所有jobs信息

方法

server.get_jobs()

执行结果

[
    {
        u'url': u'http: //ci.culiu.org: 8088/job/99baoyou-android-rawlog-click-process/',
        u'color': u'disabled',
        u'fullname': u'99baoyou-android-rawlog-click-process',
        u'_class': u'hudson.model.FreeStyleProject',
        u'name': u'99baoyou-android-rawlog-click-process'
    },
    {
        u'url': u'http: //ci.culiu.org: 8088/job/99baoyou-android-rawlog-order_related-process/',
        u'color': u'disabled',
        u'fullname': u'99baoyou-android-rawlog-order_related-process',
        u'_class': u'hudson.model.FreeStyleProject',
        u'name': u'99baoyou-android-rawlog-order_related-process'
    },
    {
        u'url': u'http: //ci.culiu.org: 8088/job/ad-system-actionlog-dev-deploy/',
        u'color': u'red',
        u'fullname': u'ad-system-actionlog-dev-deploy',
        u'_class': u'hudson.model.FreeStyleProject',
        u'name': u'ad-system-actionlog-dev-deploy'
    },
    省略部分...
]

获取某个视图的jobs信息

方法

server.get_jobs(view_name='op')

获取结果

[
    {
        u'url': u'http: //ci.culiu.org: 8088/job/op-ccapi-dev_deploy/',
        u'color': u'blue',
        u'fullname': u'op-ccapi-dev_deploy',
        u'_class': u'hudson.model.FreeStyleProject',
        u'name': u'op-ccapi-dev_deploy'
    },
    {
        u'url': u'http: //ci.culiu.org: 8088/job/op-ccapi_deploy/',
        u'color': u'blue',
        u'fullname': u'op-ccapi_deploy',
        u'_class': u'hudson.model.FreeStyleProject',
        u'name': u'op-ccapi_deploy'
    },
    省略部分...
]

job详细信息

源码方法详细介绍

def get_job_info(self, name, depth=0, fetch_all_builds=False):
        '''Get job information dictionary.

        :param name: Job name, ``str``
        :param depth: JSON depth, ``int``
        :param fetch_all_builds: If true, all builds will be retrieved
                                 from Jenkins. Otherwise, Jenkins will
                                 only return the most recent 100
                                 builds. This comes at the expense of
                                 an additional API call which may
                                 return significant amounts of
                                 data. ``bool``
        :returns: dictionary of job information
        '''

获取指定job信息

server.get_job_info('ads-lua-backstatic-deploy')

获取到的结果就是这个样子

{
    u'scm': {
        u'_class': u'hudson.scm.NullSCM'
    },
    u'color': u'blue',
    u'lastSuccessfulBuild': {
        u'url': u'http: //ci.culiu.org: 8088/job/ads-lua-backstatic-deploy/59/',
        u'_class': u'hudson.model.FreeStyleBuild',
        u'number': 59
    },
    u'actions': [
        {
            u'_class': u'hudson.model.ParametersDefinitionProperty',
            u'parameterDefinitions': [
                {
                    u'_class': u'hudson.model.StringParameterDefinition',
                    u'type': u'StringParameterDefinition',
                    u'description': u'example: 1300\n\u5207\u9759\u6001\uff0c\u6574\u70b9\u5207\u9759\u6001\u4f8b\u5982\uff1a1300\u5c31\u662f13\u70b9\uff0c\u53d6\u6d88\u9759\u6001\u8bf7\u8bbe\u7f6e\uff1aNormal',
                    u'defaultParameterValue': {
                        u'_class': u'hudson.model.StringParameterValue',
                        u'value': u'Normal'
                    },
                    u'name': u'Backed_date_time'
                },
                {
                    u'description': u'\u9009\u5b9a\u4e3b\u673a\u7ec4',
                    u'defaultParameterValue': {
                        u'_class': u'hudson.model.StringParameterValue',
                        u'value': u'ads-online-A'
                    },
                    u'choices': [
                        u'ads-online-A',
                        u'tengine-ads-A-000',
                        u'tengine-ads-A-001',
                        u'tengine-ads-A-002',
                        u'tengine-ads-A-003',
                        u'tengine-ads-A-004',
                        u'tengine-ads-A-005',
                        u'tengine-ads-A-006',
                        u'ads-ads-api-00',
                        u'ads-ads-api-01',
                        u'ads-ads-api-02',
                        u'ads-ads-api-03',
                        u'ads-ads-api-04',
                        u'ads-ads-api-05',
                        u'ads-ads-api-06',
                        u'ads-ads-api-07',
                        u'ads-ads-api-08',
                        u'ads-ads-api-09'
                    ],
                    u'_class': u'hudson.model.ChoiceParameterDefinition',
                    u'type': u'ChoiceParameterDefinition',
                    u'name': u'HostOrGroup'
                }
            ]
        },
        {

        },
        {

        },
        {

        },
        {
            u'_class': u'com.cloudbees.plugins.credentials.ViewCredentialsAction'
        }
    ],
    u'lastCompletedBuild': {
        u'url': u'http: //ci.culiu.org: 8088/job/ads-lua-backstatic-deploy/59/',
        u'_class': u'hudson.model.FreeStyleBuild',
        u'number': 59
    },
    u'lastUnsuccessfulBuild': {
        u'url': u'http: //ci.culiu.org: 8088/job/ads-lua-backstatic-deploy/5/',
        u'_class': u'hudson.model.FreeStyleBuild',
        u'number': 5
    },
    u'upstreamProjects': [

    ],
    u'lastFailedBuild': {
        u'url': u'http: //ci.culiu.org: 8088/job/ads-lua-backstatic-deploy/5/',
        u'_class': u'hudson.model.FreeStyleBuild',
        u'number': 5
    },
    u'healthReport': [
        {
            u'iconUrl': u'health-80plus.png',
            u'score': 100,
            u'description': u'Buildstability: Norecentbuildsfailed.',
            u'iconClassName': u'icon-health-80plus'
        }
    ],
    u'queueItem': None,
    u'lastBuild': {
        u'url': u'http: //ci.culiu.org: 8088/job/ads-lua-backstatic-deploy/59/',
        u'_class': u'hudson.model.FreeStyleBuild',
        u'number': 59
    },
    u'_class': u'hudson.model.FreeStyleProject',
    u'lastStableBuild': {
        u'url': u'http: //ci.culiu.org: 8088/job/ads-lua-backstatic-deploy/59/',
        u'_class': u'hudson.model.FreeStyleBuild',
        u'number': 59
    },
    u'description': u'',
    u'downstreamProjects': [

    ],
    u'concurrentBuild': False,
    u'lastUnstableBuild': None,
    u'buildable': True,
    u'displayNameOrNull': None,
    u'inQueue': False,
    u'keepDependencies': False,
    u'fullName': u'ads-lua-backstatic-deploy',
    u'name': u'ads-lua-backstatic-deploy',
    u'displayName': u'ads-lua-backstatic-deploy',
    u'builds': [
        {
            u'url': u'http: //ci.culiu.org: 8088/job/ads-lua-backstatic-deploy/59/',
            u'_class': u'hudson.model.FreeStyleBuild',
            u'number': 59
        },
        {
            u'url': u'http: //ci.culiu.org: 8088/job/ads-lua-backstatic-deploy/58/',
            u'_class': u'hudson.model.FreeStyleBuild',
            u'number': 58
        },
        {
            u'url': u'http: //ci.culiu.org: 8088/job/ads-lua-backstatic-deploy/57/',
            u'_class': u'hudson.model.FreeStyleBuild',
            u'number': 57
        },
        ...省略部分build结果
    ],
    u'url': u'http: //ci.culiu.org: 8088/job/ads-lua-backstatic-deploy/',
    u'firstBuild': {
        u'url': u'http: //ci.culiu.org: 8088/job/ads-lua-backstatic-deploy/1/',
        u'_class': u'hudson.model.FreeStyleBuild',
        u'number': 1
    },
    u'nextBuildNumber': 60,
    u'property': [
        {
            u'_class': u'hudson.plugins.buildblocker.BuildBlockerProperty'
        },
        {
            u'_class': u'com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty'
        },
        {
            u'_class': u'hudson.model.ParametersDefinitionProperty',
            u'parameterDefinitions': [
                {
                    u'_class': u'hudson.model.StringParameterDefinition',
                    u'type': u'StringParameterDefinition',
                    u'description': u'example: 1300\n\u5207\u9759\u6001\uff0c\u6574\u70b9\u5207\u9759\u6001\u4f8b\u5982\uff1a1300\u5c31\u662f13\u70b9\uff0c\u53d6\u6d88\u9759\u6001\u8bf7\u8bbe\u7f6e\uff1aNormal',
                    u'defaultParameterValue': {
                        u'_class': u'hudson.model.StringParameterValue',
                        u'name': u'Backed_date_time',
                        u'value': u'Normal'
                    },
                    u'name': u'Backed_date_time'
                },
                {
                    u'description': u'\u9009\u5b9a\u4e3b\u673a\u7ec4',
                    u'defaultParameterValue': {
                        u'_class': u'hudson.model.StringParameterValue',
                        u'name': u'HostOrGroup',
                        u'value': u'ads-online-A'
                    },
                    u'choices': [
                        u'ads-online-A',
                        u'tengine-ads-A-000',
                        u'tengine-ads-A-001',
                        u'tengine-ads-A-002',
                        u'tengine-ads-A-003',
                        u'tengine-ads-A-004',
                        u'tengine-ads-A-005',
                        u'tengine-ads-A-006',
                        u'ads-ads-api-00',
                        u'ads-ads-api-01',
                        u'ads-ads-api-02',
                        u'ads-ads-api-03',
                        u'ads-ads-api-04',
                        u'ads-ads-api-05',
                        u'ads-ads-api-06',
                        u'ads-ads-api-07',
                        u'ads-ads-api-08',
                        u'ads-ads-api-09'
                    ],
                    u'_class': u'hudson.model.ChoiceParameterDefinition',
                    u'type': u'ChoiceParameterDefinition',
                    u'name': u'HostOrGroup'
                }
            ]
        }
    ],
    u'fullDisplayName': u'ads-lua-backstatic-deploy'
}

示例脚本——获取job的传递参数信息

# -*- coding: utf-8 -*-
import jenkins
jenkins_server_url = 'http://ci.culiu.org:8088'
user_id = 'wanghongyu'
api_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
server = jenkins.Jenkins(jenkins_server_url, username=user_id, password=api_token)
build_arg = server.get_job_info('ads-lua-backstatic-deploy')["property"]

# [
#         {u'_class': u'hudson.plugins.buildblocker.BuildBlockerProperty'},
#         {u'_class': u'com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty'},
#         {u'_class': u'hudson.model.ParametersDefinitionProperty', u'parameterDefinitions': [{u'_class': u'hudson.model.StringParameterDefinition', u'type': u'StringParameterDefinition', u'description': u'example:1300\n\u5207\u9759\u6001\uff0c\u6574\u70b9\u5207\u9759\u6001\u4f8b\u5982\uff1a1300\u5c31\u662f13\u70b9\uff0c\u53d6\u6d88\u9759\u6001\u8bf7\u8bbe\u7f6e\uff1aNormal', u'defaultParameterValue': {u'_class': u'hudson.model.StringParameterValue', u'name': u'Backed_date_time', u'value': u'Normal'}, u'name': u'Backed_date_time'}, {u'description': u'\u9009\u5b9a\u4e3b\u673a\u7ec4', u'defaultParameterValue': {u'_class': u'hudson.model.StringParameterValue', u'name': u'HostOrGroup', u'value': u'ads-online-A'}, u'choices': [u'ads-online-A', u'tengine-ads-A-000', u'tengine-ads-A-001', u'tengine-ads-A-002', u'tengine-ads-A-003', u'tengine-ads-A-004', u'tengine-ads-A-005', u'tengine-ads-A-006', u'ads-ads-api-00', u'ads-ads-api-01', u'ads-ads-api-02', u'ads-ads-api-03', u'ads-ads-api-04', u'ads-ads-api-05', u'ads-ads-api-06', u'ads-ads-api-07', u'ads-ads-api-08', u'ads-ads-api-09'], u'_class': u'hudson.model.ChoiceParameterDefinition', u'type': u'ChoiceParameterDefinition', u'name': u'HostOrGroup'}]}
#         ]

for parameter_definitions in build_arg:
    if len(parameter_definitions) > 1:


# {
#     u'_class': u'hudson.model.ParametersDefinitionProperty',
#     u'parameterDefinitions': [{u'_class': u'hudson.model.StringParameterDefinition', u'type': u'StringParameterDefinition', u'description': u'example:1300\n\u5207\u9759\u6001\uff0c\u6574\u70b9\u5207\u9759\u6001\u4f8b\u5982\uff1a1300\u5c31\u662f13\u70b9\uff0c\u53d6\u6d88\u9759\u6001\u8bf7\u8bbe\u7f6e\uff1aNormal', u'defaultParameterValue': {u'_class': u'hudson.model.StringParameterValue', u'name': u'Backed_date_time', u'value': u'Normal'}, u'name': u'Backed_date_time'}, {u'description': u'\u9009\u5b9a\u4e3b\u673a\u7ec4', u'defaultParameterValue': {u'_class': u'hudson.model.StringParameterValue', u'name': u'HostOrGroup', u'value': u'ads-online-A'}, u'choices': [u'ads-online-A', u'tengine-ads-A-000', u'tengine-ads-A-001', u'tengine-ads-A-002', u'tengine-ads-A-003', u'tengine-ads-A-004', u'tengine-ads-A-005', u'tengine-ads-A-006', u'ads-ads-api-00', u'ads-ads-api-01', u'ads-ads-api-02', u'ads-ads-api-03', u'ads-ads-api-04', u'ads-ads-api-05', u'ads-ads-api-06', u'ads-ads-api-07', u'ads-ads-api-08', u'ads-ads-api-09'], u'_class': u'hudson.model.ChoiceParameterDefinition', u'type': u'ChoiceParameterDefinition', u'name': u'HostOrGroup'}]
# }


        for parameter in parameter_definitions["parameterDefinitions"]:
            print '----------------------------------'
            print 'name: %s' % parameter["name"]
            print 'class: %s' % parameter["_class"]
            print 'type: %s' % parameter["type"]
            print 'description:\n%s' %parameter["description"]
            print 'jenkins_arg: %s' % parameter["defaultParameterValue"]["name"]
            print 'default_value: %s' % parameter["defaultParameterValue"]["value"]
            if parameter["_class"] == 'hudson.model.ChoiceParameterDefinition':
                print 'can_choices: %s' % parameter["choices"]
            print '----------------------------------'

经过我测试,这边property和action中的内容中展现的均为实时的,因为property稍微详细一点,所以这边我选择了property。

执行结果

----------------------------------
name: Backed_date_time
class: hudson.model.StringParameterDefinition
type: StringParameterDefinition
description:
example:1300
切静态,整点切静态例如:1300就是13点,取消静态请设置:Normal
jenkins_arg: Backed_date_time
default_value: Normal
----------------------------------
----------------------------------
name: HostOrGroup
class: hudson.model.ChoiceParameterDefinition
type: ChoiceParameterDefinition
description: 选定主机组
jenkins_arg: HostOrGroup
default_value: ads-online-A
can_choices: [u'ads-online-A', u'tengine-ads-A-000', u'tengine-ads-A-001', u'tengine-ads-A-002', u'tengine-ads-A-003', u'tengine-ads-A-004', u'tengine-ads-A-005', u'tengine-ads-A-006', u'ads-ads-api-00', u'ads-ads-api-01', u'ads-ads-api-02', u'ads-ads-api-03', u'ads-ads-api-04', u'ads-ads-api-05', u'ads-ads-api-06', u'ads-ads-api-07', u'ads-ads-api-08', u'ads-ads-api-09']
----------------------------------

可以看一下构建的页面

构建job

源码方法详细介绍

def build_job(self, name, parameters=None, token=None):
        '''Trigger build job.

        :param name: name of job
        :param parameters: parameters for job, or ``None``, ``dict``
        :param token: Jenkins API token
        '''

执行job

import jenkins
jenkins_server_url = 'http://ci.culiu.org:8088'
user_id = 'wanghongyu'
api_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
server = jenkins.Jenkins(jenkins_server_url, username=user_id, password=api_token)
param_dict = {'HostOrGroup':'ads-online-A' ,'Backed_date_time', 'Normal'}
server.build_job('op-tag-dev-deploy', parameters=param_dict)

对于有参数的build任务必须要传递一个字典,这个字典不能为空,哪怕字典中的key并不存在在编译参数中也可以进行执行,没有传递的值就使用默认值。

还有不论使用任何的jenkins插件进行参数配置等,最后转化到Jenkins都是统一的使用jenkins的变量,只需要传入jenkins的变量和变量值即可。

不传递字典的返回结果为

[ec2-user@yunwei-002 11:36:42 ~]$python jebuild.py
Traceback (most recent call last):
  File "jebuild.py", line 10, in <module>
    server.build_job('op-tag-dev-deploy')
  File "/usr/local/lib/python2.7/site-packages/jenkins/__init__.py", line 1051, in build_job
    self.build_job_url(name, parameters, token), b''))
  File "/usr/local/lib/python2.7/site-packages/jenkins/__init__.py", line 431, in jenkins_open
    response = urlopen(req, timeout=self.timeout).read()
  File "/usr/lib64/python2.7/urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib64/python2.7/urllib2.py", line 437, in open
    response = meth(req, response)
  File "/usr/lib64/python2.7/urllib2.py", line 550, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib64/python2.7/urllib2.py", line 475, in error
    return self._call_chain(*args)
  File "/usr/lib64/python2.7/urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "/usr/lib64/python2.7/urllib2.py", line 558, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 400: Nothing is submitted

复制项目

server.copy_job('op-tag-dev-deploy', 'op-tag-dev-deploy-why')

禁用项目

server.disable_job('op-tag-dev-deploy-why')

启用项目

server.enable_job('op-tag-dev-deploy-why')

删除项目

server.delete_job('op-tag-dev-deploy-why')

重新配置项目

该功能暂时没有理解,没有进行测试

server.reconfig_job('empty_copy', jenkins.RECONFIG_XML)

获取构建情况

last_build_number = server.get_job_info('ads-lua-backstatic-deploy')['lastCompletedBuild']['number']
build_info = server.get_build_info('ads-lua-backstatic-deploy', last_build_number)
print build_info

获取结果

{
    u'building': False,
    u'queueId': 923252,
    u'displayName': u'#59',
    u'description': None,
    u'changeSet': {
        u'items': [

        ],
        u'kind': None,
        u'_class': u'hudson.scm.EmptyChangeLogSet'
    },
    u'artifacts': [

    ],
    u'timestamp': 1503906513119L,
    u'number': 59,
    u'actions': [
        {
            u'_class': u'hudson.model.ParametersAction',
            u'parameters': [
                {
                    u'_class': u'hudson.model.StringParameterValue',
                    u'name': u'Backed_date_time',
                    u'value': u'Normal'
                },
                {
                    u'_class': u'hudson.model.StringParameterValue',
                    u'name': u'HostOrGroup',
                    u'value': u'ads-online-A'
                }
            ]
        },
        {
            u'_class': u'hudson.model.CauseAction',
            u'causes': [
                {
                    u'note': None,
                    u'_class': u'hudson.model.Cause$RemoteCause',
                    u'shortDescription': u'Startedbyremotehost10.30.28.201',
                    u'addr': u'10.30.28.201'
                }
            ]
        },
        {

        }
    ],
    u'id': u'59',
    u'keepLog': False,
    u'url': u'http: //ci.culiu.org: 8088/job/ads-lua-backstatic-deploy/59/',
    u'culprits': [

    ],
    u'result': u'SUCCESS',
    u'executor': None,
    u'duration': 701,
    u'builtOn': u'deploy-001',
    u'_class': u'hudson.model.FreeStyleBuild',
    u'fullDisplayName': u'ads-lua-backstatic-deploy#59',
    u'estimatedDuration': 716
}

其他功能可以参考官方文档,例如使用Kerberos登录认证,方法例如:获取队列get_queue_info(),获取当前用户信息get_whoami(),获取当前版本get_version(),获取插件信息get_plugins_info(),获取node信息get_node_info(),获取视图views信息get_views(),获取promotion信息,重命名jobrename_job()`等等,甚至可以通过xml来创建job等更复杂的需要结合xml进行操作。

参考了一下此博客, http://www.cnblogs.com/znicy/p/5498609.html