Skip to content

Instantly share code, notes, and snippets.

@ikuwow
Last active December 25, 2015 02:14
Show Gist options
  • Save ikuwow/d54434fe19a3fa683c0c to your computer and use it in GitHub Desktop.
Save ikuwow/d54434fe19a3fa683c0c to your computer and use it in GitHub Desktop.
GitLabからwebhook+PHPで超簡易自動デプロイシステムを作る ref: http://qiita.com/ikuwow/items/5794123bf4c379771d1c
http://webhook.mydomain.com/release.php?key=secretkey
<?php
$secret_key = "secretkey"; // GitLab以外からのリクエストを受け付けないためのキー
$git_username = "bot"; // gitlabのログインパスワード
$git_password = "password"; // gitlabのログインパスワード
$log_file = '/path/to/webhook_log.txt';
$date = (new DateTime())->format('Y-m-d H:i:s');
$path_to_repository = '/path/to/repository';
$branch_name = 'release';
$git = '/usr/local/bin/git';
if ( !isset($_GET['key']) || !($_GET['key']==$secret_key) ) {
echo "Invalid key";
$is_logged = error_log("[{$date}] Error: Invalid key\n",3,$log_file);
exit;
}
$json_string = file_get_contents('php://input');
$json = json_decode($json_string,true);
error_log("[{$date}] Pushed to {$json['ref']}\n",3,$log_file);
if ($json['ref']=="refs/heads/{$branch_name}") { // releaseブランチのpushイベントの時のみ実行
$git_remote = "http://{$git_username}:{$git_password}@gitlab.mydomain.com/group/repository_name.git";
$command = "cd {$path_to_repository} && ${git} pull {$git_remote} {$branch_name} 2>&1";
exec($command, $output, $exit_status);
if ($exit_status > 0) {
$error_msg = "[{$date}] Error: \n";
foreach ($output as $line) {
$error_msg .= " {$line}\n";
}
error_log($error_msg,3,$log_file);
} else {
error_log("[{$date}] Updated successfully\n",3,$log_file);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment