持续集成概念
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: PyCharm
from flask import Flask,request,render_template,make_response,Response
import json,os,re,requests
import subprocess
import re
app = 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/bash
Dir="/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服务器配置
效果展示
1.gitlab合并请求推送至钉钉
2.nginx访问url钉钉推送
3.ELK展示php错误日志
完
有问题可以关注一下我的公众号私信我,上面有我的学习资源以及一些其他福利:Devops部落