7

私がやりたいのは、FTP展開からGITに切り替えることです。つまり、Bitbucketプライベートリポジトリと共有ウェブホスティングの同期を自動的に維持したいのです。私はグーグルで検索し、(この記事に基づいて) Webサーバーをデプロイするための次のスクリプトを見つけました。

// 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にエラーはありません)

私は何が欠けていますか?

ちなみに、私はそのようなことをすることができますか?

ご存知のように、Bitbucketは、コミットが行われた直後に、POST情報を(ユーザーが指定した)正確なURLに送信できます。したがって、deploy.phpがPOSTを受信すると、コミット全体をzipまたはtarとして取得し、現在のファイルをクリーンアップして、新しいコミットをWebサーバーに解凍できます。

それは可能ですか?はいの場合、どのように?他の良い方法はありますか?

アップデート

phpプロジェクトの自動デプロイ用に以下のコードを見つけました。問題はhttps://bitbucket.org/$username/$reponame/get/tip.zip、このURLがbitbucketプライベートgitリポジトリで機能しないことです:おそらく認証に関連しています(パブリックリポジトリでこれをテストしていません)必要なのは、最後のコミットの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!";
    
?>
4

3 に答える 3

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 アカウントでテストしました。とてもよく効きます。

最後のチェンジセット ノードを取得する必要がある場合は、bitbucket api GET チェンジセットのリストを使用する必要があります。

$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);
于 2012-10-07T01:00:59.527 に答える
1

私は最近、優れたツールである Capistrano を発見しました。最初は ruby​​ 用に開発されましたが、php http://www.davegardner.me.uk/blog/2012/02/13/php-deployment-with-capistrano/と組み合わせても素晴らしいです。

于 2012-10-07T03:07:25.097 に答える
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. ビットバケット
于 2012-10-07T06:01:40.113 に答える