This article shows you how to automatically deploy your app from a git repo hosted at Bitbucket. That is, whenever you push your master branch to Bitbucket, Bitbucket will POST to a URL on your server, which will then pull and deploy the repo.
The code in this article is based on this tutorial.
cd ~/apps/APPNAME
git clone --mirror git@bitbucket.org:USERNAME/REPONAME.git repo
,
which checks out the repo to a directory called repo.cd repo GIT_WORK_TREE=/srv/users/SYSUSER/apps/APPNAME/public git checkout -f master
<?php $repo_dir = '/srv/users/SYSUSER/apps/APPNAME/repo'; $web_root_dir = '/srv/users/SYSUSER/apps/APPNAME/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'; $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 === 'master' || isset($commit->branches) && in_array('master', $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); } ?>
For debugging, the script in that blog post creates a log file at ~/apps/APPNAME/public/deploy.log. You can also check your app's nginx logs if you aren't sure Bitbucket made the request.