<?php
/*
* 碼云webhooks自動(dòng)更新程序
* 根據(jù)不同分支來自動(dòng)更新正式站/測試站
* $logdir 日志存放目錄
* $name 站點(diǎn)標(biāo)識(shí)字符,多站點(diǎn)方便區(qū)分日志
* $password 碼云設(shè)置的訪問密碼
* $wwwroot 正式站點(diǎn)目錄
* $testdir 測試站點(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 = '測試';
? ? }
? ? $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);
}