git自动化部署之webhooks的使用(php版本)

在github的webhooks中设定对应信息

  • 设定要请求的服务器命令调用地址, 如: http://fizzday.net/webhooks
  • 设定密钥key, 如: fizzday

在服务器上编写对应的命令(纯PHP代码)

  • 编写 http://fizzday.net/webhooks 请求的方法如下:
<?php
// GitHub Webhook Secret.
// Keep it the same with the 'Secret' field on your Webhooks / Manage webhook page of your respostory.
$secret = "";
// 项目根目录, 如: "/var/www/fizzday"
$path = "";
// Headers deliveried from GitHub
$signature = $_SERVER['HTTP_X_HUB_SIGNATURE'];
if ($signature) {
    $hash = "sha1=" . hash_hmac('sha1', $HTTP_RAW_POST_DATA, $secret);
    if (strcmp($signature, $hash) == 0) {
        echo shell_exec("cd {$path} && /usr/bin/git reset --hard origin/master && /usr/bin/git clean -f && /usr/bin/git pull 2>&1");
        exit();
    }
}
http_response_code(404);
  • 命令说明:
shell_exec("cd {$path} && /usr/bin/git reset --hard origin/master && /usr/bin/git clean -f && /usr/bin/git pull 2>&1");

/usr/bin/git reset --hard origin/master 强制恢复版本到前一个稳定版
/usr/bin/git clean -f 清理提交的更改
/usr/bin/git pull 2>&1 拉取最新的版本到本地

也可以调用本地脚本

创建shell脚本文件webhooks.sh, 并写入:

#!/bin/bash
 
WEB_PATH='/var/www/fizzday'
WEB_USER='nginx'
WEB_USERGROUP='nginx'
 
echo "Start deployment"
cd $WEB_PATH
echo "pulling source code..."
git reset --hard origin/master
git clean -f
git pull
git checkout master
echo "changing permissions..."
chown -R $WEB_USER:$WEB_USERGROUP $WEB_PATH
echo "Finished."
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • git常用命令 GIT常用命令备忘:http://stormzhang.com/git/2014/01/27/gi...
    新篇章阅读 8,667评论 1 26
  • 1. 安装 Github 查看是否安装git: $ git config --global user.name "...
    Albert_Sun阅读 13,744评论 9 163
  • 本片内容转自CSDN http://blog.csdn.net/ithomer/article/details/7...
    五娃儿阅读 4,966评论 2 88
  • 简介 传统布局基于盒子模型,通过 display float position 属性进行布局,但是有些效果在盒模型...
    kabumie阅读 1,342评论 0 5
  • 我的文笔不好…………就用第一人称来讲述一下我这段时间的学习历程吧。 那天,天气阴。我像往常一样打开了微信订阅号,点...
    混乱的线阅读 374评论 0 5