持續集成是一種軟件開發實踐,即團隊開發成員常常集成他們的工做,經過每一個成員天天至少集成一次,也就意味着天天可能會發生屢次集成。每次集成都經過自動化的構建(包括編譯,發佈,自動化測試)來驗證,從而儘早地發現集成錯誤。 --馬丁福勒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部落