1
0

More than 3 years have passed since last update.

自動デプロイ(手抜き編)

Posted at

やりたいこと

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を設定

以上!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0