Skip to content

Instantly share code, notes, and snippets.

@867
Last active December 21, 2015 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 867/3b489d9b9e4ef8a43be5 to your computer and use it in GitHub Desktop.
Save 867/3b489d9b9e4ef8a43be5 to your computer and use it in GitHub Desktop.
BacklogのサブディレクトリのみをCPIへのデプロイするスクリプト。
<?php
/**
* BacklogからCPIへのデプロイスクリプト(https://gist.github.com/hissy/6a114d0d9a85f3338aed)
* 上記に対し、Gitのサブディレクトリ(/html)のみをデプロイするよう改変しました。
*/
// CPIユーザーID(契約情報で確認してください)
$user_id = 'abc123defg';
// Gitレポジトリの位置の指定(固定で[.git])
$git_dir = '/usr/home/' . $user_id . '/.git';
// 展開先ディレクトリの指定(/htmlに展開するので固定で親ルートを指定)
$work_tree = '/usr/home/' . $user_id;
// logファイルの指定
$log_file = '/usr/home/' . $user_id . '/deploy.log';
// Gitコマンドパス
$git_command = '/usr/local/bin/git';
// リリースするブランチの指定
$deploy_ref = 'refs/heads/master';
/**
* Git Webフックを受信する
*/
$payload = json_decode($_POST['payload']);
// 指定されたブランチかどうかの確認
$checkout = false;
if ($payload){
$ref = $payload->ref;
if ($ref == $deploy_ref) {
$checkout = true;
}
}
// 指定されたブランチの場合、fetch+checkoutを実行して、最終コミットをlogファイルに保存する
if ($checkout) {
exec($git_command . ' --git-dir=' . $git_dir . ' fetch');
exec($git_command . ' --git-dir=' . $git_dir . ' --work-tree=' . $work_tree . ' checkout -f');
$commit_hash = shell_exec($git_command . ' --git-dir=' . $git_dir . ' rev-parse --verify HEAD');
file_put_contents($log_file, date('r') . " Ref: " . $ref . " Commit: " . $commit_hash . "\n", FILE_APPEND);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment