python-jenkins 自动化实践(UI 和 API 自动化 混合应用)

2021-03-27 03:26

阅读:507

标签:mit   名称   odi   yaml   range   drive   obj   sub   turn   

jenkins 是一款很好的CI(持续集成)工具,但是在常规化部署时如果碰到很多job需要构建和改变代码分支进行部署时还是挺麻烦的,所以考虑实现自动化。

*# -- coding: utf-8 -*-

import jenkins
import re

def GetYaml():
yamlPath = os.path.join(os.getcwd(),"JenkinsBranch.yaml")
f = open(yamlPath,‘r‘,encoding=‘utf-8‘)
yamldata = yaml.load(f.read())
return yamldata

def linkJenkins():
jenkins_server_url = ‘http://jenkins.test***.cn/jenkins/‘
server = jenkins.Jenkins(jenkins_server_url,username=userName,password=password)
return server

def CheckBranch(server):
jobsLen = len(server.get_jobs())
list1 = []
list2 = []

for i in range(jobsLen):
    # print(server.get_jobs()[i][‘name‘])
    matchObj = re.findall(re.compile(r‘\*/(.*?)‘),server.get_job_config(server.get_jobs()[i][‘name‘]))
    if matchObj is None:
        print("ServerName:" + server.get_jobs()[i][‘name‘] + "  分支名称:" + "None")
        continue
    elif matchObj[0] != ‘master‘:

        list1.append(server.get_jobs()[i][‘name‘]+‘ ‘+matchObj[0])
    else:
        list2.append(server.get_jobs()[i][‘name‘] + ‘ ‘ + matchObj[0])

for i in range(len(list1)):
    print("序号:%s   服务名:%s  分支名:%s" % (i + 1, list1[i].split()[0],list1[i].split()[1]))

print(‘\n‘)

for i in range(len(list2)):
    print("序号:%s   服务名:%s 分支名:%s" % (i + 1, list2[i].split()[0],list2[i].split()[1]))

def SetBranch():
yamlData = GetYaml()
jenkins_server_url = yamlData[‘Jenkins‘][‘url‘]
userName = yamlData[‘Jenkins‘][‘username‘]
password = yamlData[‘Jenkins‘][‘password‘]

driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(10)
driver.get(jenkins_server_url)
driver.find_element_by_id("j_username").send_keys(userName)
driver.find_element_by_name("j_password").send_keys(password)
driver.find_element_by_id("yui-gen1-button").click()

server = linkJenkins()
jobsLen = len(server.get_jobs())
mustBuildJob = GetYaml()[‘Server‘]
for i in range(jobsLen):
    jobName = server.get_jobs()[i][‘name‘]
    for key in mustBuildJob:
        if jobName == key:
            driver.get(jenkins_server_url + "job/" + key + "/configure")
            branchKey = driver.find_element_by_xpath(‘//*[@id="main-panel"]/div/div/div/form/***/input‘)
           ** # print(branchKey.get_attribute(‘value‘))**
            branchKey.clear()
            branchKey.send_keys("*/" + str(mustBuildJob[key]))
            time.sleep(2)
            driver.find_element_by_name(‘Submit‘).click()
            time.sleep(2)
            server.build_job(jobName)

def SetMaster():
yamlData = GetYaml()
jenkins_server_url = yamlData[‘Jenkins‘][‘url‘]
userName = yamlData[‘Jenkins‘][‘username‘]
password = yamlData[‘Jenkins‘][‘password‘]

driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(10)
driver.get(jenkins_server_url)
driver.find_element_by_id("j_username").send_keys(userName)
driver.find_element_by_name("j_password").send_keys(password)
driver.find_element_by_id("yui-gen1-button").click()

server = linkJenkins()
jobsLen = len(server.get_jobs())
for i in range(jobsLen):
    driver.get(jenkins_server_url + "job/" + server.get_jobs()[i][‘name‘] + "/configure")
    branchKey = driver.find_element_by_xpath(‘//*[@id="main-panel"]/div/div/div/form/table/tbody***/input‘)
    branch = branchKey.get_attribute(‘value‘).split(‘/‘)[1]
    if branch != ‘master‘:
        branchKey.clear()
        branchKey.send_keys("*/master")
        time.sleep(1)
        driver.find_element_by_name(‘Submit‘).click()
        server.build_job(server.get_jobs()[i][‘name‘])                              

JenkinsBranch.yaml

Jenkins:
url: http://jenkins.test.***.cn/jenkins/
username: 123.gean
password: 123654

Server:
api-invoice: master
api-sms: master

if name == "main":

link = linkJenkins()
CheckBranch(link)
SetBranch()
SetMaster()

python-jenkins 自动化实践(UI 和 API 自动化 混合应用)

标签:mit   名称   odi   yaml   range   drive   obj   sub   turn   

原文地址:https://blog.51cto.com/14103476/2533677


评论


亲,登录后才可以留言!