月度归档:2016年02月

开源中国的GIT webhook钩子的php版本

php代码

<?php
/**
 * 写日记
 * @param str $msg
 */
function writeLog($msg,$file='')
{
    date_default_timezone_set("Asia/Shanghai");
    $dir = __DIR__.'/log/';
    if(!empty($file))
    {
        $file = $dir.$file;
    }else{
    	$file = $dir.'hook.log';
    }

    if (!file_exists ( $file ))
    {
    	 touch ( $file );
    }
    $info = "\r\n".date('Y-m-d H/i/s').': '.$msg;
    $fh = fopen ( $file, 'a+' );
    fwrite ( $fh, $info);
    fclose ( $fh );
}

if(isset($_POST['hook']))
{
    
	$passwd = '';//这里是您的KEY
	$hookJson = $_POST['hook'];
	writeLog($hookJson,'hook_json.log');
	if($json = json_decode($hookJson))
	{
		if($json->password === $passwd)
		{
			echo exec('/root/toinwork.sh');
			writeLog('toinwork执行成功','success.log');
		}	
	}else{
		writeLog('toinwork,hook密码不正确..执行失败','failure.log');
	}
}

shell执行脚本

#!/bin/bash
WEB_PATH='you is path'
WEB_USER='nginx'
WEB_USERGROUP='nginx'

echo "Start deployment"
cd $WEB_PATH
echo "pulling source code..."
git reset --hard origin/develop
git clean -f
git pull
git checkout develop
echo "changing permissions..."
chown -R $WEB_USER:$WEB_USERGROUP $WEB_PATH
echo "Finished."

请配置执行权限和用户组和用户