git rev-parse --short HEAD 没有得到最新的提交
git rev-parse --short HEAD doesn't get latest commit
嘿,所以我正在使用这样的 post 挂钩:
<?php
file_put_contents('deploy.log', serialize($_POST['payload']), FILE_APPEND);
$repo_dir = '/home/admin/web/website/repo-fullstack.git';
$web_root_dir = '/home/admin/web/website/public_html';
// Full path to git binary is required if git is not in your PHP user's path. Otherwise just use 'git'.
$git_bin_path = 'git';
$update = false;
// Parse data from Bitbucket hook payload
$payload = json_decode($_POST['payload']);
if (empty($payload->commits)){
// When merging and pushing to bitbucket, the commits array will be empty.
// In this case there is no way to know what branch was pushed to, so we will do an update.
$update = true;
} else {
foreach ($payload->commits as $commit) {
$branch = $commit->branch;
if ($branch === 'production' || isset($commit->branches) && in_array('production', $commit->branches)) {
$update = true;
break;
}
}
}
if ($update) {
// Do a git checkout to the web root
exec('cd ' . $repo_dir . ' && ' . $git_bin_path . ' fetch');
exec('cd ' . $repo_dir . ' && GIT_WORK_TREE=' . $web_root_dir . ' ' . $git_bin_path . ' checkout -f');
// Log the deployment
$commit_hash = shell_exec('cd ' . $repo_dir . ' && ' . $git_bin_path . ' rev-parse --short HEAD');
file_put_contents('deploy.log', date('m/d/Y h:i:s a') . " Deployed branch: " . $branch . " Commit: " . $commit_hash . "\n", FILE_APPEND);
}
?>
问题是 POST HOOK 获得旧提交,并且没有获得最新提交。我注意到即使是 ROOT 用户。我 运行 命令 git rev-parse --short HEAD 它也得到了旧的提交。所以这绝对不是权限问题。
此命令不起作用的任何原因?我只是不明白为什么它会得到一个旧的提交。
编辑:如果您想知道的话,最奇怪的问题是,它获得了 POST HOOK 的正确描述。只是不是正确的提交。这是什么鬼?
这个脚本有几个问题:
$branch = $commit->branch;
替换为
$branch = $payload->repository->default_branch
它执行 git 提取,但您需要 git 拉取,因此在提取后添加此
exec('cd ' . $repo_dir . ' && ' . $git_bin_path . ' pull');
我也遇到了文件从存储库中删除但仍然存在的问题。要解决此问题,请在 git 结帐后添加 git clean,如下所示:
exec('GIT_WORK_TREE=' . $web_root_dir . ' ' . $git_bin_path . ' clean -fdx');
整个事情看起来像这样,我还添加了一个不错的邮件功能:
<?php
$repo_dir = '/srv/users/YOURUSER/gitrepo/APPLICATIONAME';
$web_root_dir = '/srv/users/YOURUSER/apps/APPLICATIONAME/public';
// Full path to git binary is required if git is not in your PHP user's path. Otherwise just use 'git'.
$git_bin_path = 'git';
// Parse data from Github hook payload
$payload = json_decode($_POST['payload']);
$empty = false;
$update = false;
if (empty($payload->commits)){
// When merging and pushing to bitbucket, the commits array will be empty.
// In this case there is no way to know what branch was pushed to, so we will do an update.
$empty = true;
$update = true;
} else {
$branch = $payload->repository->default_branch;
$message = $payload->head_commit->message;
if ($branch === 'master' ) {
$update = true;
}
}
if ($update) {
// Do a git checkout to the web root
exec('cd ' . $repo_dir . ' && ' . $git_bin_path . ' fetch');
exec('cd ' . $repo_dir . ' && ' . $git_bin_path . ' pull');
exec('cd ' . $repo_dir . ' && GIT_WORK_TREE=' . $web_root_dir . ' ' . $git_bin_path . ' checkout -f');
exec('cd ' . $repo_dir . ' && GIT_WORK_TREE=' . $web_root_dir . ' ' . $git_bin_path . ' clean -fdx');
// Log the deployment
$commit_hash = shell_exec('cd ' . $repo_dir . ' && ' . $git_bin_path . ' rev-parse --short HEAD');
file_put_contents('.deploy.log', date('Y-m-d H:i:s') . " Github -- " . $message . " HASH: " . $commit_hash . "\n", FILE_APPEND);
// prepare and send the notification email
$headers = "From: github@YOURDOMAIN.COM\r\n";
$headers .= 'CC: ' . $payload->pusher->email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
// send mail to someone, and the github user who pushed the commit
$body = '<p>The Github user <a href="https://github.com/'
. $payload->pusher->name .'">@' . $payload->pusher->name . '</a>'
. ' has pushed to <b>' . $payload->repository->url
. '</b></p>';
$body .= '<p>Here\'s a brief list of what has been changed:</p>';
$body .= '<ul>';
foreach ($payload->commits as $commit) {
$body .= '<li>'.$commit->message.'<br />';
$body .= '<small style="color:#e67e22 ">Modified: </small><b>'.count($commit->modified)
.'</b> <small style="color:#58d68d ">Added: </small><b>'.count($commit->added)
.'</b> <small style="color:#e74c3c">Removed: </small><b>'.count($commit->removed)
.'</b> <a href="' . $commit->url
. '">Compare here</a></li>';
}
$body .= '</ul>';
$body .= '</pre>';
$body .= '<p>Thanks for contributing, <br/>Github Webhook Endpoint';
$body .= ' @ '.$_SERVER['REMOTE_ADDR'].'</p>';
mail('REPOADMIN@YOURDOMAIN.COM', 'Deployed to YOURAPPLICATION', $body, $headers);
echo(date('Y-m-d H:i:s'));
echo(" \r\n$message deployed");
} else {
header('HTTP/1.1 500 Internal Server Error');
}
?>
嘿,所以我正在使用这样的 post 挂钩:
<?php
file_put_contents('deploy.log', serialize($_POST['payload']), FILE_APPEND);
$repo_dir = '/home/admin/web/website/repo-fullstack.git';
$web_root_dir = '/home/admin/web/website/public_html';
// Full path to git binary is required if git is not in your PHP user's path. Otherwise just use 'git'.
$git_bin_path = 'git';
$update = false;
// Parse data from Bitbucket hook payload
$payload = json_decode($_POST['payload']);
if (empty($payload->commits)){
// When merging and pushing to bitbucket, the commits array will be empty.
// In this case there is no way to know what branch was pushed to, so we will do an update.
$update = true;
} else {
foreach ($payload->commits as $commit) {
$branch = $commit->branch;
if ($branch === 'production' || isset($commit->branches) && in_array('production', $commit->branches)) {
$update = true;
break;
}
}
}
if ($update) {
// Do a git checkout to the web root
exec('cd ' . $repo_dir . ' && ' . $git_bin_path . ' fetch');
exec('cd ' . $repo_dir . ' && GIT_WORK_TREE=' . $web_root_dir . ' ' . $git_bin_path . ' checkout -f');
// Log the deployment
$commit_hash = shell_exec('cd ' . $repo_dir . ' && ' . $git_bin_path . ' rev-parse --short HEAD');
file_put_contents('deploy.log', date('m/d/Y h:i:s a') . " Deployed branch: " . $branch . " Commit: " . $commit_hash . "\n", FILE_APPEND);
}
?>
问题是 POST HOOK 获得旧提交,并且没有获得最新提交。我注意到即使是 ROOT 用户。我 运行 命令 git rev-parse --short HEAD 它也得到了旧的提交。所以这绝对不是权限问题。
此命令不起作用的任何原因?我只是不明白为什么它会得到一个旧的提交。
编辑:如果您想知道的话,最奇怪的问题是,它获得了 POST HOOK 的正确描述。只是不是正确的提交。这是什么鬼?
这个脚本有几个问题:
$branch = $commit->branch;
替换为
$branch = $payload->repository->default_branch
它执行 git 提取,但您需要 git 拉取,因此在提取后添加此
exec('cd ' . $repo_dir . ' && ' . $git_bin_path . ' pull');
我也遇到了文件从存储库中删除但仍然存在的问题。要解决此问题,请在 git 结帐后添加 git clean,如下所示:
exec('GIT_WORK_TREE=' . $web_root_dir . ' ' . $git_bin_path . ' clean -fdx');
整个事情看起来像这样,我还添加了一个不错的邮件功能:
<?php
$repo_dir = '/srv/users/YOURUSER/gitrepo/APPLICATIONAME';
$web_root_dir = '/srv/users/YOURUSER/apps/APPLICATIONAME/public';
// Full path to git binary is required if git is not in your PHP user's path. Otherwise just use 'git'.
$git_bin_path = 'git';
// Parse data from Github hook payload
$payload = json_decode($_POST['payload']);
$empty = false;
$update = false;
if (empty($payload->commits)){
// When merging and pushing to bitbucket, the commits array will be empty.
// In this case there is no way to know what branch was pushed to, so we will do an update.
$empty = true;
$update = true;
} else {
$branch = $payload->repository->default_branch;
$message = $payload->head_commit->message;
if ($branch === 'master' ) {
$update = true;
}
}
if ($update) {
// Do a git checkout to the web root
exec('cd ' . $repo_dir . ' && ' . $git_bin_path . ' fetch');
exec('cd ' . $repo_dir . ' && ' . $git_bin_path . ' pull');
exec('cd ' . $repo_dir . ' && GIT_WORK_TREE=' . $web_root_dir . ' ' . $git_bin_path . ' checkout -f');
exec('cd ' . $repo_dir . ' && GIT_WORK_TREE=' . $web_root_dir . ' ' . $git_bin_path . ' clean -fdx');
// Log the deployment
$commit_hash = shell_exec('cd ' . $repo_dir . ' && ' . $git_bin_path . ' rev-parse --short HEAD');
file_put_contents('.deploy.log', date('Y-m-d H:i:s') . " Github -- " . $message . " HASH: " . $commit_hash . "\n", FILE_APPEND);
// prepare and send the notification email
$headers = "From: github@YOURDOMAIN.COM\r\n";
$headers .= 'CC: ' . $payload->pusher->email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
// send mail to someone, and the github user who pushed the commit
$body = '<p>The Github user <a href="https://github.com/'
. $payload->pusher->name .'">@' . $payload->pusher->name . '</a>'
. ' has pushed to <b>' . $payload->repository->url
. '</b></p>';
$body .= '<p>Here\'s a brief list of what has been changed:</p>';
$body .= '<ul>';
foreach ($payload->commits as $commit) {
$body .= '<li>'.$commit->message.'<br />';
$body .= '<small style="color:#e67e22 ">Modified: </small><b>'.count($commit->modified)
.'</b> <small style="color:#58d68d ">Added: </small><b>'.count($commit->added)
.'</b> <small style="color:#e74c3c">Removed: </small><b>'.count($commit->removed)
.'</b> <a href="' . $commit->url
. '">Compare here</a></li>';
}
$body .= '</ul>';
$body .= '</pre>';
$body .= '<p>Thanks for contributing, <br/>Github Webhook Endpoint';
$body .= ' @ '.$_SERVER['REMOTE_ADDR'].'</p>';
mail('REPOADMIN@YOURDOMAIN.COM', 'Deployed to YOURAPPLICATION', $body, $headers);
echo(date('Y-m-d H:i:s'));
echo(" \r\n$message deployed");
} else {
header('HTTP/1.1 500 Internal Server Error');
}
?>