- 論壇徽章:
- 0
|
為了解決自動部署問題,查詢了一些解決方案后,為了穩(wěn)定最終決定使用Git@OSC來實現(xiàn)該需求。
詳見:http://blog.skyx.in/archives/158/
deploy.php- <?php
- header("Content-type: text/html; charset=utf-8");
-
- if (! isset($_REQUEST['hook'])) die ('非法請求');
-
- $config = require('config.php');
- //hook內(nèi)容詳見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',
- ],
- ]
- ];
復制代碼 |
|