2012-10-03 31 views
7

我想要做的是從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!"; 

?> 
+0

運行此腳本時,錯誤日誌中有什麼?也許它需要太長的時間或使用太多的內存,而httpd正在殺死它? – cdhowie

+0

SSH到盒子上並運行'git pull'或'git clone'?或者創建一個可以爲你做的腳本? –

+0

@cdhowie正如我所說,沒有error.log。不知道會發生什麼 – heron

回答

2

該解決方案不提供身份驗證:

// 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); 

但捲曲允許它。因此,可以像使用第一個腳本一樣從專用存儲庫下載zip壓縮文件。

$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); 

我已經測試了它爲我的bitbucket帳戶。它工作得很好。

如果要得到我們應該用到位桶API GET a list of changesets最後變更節點:

$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); 
+0

我無法得到,在php文件裏面寫什麼? – heron

+0

@epic_syntax閱讀我的答案 –

0

根據您的更新,替換你用下面的代碼的PHP文件的內容:

<?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's POST service and decode 
$json = stripslashes($_POST['payload']); 
$data = json_decode($json); 

// set higher script timeout (for large repo's 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"); 
?> 

更新

下面是這個庫腳本(我上次修改)上

  1. GitHub
  2. Bitbucket