Created
January 4, 2015 17:00
-
-
Save hissy/d9f0783df8f09e41c964 to your computer and use it in GitHub Desktop.
Automated git deployments from Backlog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Original: http://jonathannicol.com/blog/2013/11/19/automated-git-deployments-from-bitbucket/ | |
$repo_dir = '/home/<username>/<repo-name>.git'; | |
$web_root_dir = '/home/<username>/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 ($payload){ | |
$update = true; | |
$ref = $payload->ref; | |
} | |
$ref = ($ref) ? $ref : ''; | |
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') . " Ref: " . $ref . " Commit: " . $commit_hash . "\n", FILE_APPEND); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment