python使用jenkins-api结合mysql,从md文件中读取文件,实现批量构建

一: 程序整体思想:

1: 公司发版流程为开发在gitlab上创建marddown文件,写上需要发版的工程,运维从git上获取需要发版的工程,手工build项目。

2: 程序的工作流程: 从md文件中读取信息,使用正则表达式过滤想要的工程名,tag号,commit号, 存入mysql中,然后从mysql中select出对应的信息,作为参数传递给Jenkins,执行build,并检查对应的信息。

直接上程序:

1: 从md文件中读取文件并存入到mysql中:\

import markdown

import os

import time

import pymysqlas mysql

import re

today = time.strftime('%Y%m%d',time.localtime(time.time()))

mysql_conn= mysql.connect(

host ="127.0.0.1",

port =3306,

user ="root",

passwd ="tuchengjun",

db ="deploy",

charset="utf8"

)

rcursor = mysql_conn.cursor()

#根据日期创建对应的表

createtable_sql ='''CREATE TABLE `%s` (

  `item` varchar(255) COLLATE utf8_bin NOT NULL,

  `package` varchar(255) COLLATE utf8_bin DEFAULT NULL,

  `tag` varchar(255) COLLATE utf8_bin DEFAULT NULL,

  `persion` varchar(255) COLLATE utf8_bin DEFAULT NULL,

  `commitID` varchar(255) COLLATE utf8_bin DEFAULT NULL,

  `disconf` varchar(255) COLLATE utf8_bin DEFAULT NULL,

  `status` int(255) DEFAULT NULL,

  `commit_right` int(255) DEFAULT NULL,

  `remarks` varchar(255) COLLATE utf8_bin DEFAULT NULL,

  `time`datetime DEFAULT NULL,

  `id` int(11) NOT NULL AUTO_INCREMENT,

  PRIMARY KEY (`id`) USING BTREE

) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;''' % today

rcursor.execute(createtable_sql)

mysql_conn.commit()

# mysql_conn.close()

def md2db():

file ='/Users/tcj/cmb/publish-records/维护记录/20190321/项目列表.md'

    f =open(file)

for linein f.readlines():

deploy_list = []

if re.search('[\s\S]+?\|[\s\S]+?', line, re.I):

if re.match('\|\s\w', line):

if not re.match(r'\|\s[\u4e00-\u9fa5]+',line):

deploy_list.append(line.split('|'))

sql ='''INSERT INTO `deploy`.`%s`(`item`, `package`, `tag`, `persion`, `commitID`, `disconf`, `remarks`) VALUES ('%s', '%s', '%s ', '%s', '%s', '%s', '%s')

                            ''' % (today, deploy_list[0][1], deploy_list[0][2], deploy_list[0][3], deploy_list[0][4], deploy_list[0][5], deploy_list[0][6], deploy_list[0][8])

rcursor.execute(sql)

mysql_conn.commit()

f.close()

if __name__ =='__main__':

md2db()

从mysql中获取对应的信息,构造参数,传入Jenkins,执行build操作。 程序如下:

-- coding: utf-8 -

'''

使用Jenkins API 实现构建, 整体思想是 从数据库中select出需要build的项目,和项目的git tag commitid信息

然后传给Jenkins构建,构建完成后,比对对应的构建版本号的commit信息是否和数据库中存的是一样的。

使用map代替for循环,使用multiprocessing.dummy 完成并行构建,其中pool(n) 中的n决定了同时进行几个build任务

后续的任务是从gitlab 中的markdown 中读出来需要构建的项目和相关信息,自动存到数据库中,已经使用Jenkins的 Promotion Status

插件进行deploy 这样就形成了整个发布过程的闭环。


import time

import pymysqlas mysql

from jenkinsapi.jenkinsimport Jenkins

from jenkinsapi.buildimport Build

import jenkins

import threading

import json

import re

from multiprocessing.dummyimport Poolas ThreadPool

pool = ThreadPool(4)

today = time.strftime('%Y%m%d',time.localtime(time.time()))

jenkins_server = jenkins.Jenkins('http://1.1.1.1/jenkins',username="admin",password="11111")



mysql_conn= mysql.connect(

host ="127.0.0.1",

port =3306,

user ="root",

passwd ="111111",

db ="deploy",

charset="utf8"

)

rcursor = mysql_conn.cursor()

sql ='''select item,tag,commitID from deploy.%s WHERE `tag` <> '' OR `commitID` <> '' ''' % today

rcursor.execute(sql)

f = rcursor.fetchall()

#测试输出

# print(jenkins_server.get_job_name('media-center-api-snapshot-mini'))

# for (i) in f:

#    if jenkins_server.get_job_name('{i}-mini'.format(i=i[0].strip())) is None:

#        print("%s not has mini" % i[0])

#    else:

#        print("%s has mini" % i[0])

# for (i) in f:

#    itrm = '{i}-mini'.format(i=i[0].strip())

#    print(itrm)

#    # print(i[1].split(' ')[2])

def build_jenkins_job(i):

try:

if '{i}-mini'.format(i=i[0].strip())in jenkins_server.get_all_jobs():

build_number = jenkins_server.get_job_info('{i}-mini'.format(i=i[0].strip()))['nextBuildNumber']

param_dict = {'GIT_BRANCH':i[1].split(' ')[2]}

jenkins_server.build_job(parameters=param_dict,name='{i}-mini'.format(i=i[0].strip()))

time.sleep(10)

while True:

print(jenkins_server.get_build_console_output('{i}-mini'.format(i=i[0].strip()), build_number))

if jenkins_server.get_build_info('{i}-mini'.format(i=i[0].strip()), build_number)['building'] ==True:

time.sleep(10)

else:

break

            build_result = jenkins_server.get_build_info('{i}-mini'.format(i=i[0].strip()), build_number)['result']

if build_result =='SUCCESS':

sql2 ='''UPDATE deploy.%s SET status = 1 WHERE item = '%s' ''' % (today,'{i}-mini'.format(i=i[0].strip()))

rcursor.execute(sql2)

mysql_conn.commit()

output = jenkins_server.get_build_console_output('{i}-mini'.format(i='{i}-mini'.format(i=i[0].strip()).strip()), build_number)

for linein output.split('\n'):

if '/usr/bin/git checkout' in line:

revision = line.split()[4][0:6]

break

            if revision == i[2]:

sql3 ='''UPDATE `deploy`.`%s` SET `commit_right` = 1 WHERE item = '%s' ''' % (today,'{i}-mini'.format(i=i[0].strip()))

rcursor.execute(sql3)

mysql_conn.commit()

rcursor.close()

else:

next()

except Exception as e:

print(e)

#利用map函数取代for循环 实现并行执行build操作

if __name__ =='__main__':

pool = ThreadPool(4)

pool.map(build_jenkins_job,f)

pool.close()

pool.join()

# build_jenkins_job()

你可能感兴趣的:(python使用jenkins-api结合mysql,从md文件中读取文件,实现批量构建)