基於gitlab webhooks 自動構建流程php
一、服務器安裝 git 服務git
安裝成功web
配置 PHP 腳本:shell
<?php // 接受頭部信息 if (!isset($_GET['youparam'])) { logs('youparamnot isset'); exit(); } if ($_GET['youparam'] != 'qiuxue666') { logs('youparamnot neq key'); exit(); } // 接受參數 $datas = file_get_contents('php://input', 'r');; $datas = json_decode($datas,true); // 參數配置 $brand = 'master'; $execName = './exce_' . $brand .'.sh'; $brands = 'refs/heads/' . $brand; if(isset($datas['ref']) && $datas['ref'] == $brands){ $result = exec($execName); // logs(" exec result: " . $result); } logs($datas['user_name'] . " commit to branch:" . $brand); function logs ($data) { $text = '[' . date('Y-m-d H:i:s') .'] ' . $data . PHP_EOL; file_put_contents('./log/gitlab'.date('md') . '.log', $text,FILE_APPEND); } ?>
shell 腳本編寫:json
#!/bin/sh
#php shell腳本目錄 path="/data/wwwroot/objpath"
#項目腳本目錄(要判斷的目錄)
objpath="/data/wwwroot/objpath/obj" if [ -d "$objpath" ]; then cd "$objpath" git pull else cd "$path" # 拉取 master git clone -b master git@gitlab.com:youname/youobj.git fi
gitlab 配置webhooks:服務器
webhooks 推送的一些參數(具體可寫入日誌查看):gitlab
ref 參數能夠看到推送事件所屬那個分支spa