やりたいこと
git pullでデプロイしている時に、gitへのプッシュをトリガーに自動でpullさせる
前提
-
git pull
はSSHユーザで実行する(以下、SSHユーザをmy-ssh-user
とする) - CloneはSSHのプロトコルで行う
例)git clone git@github.com:user/repo.git
- Nginx & PHP 環境
- gitにwebhookの機能がある
手順
1. sudo で nginxユーザにmy-ssh-userユーザでgitコマンドを実行する事を許可する
# visudo
+ nginx ALL=(my-ssh-user) NOPASSWD:/usr/bin/git
2. webhook.php をドキュメントルート直下に配置
<?php
$command = 'cd /documentroot & sudo -u my-ssh-user git pull';
$result = exec($command);
print($result);
3. URLでwebhook.phpを実行して動作確認
例) https://my-domain.example.jp/webhook.php
正常に動作し、最新のソースであれば下記がレスポンスされる
Already up to date.
4. gitのwebhookに3のURLを設定
以上!