通过git@osc的push钩子实现自动部署
为了解决自动部署问题,查询了一些解决方案后,为了稳定最终决定使用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);
Copy after login
config.php
<?php return [ 'web_user' => 'www', 'web_group' => 'www', 'projects' => [ 'project' => [ 'password' => 'password', 'web_path' => '/home/wwwroot/default/project', ], ] ];
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article
Assassin's Creed Shadows: Seashell Riddle Solution
3 weeks ago
By DDD
What's New in Windows 11 KB5054979 & How to Fix Update Issues
2 weeks ago
By DDD
Where to find the Crane Control Keycard in Atomfall
3 weeks ago
By DDD
Assassin's Creed Shadows - How To Find The Blacksmith And Unlock Weapon And Armour Customisation
4 weeks ago
By DDD
Roblox: Dead Rails - How To Complete Every Challenge
3 weeks ago
By DDD

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics
CakePHP Tutorial
1386
52

