标签:原理 com 需要 事件 开发环境 local 测试环境 compose ret
持续集成概念持续集成是一种软件开发实践,即团队开发成员经常集成他们的工作,通过每个成员每天至少集成一次,也就意味着每天可能会发生多次集成。每次集成都通过自动化的构建(包括编译,发布,自动化测试)来验证,从而尽早地发现集成错误。 --马丁福勒
持续集成的前提必须要有一个健壮且分明的版本工具,毫无疑问我们这里使用git
作为版本工具
对于hotfix和feature分支允许开发者push,对于develop和master分支只允许开发者merge。
配置服务器秘钥
这里只贴出部分代码只供参考,因为具体需求可能不同,这里就抛砖引玉。
#!/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 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
#!/bin/bash
#定义文件目录
Dir="/var/www/html"
EnvirmentJs="/var/www/html/ucarCarWeb/src/js/environment.js.develop"
DirEnvirJs="/var/www/html/ucarCarWeb/src/js/environment.js"
EnjoyJsDe="/var/www/html/EnjoyCarWeb/src/config/environment.js.develop"
EnjoyJs="/var/www/html/EnjoyCarWeb/src/config/environment.js"
function pull_say() {
PullDir=$1
if [ $? -ne 0 ];then
echo "$PullDir Git Pull Error"
fi
}
echo ‘start‘
if [ $1 == "EnjoyCarWeb" ];then
cd $Dir/EnjoyCarWeb
startTime=$(ls -l package.json|awk ‘{print $6,$7,$8}‘)
JstartTime=$(ls -l $EnjoyJsDe|awk ‘{print $6,$7,$8}‘)
#拉取项目代码
git pull origin develop/v1.3.4
pull_say
stopTime=$(ls -l package.json|awk ‘{print $6,$7,$8}‘)
JstopTime=$(ls -l $EnjoyJsDe|awk ‘{print $6,$7,$8}‘)
if [ "$JstartTime" != "$JstopTime" ];then
cp $EnjoyJsDe $EnjoyJs
fi
#编译代码
if [ "$startTime" != "$stopTime" ];then
rm -f package-lock.json
/usr/bin/npm install
/usr/bin/node build/build.js
else
/usr/bin/node build/build.js
fi
echo $1 "Success"
elif [ $1 == "ucarCarWeb" ];then
cd $Dir/ucarCarWeb
startTime=$(ls -l package.json|awk ‘{print $6,$7,$8}‘)
JstartTime=$(ls -l $EnvirmentJs|awk ‘{print $6,$7,$8}‘)
git pull origin develop
pull_say
stopTime=$(ls -l package.json|awk ‘{print $6,$7,$8}‘)
JstopTime=$(ls -l $EnvirmentJs|awk ‘{print $6,$7,$8}‘)
if [ "$startTime" != "$stopTime" ];then
rm -f package-lock.json
/usr/bin/npm install
/usr/bin/node build/build.js
else
/usr/bin/node build/build.js
fi
if [ "$JstartTime" != "$JstopTime" ];then
cp $EnvirmentJs $DirEnvirJs
fi
echo $1 "Success"
fi
echo "Complate.."
开发分支和预算线分支与上面大致相同,这里就不贴出来了
gitlab合并请求推送至钉钉
nginx访问url钉钉推送
喜欢我写的东西的朋友可以关注一下我的公众号,上面有我的学习资源以及一些其他福利。:Devops部落
标签:原理 com 需要 事件 开发环境 local 测试环境 compose ret
原文地址:http://blog.51cto.com/dashui/2340204