我想要做的是從FTP部署切換到GIT。我的意思是,我想保持自動保持同步我的Bitbucket私人回購和我的共享虛擬主機。我搜索了一下,發現了下面的腳本來部署我的網絡服務器(based on this article)。設置PHP項目的自動GIT部署
// Set these dependant on your BB credentials
$username = 'username';
$password = 'password';
// Grab the data from BB's 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);
}
}
問題是,這適用於簡單的變更,如5-10文件更改。但是,當我第一次將整個項目(例如600-700個文件和文件夾)推送到我的bitbucket專用配置文件中時,此腳本不起作用。 (只是沒有,在errors.log沒有錯誤)
我錯過了什麼?
順便說一句,我可以做這樣的事情:直接提交作出後
我們知道,到位桶可以發送POST信息具有準確的網址(由用戶給出)。所以當deploy.php接收POST時,我們可以將整個提交作爲zip或tar,清理我們當前的文件並將新提交解壓縮到web服務器中。
這可能嗎?如果是,那麼如何?任何其他好方法?
更新
我發現下面的代碼自動部署php項目。問題是https://bitbucket.org/$username/$reponame/get/tip.zip
此網址在bitbucket私人git repo上無效:可能與身份驗證有關(我沒有在公開回購中測試過)我需要的是獲取最後一個提交的zip文件並在我的項目中解壓縮。
<?
// your Bitbucket username
$username = "edifreak";
// your Bitbucket repo name
$reponame = "canvas-game-demo";
// extract to
$dest = "./"; // leave ./ for relative destination
////////////////////////////////////////////////////////
// Let's get stuff done!
// set higher script timeout (for large repo's 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!";
?>
運行此腳本時,錯誤日誌中有什麼?也許它需要太長的時間或使用太多的內存,而httpd正在殺死它? – cdhowie
SSH到盒子上並運行'git pull'或'git clone'?或者創建一個可以爲你做的腳本? –
@cdhowie正如我所說,沒有error.log。不知道會發生什麼 – heron