Setting up automatic GIT deployment of a PHP project

I want to do this to switch from FTP deployment to GIT. I want to say, I want me to automatically sync my private Bitbucket repo and my shared web hosting. I googled and found the following script to deploy my web server ( based on this article ).

// Set these dependant on your BB credentials $username = 'username'; $password = 'password'; // Grab the data from BB POST service and decode $json = stripslashes($_POST['payload']); $data = json_decode($json); // Set some parameters to fetch the correct files $uri = $data->repository->absolute_url; $node = $data->commits[0]->node; $files = $data->commits[0]->files; // Foreach through the files and curl them over foreach ($files as $file) { if ($file->type == "removed") { unlink($file->file); } else { $url = "https://api.bitbucket.org/1.0/repositories" . $uri . "raw/" .$node ."/" . $file->file; $path = $file->file; $dirname = dirname($path); if (!is_dir($dirname)) { mkdir($dirname, 0775, true); } $fp = fopen($path, 'w'); $ch = curl_init($url); curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_FILE, $fp); $data = curl_exec($ch); curl_close($ch); fclose($fp); } } 

The problem is that this works on simple change sets, such as changing a 5-10 file. But when I first click the entire project (for example, with 600-700 files and folders) into my own user profile, this script does not work. (just not, there is no error.log error)

What am I missing?

By the way, can I do something like this:

As you know, Bitbucket can send POST information to the exact url (specified by the user) immediately after committing. Therefore, when deploy.php receives a POST, we can get the whole commit as zip or tar, clear the current files and unzip the new commit into a web server.

Is it possible? If so, how? Any other good way?

Update

I found code for automatically deploying a php project. The problem is that https://bitbucket.org/$username/$reponame/get/tip.zip this url does not work on the bbbucket git private repository: it may be related to authentication (I have not tested this on a public repo) . I need to get the latest commit file and unzip inside my project.

 <? // your Bitbucket username $username = "edifreak"; // your Bitbucket repo name $reponame = "canvas-game-demo"; // extract to $dest = "./"; // leave ./ for relative destination //////////////////////////////////////////////////////// // Let get stuff done! // set higher script timeout (for large repo or slow servers) set_time_limit(380); // download the repo zip file $repofile = file_get_contents("https://bitbucket.org/$username/$reponame/get/tip.zip"); file_put_contents('tip.zip', $repofile); unset($repofile); // unzip $zip = new ZipArchive; $res = $zip->open('tip.zip'); if ($res === TRUE) { $zip->extractTo('./'); $zip->close(); } else { die('ZIP not supported on this server!'); } // delete unnecessary .hg files @unlink("$username-$reponame-tip/.hgignore"); @unlink("$username-$reponame-tip/.hg_archival.txt"); // function to delete all files in a directory recursively function rmdir_recursively($dir) { if (is_dir($dir)) { $objects = scandir($dir); foreach ($objects as $object) { if ($object != "." && $object != "..") { if (filetype($dir."/".$object) == "dir") rmdir_recursively($dir."/".$object); else unlink($dir."/".$object); } } reset($objects); rmdir($dir); } } // function to recursively copy the files function copy_recursively($src, $dest) { if (is_dir($src)) { if($dest != "./") rmdir_recursively($dest); @mkdir($dest); $files = scandir($src); foreach ($files as $file) if ($file != "." && $file != "..") copy_recursively("$src/$file", "$dest/$file"); } else if (file_exists($src)) copy($src, $dest); rmdir_recursively($src); } // start copying the files from extracted repo and delete the old directory recursively copy_recursively("$username-$reponame-tip", $dest); // delete the repo zip file unlink("tip.zip"); // Yep, we're done :) echo "We're done!"; ?> 
+7
source share
3 answers

This solution does not provide authentication:

 // download the repo zip file $repofile = file_get_contents("https://bitbucket.org/$username/$reponame/get/tip.zip"); file_put_contents('tip.zip', $repofile); unset($repofile); 

But curl allows it. Therefore, the zip archive can be downloaded from the private repository in the same way as in the first script.

 $node = ''; // a node from repo, like c366e96f16... $fp = fopen($path, 'w'); $ch = curl_init("https://bitbucket.org/$username/$reponame/get/$node.zip"); curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_FILE, $fp); $data = curl_exec($ch); curl_close($ch); fclose($fp); 

I tested it for my bitbucket account. It works very well.

If necessary, get the latest node change set, that we should use the bitbucket api GET list of change sets :

 $username = 'login'; $password = 'pass'; $owner = $username; // if user is owner $repo = 'repo name'; $response = ""; $callback = function($url, $chunk) use (&$response){ $response .= $chunk; return strlen($chunk); }; $ch = curl_init("https://api.bitbucket.org/1.0/repositories/$owner/$repo/changesets?limit=1"); curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent:Mozilla/5.0')); curl_setopt($ch, CURLOPT_WRITEFUNCTION, $callback); curl_exec($ch); curl_close($ch); $changesets = json_decode($response, true); $node = $changesets['changesets'][0]['node']; $raw_node = $changesets['changesets'][0]['raw_node']; print($node . PHP_EOL); print($raw_node . PHP_EOL); 
+2
source

I recently opened Capistrano, which is a great tool. It was originally designed for rubies, but it also worked great with php http://www.davegardner.me.uk/blog/2012/02/13/php-deployment-with-capistrano/

+1
source

Based on your update, replace the contents of php files with the code below:

 <?php // Set these dependant on your BB credentials $username = ''; $password = ''; // your Bitbucket repo name $reponame = ""; // extract to $dest = "./"; // leave ./ for relative destination // Grab the data from BB POST service and decode $json = stripslashes($_POST['payload']); $data = json_decode($json); // set higher script timeout (for large repo or slow servers) set_time_limit(5000); // Set some parameters to fetch the correct files $uri = $data->repository->absolute_url; $node = $data->commits[0]->node; $files = $data->commits[0]->files; // download the repo zip file $fp = fopen("tip.zip", 'w'); $ch = curl_init("https://bitbucket.org/$username/$reponame/get/$node.zip"); curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_FILE, $fp); $data = curl_exec($ch); curl_close($ch); fclose($fp); // unzip $zip = new ZipArchive; $res = $zip->open('tip.zip'); if ($res === TRUE) { $zip->extractTo('./'); $zip->close(); } else { die('ZIP not supported on this server!'); } // function to delete all files in a directory recursively function rmdir_recursively($dir) { if (is_dir($dir)) { $objects = scandir($dir); foreach ($objects as $object) { if ($object != "." && $object != "..") { if (filetype($dir . "/" . $object) == "dir") rmdir_recursively($dir . "/" . $object); else unlink($dir . "/" . $object); } } reset($objects); rmdir($dir); } } // function to recursively copy the files function copy_recursively($src, $dest) { if (is_dir($src)) { if ($dest != "./") rmdir_recursively($dest); @mkdir($dest); $files = scandir($src); foreach ($files as $file) if ($file != "." && $file != "..") copy_recursively("$src/$file", "$dest/$file"); } else if (file_exists($src)) copy($src, $dest); rmdir_recursively($src); } // start copying the files from extracted repo and delete the old directory recursively copy_recursively("$username-$reponame-$node", $dest); // delete the repo zip file unlink("tip.zip"); ?> 

Update

Here are the repositories of this script (Modified by me) on

0
source

All Articles