こんにちは個別指導塾コミット塾長、AWESOME開発担当の船津です。
目次
今回は面倒だけどやっておきたい簡易的なgitでの自動デプロイです。
wordpressのあまり大きくないプロジェクトだとテーマファイルだけさくっと管理したいという需要があると思います。今回はそんな人向けの記事です。
こちらを参考にさせていただきました。
https://gist.github.com/milo/daed6e958ea534e4eba3
やること
releaseブランチにgit pushするとリモート側のサーバーがgit pull releaseをしてreleaseブランチとファイルを同期させる
サーバーにphpファイルを設置
ファイル名はtest-deploy.phpにしてみます。
<php $hookSecret = 'githubで設定するsecretを書く'; # 空でもOK
set_error_handler(function($severity, $message, $file, $line) { throw new \ErrorException($message, 0, $severity, $file, $line); }); set_exception_handler(function($e) { header('HTTP/1.1 500 Internal Server Error'); echo "Error on line {$e->getLine()}: " . htmlSpecialChars($e->getMessage());
die();
});
$rawPost = NULL;
if ($hookSecret !== NULL) {
if (!isset($_SERVER['HTTP_X_HUB_SIGNATURE'])) {
throw new \Exception("HTTP header 'X-Hub-Signature' is missing.");
} elseif (!extension_loaded('hash')) {
throw new \Exception("Missing 'hash' extension to check the secret code validity.");
}
list($algo, $hash) = explode('=', $_SERVER['HTTP_X_HUB_SIGNATURE'], 2) + array('', '');
if (!in_array($algo, hash_algos(), TRUE)) {
throw new \Exception("Hash algorithm '$algo' is not supported.");
}
$rawPost = file_get_contents('php://input');
if ($hash !== hash_hmac($algo, $rawPost, $hookSecret)) {
throw new \Exception('Hook secret does not match.');
}
};
if (!isset($_SERVER['HTTP_CONTENT_TYPE'])) {
throw new \Exception("Missing HTTP 'Content-Type' header.");
} elseif (!isset($_SERVER['HTTP_X_GITHUB_EVENT'])) {
throw new \Exception("Missing HTTP 'X-Github-Event' header.");
}
switch ($_SERVER['HTTP_CONTENT_TYPE']) {
case 'application/json':
$json = $rawPost ?: file_get_contents('php://input');
break;
case 'application/x-www-form-urlencoded':
$json = $_POST['payload'];
break;
default:
throw new \Exception("Unsupported content type: $_SERVER[HTTP_CONTENT_TYPE]");
}
# イベントタイプで処理を分けます
# https://developer.github.com/v3/activity/events/types/
$payload = json_decode($json);
switch (strtolower($_SERVER['HTTP_X_GITHUB_EVENT'])) {
# githubからのテストping送信への応答
case 'ping':
echo 'pong';
break;
# push時の応答 OKを返しておきます
case 'push':
# テーマフォルダに移動して、git pull
exec('cd テーマフォルダの絶対パス; git pull origin release');
default:
header('HTTP/1.0 404 Not Found');
echo "Event:$_SERVER[HTTP_X_GITHUB_EVENT] Payload:\n";
print_r($payload); # For debug only. Can be found in GitHub hook log.
die();
}
ファイルが作れたらサーバーの公開用ディレクトリに設置します。
githubの設定
設定ははこんな感じにします。
secretには先程のファイルの中に記述したものを使います。

成功するとこういったレスポンスが返ってきます。

コマンドが正しければreleaseブランチの内容がpullされてきているはずです。
最後に
いかがでしょうか?大抵の場合はこれで済んでしまうかと思われます。
セキュリティに関してはいろいろと課題がありそうですね。











