APP下载

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

消息来源:baojiabao.com 作者: 发布时间:2024-05-17

报价宝综合消息初创团队持续整合的落地与实现(gitlab+python)

持续整合概念

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

git工作分支

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

这里只简单说一下各个分支的作用,想了解更多关于git工作流知识,请点选https://www.cnblogs.com/xirongliu/p/4584653.html

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="https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxx"

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 [email protected]:${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部落

2019-07-04 14:49:00

相关文章