2012-10-03 2 views
7

Was ich tun möchte, ist, von FTP-Bereitstellung in GIT zu wechseln. Ich meine, ich möchte mein privates Bitbucket-Repo und mein geteiltes Webhosting automatisch synchronisiert halten. Ich habe gegoogelt und folgendes Skript gefunden, um meinen Webserver zu betreiben (based on this article).Einstellung der automatischen GIT-Bereitstellung von PHP-Projekt

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

Das Problem ist, dass dies auf einfache Changesets wie 5-10 Dateiänderung funktioniert. Aber wenn ich das gesamte Projekt zum ersten Mal (zum Beispiel mit 600-700 Dateien und Ordnern) in mein Bitbucket-Privatprofil schiebe, funktioniert dieses Skript nicht. (nur nicht, kein Fehler auf errors.log)

Was fehlt mir?

By the way, Kann ich etwas tun, wie folgt aus:

Wie wir wissen, Bitbucket können direkt POST Informationen in eine genaue URL (vom Benutzer angegeben) senden eine nach Commit erfolgt ist. Wenn also deploy.php POST empfängt, können wir das gesamte Commit als Zip oder Teer erhalten, unsere aktuellen Dateien bereinigen und das neue Commit auf den Webserver entpacken.

Ist das möglich? Wenn ja, wie? Irgendein anderer guter Weg?

aktualisieren

fand ich den Code für die automatisierte Bereitstellung von PHP-Projekt. Das Problem ist https://bitbucket.org/$username/$reponame/get/tip.zip Diese URL funktioniert nicht auf bitbucket privaten Git Repo: wahrscheinlich im Zusammenhang mit der Authentifizierung (Ich habe dies nicht auf öffentliche Repo getestet) Was ich brauche, ist die letzte commit Zip-Datei und entpacken in meinem Projekt.

<? 

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

Was ist in Ihrem Fehlerprotokoll, wenn Sie dieses Skript ausführen? Vielleicht dauert es zu lange oder zu viel RAM und der httpd bringt es um? – cdhowie

+0

ssh auf die Box und renne 'git pull' oder' git clone'? Oder ein Skript erstellen, das das für Sie erledigt? –

+0

@cdhowie Wie gesagt, gibt es kein error.log. Weiß nicht, was passiert – heron

Antwort

2

Diese Lösung kann bietet keine Authentifizierung:

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

Aber curl es erlaubt. Ein Zip-Archiv kann also wie im ersten Skript von einem privaten Repository heruntergeladen werden.

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

Ich habe es für mein Bitbucket-Konto getestet. Es funktioniert sehr gut.

letzten changeset Knoten zu erhalten, die wir bitbucket api verwenden sollten GET a list of changesets Falls erforderlich:

$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

Ich kann nicht, was in PHP-Datei schreiben? – heron

+0

@epic_syntax Lies meine Antwort –

0

Basierend auf dem Update, ersetzen Sie PHP-Dateien Inhalt mit Code unter:

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

aktualisieren

Hier sind Repositories dieser Skript (Geändert von Me) auf

  1. GitHub
  2. Bitbucket