持续集成是一种软件开发实践,即团队开发成员常常集成他们的工做,经过每一个成员天天至少集成一次,也就意味着天天可能会发生屡次集成。每次集成都经过自动化的构建(包括编译,发布,自动化测试)来验证,从而尽早地发现集成错误。 --马丁福勒php
持续集成的前提必需要有一个健壮且分明的版本工具,毫无疑问咱们这里使用git
做为版本工具html
对于hotfix和feature分支容许开发者push,对于develop和master分支只容许开发者merge。java
配置服务器秘钥 node
添加系统钩子,并选择在何时触发钩子python
这里只贴出部分代码只供参考,由于具体需求可能不一样,这里就抛砖引玉。nginx
#!/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脚原本处理laravel
#!/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.."
复制代码
开发分支和预算线分支与上面大体相同,这里就不贴出来了git
请点击ELK实时分析之php的laravel项目日志 github
gitlab合并请求推送至钉钉 web
nginx访问url钉钉推送
ELK展现php错误日志
喜欢我写的东西的朋友能够关注一下个人公众号,上面有个人学习资源以及一些其余福利。:Devops部落