jenkins之job创建(python-jenkins)

'''
说明:下载python-jenkins模块
1.同级目录下放好需要创建job的的config.xml文件(可以从jenkins安装目录下的jobs下的工程中获取)
2.同级目录下放好新建job名的文件jobsName.ini
3.同级目录下放好修改job名的文件changeJobsName.ini
'''
import jenkins

jobsOperation = input("请选择是新建jobs还是修改jobs:\na.创建jobs \nb.修改jobs \n")
# 连接远程Jenkins
server = jenkins.Jenkins("http://127.0.0.1:8080/jenkins/", username="admin", password="123456")
# 读取及修改的配置模板
pathConfigxml = open("config.xml", encoding='utf-8').read()
# 读取要创建的jobs名称
def readJobsName():
    jobnames = open('jobsName.ini', encoding="utf-8")
    return jobnames
    pass
# 读取要修改配置的jobs名称
def readChangeJobsName():
    changeJobsName = open('changeJobsName.ini', encoding='utf-8')
    return changeJobsName
    pass
# 创建新的jobs
def createNewJobs():
    jobsname = readJobsName()
    for jobName in jobsname:
        server.create_job(jobName.replace("\n", ""), pathConfigxml)
    pass
# 批量修改已有的jobs
def changeJobs():
    changeJobsName = readChangeJobsName()
    for changeJobName in changeJobsName:
        server.reconfig_job(changeJobName.replace("\n", ""), pathConfigxml)
    pass
if "a" == jobsOperation:
    createNewJobs()
elif "b" == jobsOperation:
    changeJobs()
else:
    print("未选择操作,退出任务!")

  

posted @ 2020-06-14 23:17  漫步sch  阅读(1673)  评论(0编辑  收藏  举报