龙空技术网

初创团队持续集成的落地与实现(gitlab+python)

Devops部落 1776

前言:

此时大家对“持续集成python”大概比较珍视,我们都需要分析一些“持续集成python”的相关内容。那么小编在网摘上收集了一些对于“持续集成python””的相关文章,希望你们能喜欢,看官们快快来了解一下吧!

持续集成概念

持续集成是一种软件开发实践,即团队开发成员经常集成他们的工作,通过每个成员每天至少集成一次,也就意味着每天可能会发生多次集成。每次集成都通过自动化的构建(包括编译,发布,自动化测试)来验证,从而尽早地发现集成错误。 --马丁福勒

git工作分支

持续集成的前提必须要有一个健壮且分明的版本工具,毫无疑问我们这里使用git作为版本工具

这里只简单说一下各个分支的作用,想了解更多关于git工作流知识,请点击
feature/* 功能分支,用于一个新的功能的开发hotfix/* 热修复分支,用于对线上环境的bug热修复develop/* 测试分支,测试环境对应的分支master分支,预上线环境分支

对于hotfix和feature分支允许开发者push,对于develop和master分支只允许开发者merge。

本文原理分析图示首先开发者完成代码后git push到gitlab服务器,通过gitlab上事先设定好的系统钩子来触发一个post请求到后端的webserver服务器。后端webserver服务器收到请求后通过gitlabCI.py分析来源分支与项目组,然后交给不同的shell脚本处理。通过shell脚本来更新不同环境的项目代码,如果是开发分支的话还需要配置nginx并推送访问url至钉钉。ELK服务器监控php的项目报错日志,开发者通过查看然后及时进行debug。webserver对git请求的具体处理图示开发环境(dev_branch)处理流程

对于hotfix/* 或者 feature/*

git push事件 触发gitlab钩子,然后gitlabCI脚本通过条件检测,执行开发分支的shell脚本shell脚本拉去对应的功能分支或者热修复分支,然后拷贝一份nginx模板配置文件,修改对应的域名和目录。重载nginx配置文件并将访问连接推送至钉钉群。gitlab服务器配置配置服务器秘钥添加系统钩子,并选择在什么时候触发钩子webserver配置

配置gitlab处理脚本

这里只贴出部分代码只供参考,因为具体需求可能不同,这里就抛砖引玉。

gitlabCI.py 用于处理gitlab的事件请求

#!/usr/bin/env python3# -*- coding: utf-8 -*-# @Time : 2018-12-18 17:41# @Author : opsonly# @Site :# @File : gitlabCi.py# @Software: PyCharmfrom flask import Flask,request,render_template,make_response,Responseimport json,os,re,requestsimport subprocessimport reapp = Flask(__name__)null = ""cmd = "/var/www/html/"@app.route('/test',methods=['POST'])def hello(): json_dict = json.loads(request.data) name = json_dict['event_name'] #字符串截取分支名 ref = json_dict['ref'][11:] ssl = json_dict['project']['url'] #gitlab项目名 project = json_dict['project']['name'] #gitlab分组名 namespace = json_dict['project']['namespace'] hostfix = re.compile(r'hostfix/*') feature = re.compile(r'feature/*') if name == 'push': if namespace == 'it': #预上线分支 if ref == 'master': cmd = './itmaster.sh ' + project + ref + ' ' + namespace s = subprocess.getoutput(cmd) return Response(s) # 测试分支 elif ref == 'develop': cmd = './itdevelop.sh ' + project + ref + ' ' + namespace s = subprocess.getoutput(cmd) return Response(s) #开发分支 elif hostfix.match(ref) and feature.match(ref): cmd = './itOwn.sh' + project + ref + ' ' + namespace + '' + ssl s = subprocess.getoutput(cmd) return Response(s) elif namespace == 'web': if ref == 'master': cmd = './webMaster.sh ' + project + ref + ' ' + namespace s = subprocess.getoutput(cmd) return Response(s) elif ref == 'develop': cmd = './webDevelop.sh ' + project + ref + ' ' + namespace s = subprocess.getoutput(cmd) return Response(s) # 开发分支 elif hostfix.match(ref) and feature.match(ref): cmd = './webOwn.sh' + project + ref + ' ' + namespace s = subprocess.getoutput(cmd) return Response(s) elif name =='merge_request': #可以定义一个钉钉推送,每次直接点开链接就能直达gitlab合并界面 pass else: return Response('未触发事件')if __name__ == '__main__': app.run()**将不同的请求分发至不同shell脚本来处理**

测试服务器脚本

#!/bin/bashDir="/var/www/html"function ERROR_NOTICE() {	url=""	header="'Content-Type: application/json'"	msg="'{\"msgtype\": \"text\",\"text\": {\"content\":\"$1 $2 $3\"}}'"	a='curl '$url' -H '$header' -d '$msg	eval $a}function IF_TRUE() {	if [ $? -ne 0 ];then		ERROR_NOTICE $1 $2 $3	fi}function master() {	if [ -d $Dir/$1 ];then		cd $Dir/$1		#startTime=$(ls -l composer.lock|awk '{print $6,$7,$8}')		git fetch		git checkout $2 	 git pull origin $2		cp .env.develop .env		 	 composer install		IF_TRUE $1 $2 $3	 #fi 	/usr/local/php7/bin/php artisan queue:restart		IF_TRUE $1 $2 $3		echo $1 " Success"	else		cd $Dir		git clone git@example.com:${3}/${1}.git		cd ${1}		git checkout $2		cp .env.develop .env		composer install		IF_TRUE $1 $2 $3		/usr/local/php7/bin/php artisan queue:restart		IF_TRUE $1 $2 $3	fi}master $1 $2 $3

开发分支和预算线分支与上面大致相同,这里就不贴出来了

ELK服务器配置

ELK实时分析之php的laravel项目日志

效果展示

1.gitlab合并请求推送至钉钉

2.nginx访问url钉钉推送

3.ELK展示php错误日志

最强干货:基于Jenkins+Docker的自动化代码发布流程

你所要知道的python运维常用脚本

最强干货:一文带你深入了解Kubernets(K8s)

有问题可以关注一下我的公众号私信我,上面有我的学习资源以及一些其他福利:Devops部落

标签: #持续集成python