• 【转】通过python调用jenkins 常用api操作


    原:https://www.cnblogs.com/L-O-N/p/11608220.html

      1 # -*- coding: utf-8 -*-
      2 
      3 import jenkins
      4 
      5 
      6 class TestJenkins(object):
      7     def __new__(cls, *args, **kwargs):
      8         server = 'http://1.1.1.1:8080/jenkins'
      9         username = 'admin'
     10      # 对应用户的token信息,不是明文的密码信息
     11         password = 'fljljdfladoweurojlsjdfasd123'
     12         server = jenkins.Jenkins(url=server, username=username, password=password)
     13         instance = super(TestJenkins, cls).__new__(cls, *args, **kwargs)
     14         instance.server = server
     15         return instance
     16 
     17     def __init__(self):
     18         #这里的api_token是针对远程执行任务时,jenkins要验证的token的信息
     19         self.api_token = 'okfine'
     20 
     21     def jobs_count(self):
     22         print(self.server.jobs_count())
     23 
     24     def server_info(self):
     25         print(self.server.server)
     26 
     27     def get_jobs_info(self):
     28         for item in self.server.get_all_jobs():
     29             print('name: %s' % item['name'], 'URL: ', item['url'])
     30 
     31     def get_whoami(self):
     32         print(self.server.get_whoami(depth=10))
     33 
     34     def get_auth(self):
     35         print(self.server.auth)
     36 
     37     def get_debug_job_info(self, name):
     38         print(self.server.debug_job_info(name))
     39 
     40     def check_job_exists(self, name):
     41         print(self.server.job_exists(name))
     42 
     43     def get_all_jobs(self):
     44         print(self.server.get_all_jobs())
     45 
     46     def create_job(self):
     47         print(self.server.create_job('API-1', jenkins.RECONFIG_XML))
     48         print(self.server.create_job('API-2', jenkins.RECONFIG_XML))
     49 
     50     def delete_job(self, name):
     51         print(self.server.delete_job(name))
     52 
     53     def copy_job(self, name, new_name):
     54         print(self.server.copy_job(name, new_name))
     55 
     56     def enable_job(self, name):
     57         print(self.server.enable_job(name))
     58 
     59     def disable_job(self, name):
     60         print(self.server.disable_job(name))
     61 
     62     def reconfig_job(self, name):
     63         print(self.server.reconfig_job(name, jenkins.RECONFIG_XML))
     64 
     65     def rename_job(self, name, new_name):
     66         print(self.server.rename_job(name, new_name))
     67 
     68     def build_job(self, name, parameters):
     69         print(self.server.build_job(name, parameters, token=self.api_token))
     70 
     71     def get_job_info(self, name):
     72         # 主要是通过任务的信息,来得到需要传入的参数信息,jenkins 各种自定义选项信息,单凭肉眼是很难辨别出参数信息的,
     73         # 任务相关参数信息都是在property这个参数里面
     74         build_arg = self.server.get_job_info(name)["property"]
     75         for parameter_definitions in build_arg:
     76             if len(parameter_definitions) > 1:
     77                 for parameter in parameter_definitions["parameterDefinitions"]:
     78                     print('----------------------------------')
     79                     print('name: %s' % parameter["name"])
     80                     print('class: %s' % parameter["_class"])
     81                     print('type: %s' % parameter["type"])
     82                     print('description: %s' % parameter["description"])
     83                     print('jenkins_arg: %s' % parameter["defaultParameterValue"]["name"])
     84                     print('default_value: %s' % parameter["defaultParameterValue"]["value"])
     85                     if parameter["_class"] == 'hudson.model.ChoiceParameterDefinition':
     86                         print('can_choices: %s' % parameter["choices"])
     87                     print('----------------------------------')
     88     
     89     def get_build_log(self, name):
     90         last_build_number = self.server.get_job_info(name)['lastCompletedBuild']['number']
     91         print('last_build_number', last_build_number)
     92 
     93         # 查看指定构建编号的输出
     94         print(self.server.get_build_console_output(name, last_build_number))
     95 
     96     def __call__(self, *args, **kwargs):
     97         self.get_job_info('shop')
     98      
     99         # 通过特定任务的信息来得到要传入的参数信息,然后再来执行构建任务
    100         # build new job, nice  operation
    101         # 如下列所示的5个参数,就是此次构建必须提供的参数
    102         param_dict = {'repository': 'ssh://git@xxxxx.com/xx/xxxx.git',
    103                        'profile': 'xxxx-xxx',
    104                        'branch': 'master',
    105                        'upload_nexus': False,
    106                        'deploy_app': False}
    107 
    108         self.build_job('xxxxx', parameters=param_dict)
    109 
    110 
    111 TestJenkins()()
  • 相关阅读:
    hdu4675 GCD of Sequence 莫比乌斯+组合数学
    hdu4746 Mophues 莫比乌斯
    BZOJ2820 YY的GCD 莫比乌斯+系数前缀和
    bzoj2005 能量采集 莫比乌斯或者普通容斥
    GCD 莫比乌斯反演 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的 数对(x,y)有多少对.
    spoj7001 Visible Lattice Points 莫比乌斯反演+三维空间互质对数
    hdu1695 GCD 莫比乌斯反演做法+枚举除法的取值 (5,7),(7,5)看做同一对
    bzoj2440 完全平方数 莫比乌斯值+容斥+二分
    Problem b 莫比乌斯反演+枚举除法的取值
    Codeforces839D Winter is here 容斥
  • 原文地址:https://www.cnblogs.com/HYanqing/p/12023908.html
Copyright © 2020-2023  润新知