7

我想要做的是,从 FTP 部署切换到 GIT。我的意思是,我想自动保持同步我的 Bitbucket 私人仓库和我的共享虚拟主机。我用谷歌搜索并找到了以下脚本来部署我的网络服务器(基于这篇文章)。

// 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 格式获取,清理当前文件并将新提交解压缩到 webserver。

那可能吗?如果是,那怎么办?还有什么好办法吗?

更新

我发现下面的代码用于自动部署 php 项目。问题是https://bitbucket.org/$username/$reponame/get/tip.zip这个 url 在 bitbucket private git repo 上不起作用:可能与身份验证有关(我没有在公共 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!";
    
?>
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);

但是 curl 允许它。因此,可以以与第一个脚本相同的方式从私有存储库下载 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获取变更集列表

$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 回答