表題のことを行いたいのですが、うまくいきません。。
できていること(環境aws ec2 linux php 5.3)
- backlogからjsonデータをwebhookにて取得
- サーバーにgitをインストール
- サーバーにteratermによりアクセス
デプロイファイル
<?php // リポジトリ名 $repo_name = 'hoge '; // Gitレポジトリの位置の指定 $git_dir = '/var/www/html/hoge/' . $repo_name . '.git'; // 展開先ディレクトリの指定 $work_tree = '/var/www/html/hoge/'; // logファイルの指定 $log_file = '/var/www/html/hoge/deploy.log'; // Gitコマンドパス $git_command = '/usr/local/bin/git'; // リリースするブランチの指定 $deploy_ref = 'refs/heads/master'; $request_body = file_get_contents('php://input'); $request_body = urldecode($request_body); $request_body = str_replace("payload=", "", $request_body); $payload = json_decode($request_body); $checkout = false; if ($payload){ $ref = $payload->content->ref; if ($ref == $deploy_ref) { $checkout = true; } } 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)
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); } //deploy.logはちゃんと生成されていますが、Commit: が空欄になります。
teraterm上でsshアクセスし下記を順番に行うとちゃんと出力されます。
$ sudo git --git-dir=/var/www/html/hoge.gif fetch $ sudo git --git-dir=/var/www/html/hoge.git --work-tree=/var/www/html/hoge/ checkout -f
順番に打ち込むとちゃんとファイルが更新されるのですが、
なぜかwebhookを通すとちゃんと動いてくれません。
どこがおかしいでしょうか?
https://gist.github.com/hissy/6a114d0d9a85f3338aed
を参考にさせていただきました。
あなたの回答
tips
プレビュー