python *** 作Jenkins常用方法

python *** 作Jenkins常用方法,第1张

1. 安装Jenkins包
pip install python-jenkins
2.常用方法

大家安装完python-jenkins,就可以直接copy代码,配置Jenkins信息,执行就可以了。


当然也可以自己创建job,然后通过copy方法,加上定时任务,就可以每天自动创建新的任务了。


# coding: utf-8
import jenkins

# create jenkins server,return the result
def create_jenkins_server(url, username, password):
    server = jenkins.Jenkins(url, username, password)
    return server

# create job, return job name
def create_job(server, job_name, job_config):
    server.create_job(job_name, job_config)
    return job_name

# build job, return build result
def build_job(server, job_name):
    result = server.build_job(job_name)
    return result

# copy job, return job name
def copy_job(server, job_name, new_job_name):
    server.copy_job(job_name, new_job_name)
    return new_job_name

# delete job, return result of delete
def delete_job(server, job_name):
    result = server.delete_job(job_name)
    return result

# get job config, return job config
def get_job_config(server, job_name):
    job_config = server.get_job_config(job_name)
    return job_config

# get job info, return job info
def get_job_info(server, job_name):
    job_info = server.get_job_info(job_name)
    return job_info

# active job, !!!! first step disable job,second step enable job !!!!,return result of active
def active_job(server, job_name):
    result = server.disable_job(job_name)
    result = server.enable_job(job_name)
    return result

if __name__ == '__main__':
    # create jenkins server
    url='http://xx.xx.xx.xx:8080/jenkins'
    user='xxxx'
    passwd='xxxx'
    server = create_jenkins_server(url, user, passwd)

    # create job
    job_name = 'test_job'
    job_config = '''
    
    
    false
    
    
    true
    false
    false
    false
    
    false
    
    
    
    '''
    create_job(server, job_name, job_config)
    print('create job success')

    # copy job
    new_job_name = 'test_job_copy5'
    copy_job(server, job_name, new_job_name)
    print(new_job_name)

    # active job
    active_job(server, new_job_name)
    print('active job success')

    # build job
    build_result = build_job(server, new_job_name)
    print(build_result)

    # delete job
    delete_job(server, job_name)
    print('delete job success')

    # get job config
    job_config = get_job_config(server, new_job_name)
    print(job_config)

    # get job info
    job_info = get_job_info(server, new_job_name)
    print(job_info)

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/569350.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-09
下一篇 2022-04-09

发表评论

登录后才能评论

评论列表(0条)

保存