一般項(xiàng)目的開(kāi)發(fā)測(cè)試流程,都會(huì)有測(cè)試服、生產(chǎn)服的角色聪廉,碼云代碼托管中創(chuàng)建雙分支,讓其master分支對(duì)應(yīng)生產(chǎn)服干签,develop分支對(duì)應(yīng)測(cè)試服容劳,分別實(shí)現(xiàn)自動(dòng)更新部署,現(xiàn)在分兩種情況留量,分別給出相應(yīng)的webhooks源代碼(PHP),實(shí)現(xiàn)本自動(dòng)更新的基礎(chǔ)是在相應(yīng)的服務(wù)器上已經(jīng)摘取了對(duì)應(yīng)的分支版本并且配置了部署公鑰等基礎(chǔ)操作可岂,如不了解稚茅,請(qǐng)自行搜索峰锁,或者參考我的另一篇文章《項(xiàng)目創(chuàng)建部署全流程--碼云創(chuàng)建項(xiàng)目飒货、部署webhooks服務(wù)器自動(dòng)更新》
一、測(cè)試站點(diǎn)與正式站點(diǎn)都部署在同一服務(wù)器上
這種情況比較簡(jiǎn)單扣墩,每次請(qǐng)求根據(jù)Push操作的分支來(lái)判斷相應(yīng)更新測(cè)試站點(diǎn)或者正式站點(diǎn)的項(xiàng)目文件即可呻惕,參考源代碼如下
<?php
/*
* 碼云webhooks自動(dòng)更新程序
* 根據(jù)不同分支來(lái)自動(dòng)更新正式站/測(cè)試站
* $logdir 日志存放目錄
* $name 站點(diǎn)標(biāo)識(shí)字符盲泛,多站點(diǎn)方便區(qū)分日志
* $password 碼云設(shè)置的訪問(wèn)密碼
* $wwwroot 正式站點(diǎn)目錄
* $testdir 測(cè)試站點(diǎn)目錄
* author: 王小滔
* date: 2019-11-23
*/
$logdir = "/www/wwwroot/webhook/logs/";
$name = 'project_name';
$password = 'your_password';
$wwwroot = '/www/wwwroot/project_name';
$testdir = '/www/wwwroot/project_name_test';
$json = file_get_contents("php://input");
$data = json_decode($json,true);
if ($data['password'] != $password) {
die('Password id error!');
}
if (isset($data['ref'])&& $data['total_commits_count'] > 0) {
if ($data['ref'] == 'refs/heads/master') {
$dir = $wwwroot;
$branch = 'master';
$site = '正式';
} else {
$dir = $testdir;
$branch = 'develop';
$site = '測(cè)試';
}
$res = PHP_EOL . "pull start ---------------------------------------------" . PHP_EOL;
$res .= shell_exec("cd {$dir} && git pull origin {$branch} 2<&1 ");
$res_log = '------------------------------------------------------------' . PHP_EOL;
$res_log .= $site . '站點(diǎn)更新村视,目錄:' . $dir . PHP_EOL;
$res_log .= $data['user_name'] . ' 在 ' . date('Y-m-d H:i:s'). ' 向 ' . $data['repository']['name'] . ' 項(xiàng)目的 ' . $data['ref'] . ' 分支push了 ' . $data['total_commits_count'] . ' 個(gè)commit:' . $data['commits']['message'];
$res_log .= $res . PHP_EOL;
$res_log .= "pull end -----------------------------------------------------" . PHP_EOL;
file_put_contents($logdir . $name . "_" . date('Ymd'). ".txt",$res_log,FILE_APPEND);
}
二、測(cè)試站點(diǎn)與正式站點(diǎn)分別部署在測(cè)試服與生產(chǎn)服
這種情況,其實(shí)也不復(fù)雜质况,只不過(guò)需要事先分別創(chuàng)建兩個(gè)服務(wù)器的部署公鑰添加到碼云囤捻,然后在webhooks添加兩個(gè)鉤子文件網(wǎng)址即可,以下是測(cè)試服與生產(chǎn)服分別的對(duì)應(yīng)源代碼
1蝎土、測(cè)試服webhook源代碼(對(duì)應(yīng)develop分支)
<?php
/*
* 碼云webhooks自動(dòng)更新程序
* 根據(jù)不同分支來(lái)自動(dòng)更新正式站/測(cè)試站
* $logdir 日志存放目錄
* $name 站點(diǎn)標(biāo)識(shí)字符视哑,多站點(diǎn)方便區(qū)分日志
* $password 碼云設(shè)置的訪問(wèn)密碼
* $testdir 測(cè)試站點(diǎn)目錄
* author: 王小滔
* date: 2019-11-25
*/
$logdir = "/www/wwwroot/webhook/logs/";
$name = 'project_name';
$password = 'your_password';
$testdir = '/www/wwwroot/project_name_test';
$json = file_get_contents("php://input");
$data = json_decode($json,true);
if ($data['password'] != $password) {
die('Password id error!');
}
if (isset($data['ref'])&& $data['total_commits_count'] > 0) {
if ($data['ref'] == 'refs/heads/develop') {
$res = PHP_EOL . "pull start ---------------------------------------------" . PHP_EOL;
$res .= shell_exec("cd {$testdir} && git pull origin develop 2<&1 ");
$res_log = '------------------------------------------------------------' . PHP_EOL;
$res_log .= '測(cè)試站點(diǎn)更新,目錄:' . $testdir . PHP_EOL;
$res_log .= $data['user_name'] . ' 在 ' . date('Y-m-d H:i:s'). ' 向 ' . $data['repository']['name'] . ' 項(xiàng)目的 ' . $data['ref'] . ' 分支push了 ' . $data['total_commits_count'] . ' 個(gè)commit:' . $data['commits']['message'];
$res_log .= $res . PHP_EOL;
$res_log .= "pull end -----------------------------------------------------" . PHP_EOL;
file_put_contents($logdir . $name . "_" . date('Ymd'). ".txt",$res_log,FILE_APPEND);
}
}
2誊涯、生產(chǎn)服webhook源代碼(對(duì)應(yīng)master分支)
<?php
/*
* 碼云webhooks自動(dòng)更新程序
* 根據(jù)不同分支來(lái)自動(dòng)更新正式站/測(cè)試站
* $logdir 日志存放目錄
* $name 站點(diǎn)標(biāo)識(shí)字符挡毅,多站點(diǎn)方便區(qū)分日志
* $password 碼云設(shè)置的訪問(wèn)密碼
* $testdir 測(cè)試站點(diǎn)目錄
* author: 王小滔
* date: 2019-11-25
*/
$logdir = "/www/wwwroot/webhook/logs/";
$name = 'project_name';
$password = 'your_password';
$wwwroot = '/www/wwwroot/project_name';
$json = file_get_contents("php://input");
$data = json_decode($json,true);
if ($data['password'] != $password) {
die('Password id error!');
}
if (isset($data['ref'])&& $data['total_commits_count'] > 0) {
if ($data['ref'] == 'refs/heads/master') {
$res = PHP_EOL . "pull start ---------------------------------------------" . PHP_EOL;
$res .= shell_exec("cd {$wwwroot} && git pull origin master 2<&1 ");
$res_log = '------------------------------------------------------------' . PHP_EOL;
$res_log .= '正式站點(diǎn)更新,目錄:' . $wwwroot . PHP_EOL;
$res_log .= $data['user_name'] . ' 在 ' . date('Y-m-d H:i:s'). ' 向 ' . $data['repository']['name'] . ' 項(xiàng)目的 ' . $data['ref'] . ' 分支push了 ' . $data['total_commits_count'] . ' 個(gè)commit:' . $data['commits']['message'];
$res_log .= $res . PHP_EOL;
$res_log .= "pull end -----------------------------------------------------" . PHP_EOL;
file_put_contents($logdir . $name . "_" . date('Ymd'). ".txt",$res_log,FILE_APPEND);
}
}
OK暴构,就是這么簡(jiǎn)單跪呈,希望對(duì)大家有幫助,如果還有什么問(wèn)題取逾,歡迎隨時(shí)與我交流耗绿!