Skip to content

Instantly share code, notes, and snippets.

@AtsushiA
Forked from hissy/backlog-deploy.php
Last active August 27, 2020 01:19
Show Gist options
  • Save AtsushiA/341ccca98867b0a1b2941b0cf6fec2c7 to your computer and use it in GitHub Desktop.
Save AtsushiA/341ccca98867b0a1b2941b0cf6fec2c7 to your computer and use it in GitHub Desktop.
Git Webhookを使ったCPIへのデプロイスクリプト
<?php
// CPIユーザーID(契約情報で確認してください)
$user_id = 'abc123defg';
// リポジトリ名(Backlogで確認してください)
$repo_name = 'repository_name';
// Gitレポジトリの位置の指定
$git_dir = '/usr/home/' . $user_id . '/' . $repo_name . '.git';
// 展開先ディレクトリの指定
$work_tree = '/usr/home/' . $user_id . '/html';
// logファイルの指定
$log_file = '/usr/home/' . $user_id . '/deploy.log';
// Gitコマンドパス
$git_command = '/usr/local/bin/git';
// リリースするブランチの指定
$deploy_ref = 'refs/heads/master';
/**
* Git Webフックを受信する
* BacklogのWebフックの仕様の解説はこちら
* http://www.backlog.jp/help/usersguide/git/userguide1710.html
*/
$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);
}
<?php
//original gist by hissy https://gist.github.com/hissy/6a114d0d9a85f3338aed
// ユーザーID(契約情報で確認してください)
$user_id = 'user_id';
// 対象ドメイン名
$domain_name = 'domain_name';
// 対象サブドメイン名
$sub_domain_name = 'sub_domain_name';
// リポジトリ名(Backlogで確認してください)
$repo_name = 'repository_name';
// Gitレポジトリの位置の指定
$git_dir = '/home/' . $user_id . '/git/' . $sub_domain_name . '.' . $domain_name . '/' . $repo_name . '.git';
// 展開先ディレクトリの指定
$work_tree = '/home/' . $user_id . '/' . $domain_name . '/public_html/' . $sub_domain_name;
// logファイルの指定
$log_file = '/home/' . $user_id . '/' . $domain_name . '/public_html/' . $sub_domain_name . '/log/' .'deploy.log';
// Gitコマンドパス
$git_command = '/usr/bin/git';
// リリースするブランチの指定
$deploy_ref = 'refs/heads/master';
/**
* Git Webフックを受信する
* BacklogのWebフックの仕様の解説はこちら
* http://www.backlog.jp/help/usersguide/git/userguide1710.html
*/
$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);
}
<?php
// CPIユーザーID(契約情報で確認してください)
$user_id = 'abc123defg';
// リポジトリ名(Backlogで確認してください)
$repo_name = 'repository_name';
// Gitレポジトリの位置の指定
$git_dir = '/usr/home/' . $user_id . '/' . $repo_name . '.git';
// 展開先ディレクトリの指定
$work_tree = '/usr/home/' . $user_id . '/html';
// logファイルの指定
$log_file = '/usr/home/' . $user_id . '/deploy.log';
// Gitコマンドパス
$git_command = '/usr/local/bin/git';
// リリースするブランチの指定
$deploy_ref = 'refs/heads/master';
/**
* GitHub Webフックを受信する
*/
$payload = json_decode(file_get_contents('php://input'));
// 指定されたブランチかどうかの確認
$checkout = false;
if ($payload && isset($payload->ref)){
$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