首页 > php教程 > PHP源码 > 正文

通过git@osc的push钩子实现自动部署

PHP中文网
发布: 2016-05-23 16:40:55
原创
2013人浏览过

为了解决自动部署问题,查询了一些解决方案后,为了稳定最终决定使用git@osc来实现该需求。

deploy.php

<?php
header("Content-type: text/html; charset=utf-8");
  
if (! isset($_REQUEST['hook'])) die ('非法请求');
  
$config = require('config.php');
//hook内容详见http://git.oschina.net/oschina/git-osc/wikis/HOOK%E9%92%A9%E5%AD%90
$hook = json_decode($_REQUEST["hook"], true);
//$hook = json_decode(file_get_contents('request.json'), true);
$project = $hook['push_data']['repository']['name'];
  
//判断密码
if ($hook['password'] != $config['projects'][$project]['password']) die ("密码错误");
//判断branch
if (trim(strrchr($hook['push_data']['ref'], '/'), '/') != $config['projects'][$project]['branch']) die ("非自动部署分支");
  
$shell = <<<EOF
WEB_PATH='{$config['projects'][$hook['push_data']['repository']['name']]['web_path']}'
WEB_USER='{$config['web_user']}'
WEB_GROUP='{$config['web_group']}'
  
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_GROUP \$WEB_PATH
echo "Finished."
EOF;
  
file_put_contents('deploy.sh', $shell);
$res = shell_exec("bash deploy.sh");
  
$log_file = "{$project}.log";
foreach ($hook['push_data']['commits'] as $commit) {
    file_put_contents($log_file, 
        "※" . date('Y-m-d H:i:s') . "\t" . 
        $hook['push_data']['repository']['name'] . "\t" . 
        $commit['message'] . "\t" . 
        $commit['author']['name'] . PHP_EOL, 
        FILE_APPEND
    );
}
file_put_contents($log_file, $res . PHP_EOL, FILE_APPEND);
登录后复制

config.php

<?php
return [
    'web_user' => 'www',
    'web_group' => 'www',
    'projects' => [
        'project' => [
            'password' => 'password',
            'web_path' => '/home/wwwroot/default/project',
        ],
    ]
];
登录后复制
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号