2012-10-03 31 views
7

की स्वचालित जीआईटी परिनियोजन सेट करना, मैं क्या करना चाहता हूं, एफ़टीपी परिनियोजन से जीआईटी में स्विच करने के लिए है। मेरा मतलब है, मैं अपने बिटबकेट निजी रिपो और मेरे साझा वेबहोस्टिंग को स्वचालित रूप से समन्वयित रखना चाहता हूं। मैंने अपने वेबसर्वर को तैनात करने के लिए निम्नलिखित स्क्रिप्ट पाई और पाया (based on this article)।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);  
    } 
} 

समस्या यह है कि यह 5-10 फ़ाइल परिवर्तन जैसे सरल परिवर्तनों पर काम करता है। लेकिन जब मैं अपनी परियोजना को पहली बार धक्का देता हूं (उदाहरण के लिए 600-700 फाइलों और फ़ोल्डरों के साथ) मेरी बिटबकेट निजी प्रोफ़ाइल में, यह स्क्रिप्ट काम नहीं करती है। (बस नहीं, error.log पर कोई त्रुटि नहीं)

मुझे क्या याद आ रही है? सीधे के बाद एक प्रतिबद्ध किया गया है

हम जानते हैं, Bitbucket एक सटीक यूआरएल (उपयोगकर्ता द्वारा दिए गए) में पोस्ट जानकारी भेज सकते हैं:

वैसे, मैं ऐसा ही कुछ कर सकते हैं। तो जब deploy.php पोस्ट प्राप्त करता है, हम पूरी प्रतिबद्धता को ज़िप या टैर के रूप में प्राप्त कर सकते हैं, हमारी वर्तमान फ़ाइलों को साफ़ कर सकते हैं और नई प्रतिबद्धता को वेबसर्वर में अनजिप कर सकते हैं।

क्या यह संभव है? यदि हां तो कैसे? कोई अन्य अच्छा तरीका?

अद्यतन

मैं स्वचालित तैनाती php परियोजना के लिए नीचे दिए गए कोड को पाया। समस्या https://bitbucket.org/$username/$reponame/get/tip.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

एसएसएच बॉक्स पर और 'गिट पुल' या 'गिट क्लोन' चलाएं? या एक स्क्रिप्ट बनाएं जो आपके लिए ऐसा करेगी? –

+0

@cdhowie जैसा कि मैंने कहा, कोई त्रुटि नहीं है। लॉग इन करें। डुनो क्या होता है – 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); 

लेकिन कर्ल यह अनुमति देता है। इसलिए एक ज़िप संग्रह को एक निजी भंडार से डाउनलोड किया जा सकता है जैसे कि पहली स्क्रिप्ट में।

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

मैंने इसे अपने बिटबकेट खाते के लिए परीक्षण किया है। यह बहुत अच्छा काम है।

तो पिछले changeset नोड है कि हम bitbucket एपीआई 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