2012-10-03 22 views
7

Điều tôi muốn làm là chuyển từ triển khai FTP thành GIT. Ý tôi là, tôi muốn tiếp tục tự động tiếp tục đồng bộ hóa repo riêng của Bitbucket và webhosting được chia sẻ của tôi. Tôi googled và tìm thấy kịch bản sau để triển khai máy chủ web của tôi (based on this article).Cài đặt triển khai GIT tự động của dự án PHP

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

Vấn đề là, điều này hoạt động trên các thay đổi đơn giản như thay đổi 5-10 tệp. Nhưng khi tôi đẩy toàn bộ dự án lần đầu tiên (ví dụ với 600-700 tệp và thư mục) vào hồ sơ cá nhân bitbucket của tôi, tập lệnh này không hoạt động. (chỉ cần không, không có lỗi trên errors.log)

Tôi đang thiếu gì?

Bằng cách này, tôi có thể làm điều gì đó như thế:

Như chúng ta đã biết, Bitbucket có thể gửi thông tin POST vào một địa chỉ chính xác (do người dùng) trực tiếp sau khi một cam kết đã được thực hiện. Vì vậy, khi deploy.php nhận POST, chúng ta có thể nhận được toàn bộ cam kết như một zip hoặc tar, làm sạch các tệp hiện tại của chúng ta và giải nén cam kết mới vào máy chủ web.

Điều đó có khả thi không? Nếu có thì làm thế nào? Có cách nào khác tốt không?

Cập nhật

Tôi tìm thấy mã bên dưới để tự động triển khai dự án php. Vấn đề là https://bitbucket.org/$username/$reponame/get/tip.zip url này không hoạt động trên bitbucket private git repo: có thể liên quan đến xác thực (tôi chưa thử nghiệm trên repo công khai) Những gì tôi cần là lấy tệp zip của commit cuối cùng và giải nén bên trong dự án của tôi.

<? 

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

Nhật ký lỗi của bạn khi bạn chạy tập lệnh này là gì? Có lẽ nó mất quá nhiều thời gian hoặc sử dụng quá nhiều RAM và httpd đang giết nó? – cdhowie

+0

ssh vào hộp và chạy 'git pull' hoặc' git clone'? Hoặc tạo ra một kịch bản sẽ làm điều đó cho bạn? –

+0

@cdhowie Như tôi đã nói, không có error.log. Dunno những gì xảy ra – heron

Trả lời

2

Giải pháp này không cung cấp xác thực:

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

Nhưng curl cho phép nó. Vì vậy, một kho lưu trữ zip có thể được tải xuống từ một kho lưu trữ riêng tư giống như trong tập lệnh đầu tiên.

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

Tôi đã thử nghiệm tài khoản bitbucket của mình. Nó hoạt động rất tốt.

Nếu cần thiết để có được nút changeset cuối cùng mà chúng ta nên sử dụng 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

Tôi không thể nhận được, những gì để viết bên trong tập tin php? – heron

+0

@epic_syntax Đọc câu trả lời của tôi –

0

Dựa trên bản cập nhật của bạn, thay thế bạn nội dung file php với mã bên dưới:

<?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"); 
?> 

Cập nhật

Dưới đây là kho này kịch bản (thay đổi bởi Me) trên

  1. GitHub
  2. Bitbucket
Các vấn đề liên quan