PHP exec Vs. apache用户shell:自动GIT部署PHP BitBucket服务器


PHP exec Vs. apache user shell : Automatic GIT deployments PHP BitBucket Server

我做了一些研究,我试图复制类似于JONATHAN NICOL的博客文章,"从Bitbucket自动部署git"。

UPDATE TO ISSUE

我已经启用了exec()函数输出的日志记录,并在命令末尾添加了2>&1

当我打印返回到deploy.log的内容时,我收到以下内容:

//FROM exec('cd /home/apache/www.websitename.org.git && git fetch 2>&1');

(
    [0] => fatal: Not a git repository (or any parent up to mount point /home)
    [1] => Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
)

//exec('cd /home/apache/www.websitename.org.git && GIT_WORK_TREE=/var/www/html/www.websitename.org /usr/bin/git checkout -f');输出

Array
(
    [0] => fatal: Not a git repository (or any parent up to mount point /home)
    [1] => Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
)

问题描述:

如果我复制在引用的PHP Web Hook脚本中包含的exec()函数中执行的命令的输出,那么当我的feature branch合并到dev时对文件所做的更改将被复制到适当的目录中。我的问题是,我想确保在exec()函数中运行的命令实际上正在执行。

起初我认为这是由于权限问题,但我从shell发出的命令与通过PHP的exec()函数发出命令的用户相同。

我似乎不明白为什么Web Hook脚本生成的命令在apache用户的shell中工作,但是当PHP作为apache运行并生成它们时,它们不工作。

环境描述:

我在RedHat 7.2机器上运行BitBucket服务器v4.7.1。该服务器还包含一个开发环境,我希望在合并特定分支时自动部署文件。

我的web根目录是apache可以访问的/var/www/html。当我执行<?php echo exec('whoami'); ?>时,输出是apache

我正在运行PHP 5.6.23,我可以从PHP脚本执行shell命令。

我的php用户是apache, apache也有一个shell连接到这个用户

我在GIT中的默认分支是master,我也有devstaging分支。典型的工作流程是创建master的分支,并将更改提交到功能分支。然后将该功能分支合并到dev,然后是staging,最终返回到master

使用Nicol的博客文章的结构和修改后的BitBucket Repo用于bitbucket.org,我已经能够复制以下内容:

使用以下命令将GIT存储库克隆为镜像:

AS apache user:

cd /home/apache/
git clone --mirror ssh://git@path.to.bitbucket.local:7999/wn/www.websitename.org.git

:

cd /home/apache/www.websitename.org.git
GIT_WORK_TREE=/var/www/html/www.websitename.org git checkout -f dev

Apache对.git repo有适当的权限,Apache对$PATH中的git有权限,Apache对/var/www/html/www.websitename.org有所有权和权限

我在这种情况下检查dev,因为我在BitBucket中的Web Hook设置为在功能分支合并到dev并推到BitBucket时响应。

Bitbucket Server Web Post Hooks插件调用的PHP脚本类似如下:

更新后的代码示例

    <?php
//$repo_name = $_GET['repoName'];
//$client = $_GET['client'];

//for debug
//file_put_contents('deploy.log', serialize($_POST['payload']), FILE_APPEND);
//file_put_contents('deploy.log', $_GET['client'], FILE_APPEND);
//file_put_contents('deploy.log', $_GET['repoName'], FILE_APPEND);
//file_put_contents('deploy.log', print_r($_POST), FILE_APPEND);
// Full path to git binary is required if git is not in your PHP user's path. Otherwise just use 'git'.
$git_bin_path = '/usr/bin/git';

echo getcwd() . "'n";

$update = false;

// Parse data from Bitbucket hook payload
//$payload = json_decode($_POST['payload']);
if ( isset($_POST['payload']) ) { // old method
    $payload = $_POST['payload'];
} else { // new method
    $payload = json_decode(file_get_contents('php://input'),false);
}
/*if(function_exists('exec')) {
file_put_contents('deploy.log', print_r("exec enabled",true), FILE_APPEND);
}else{
    file_put_contents('deploy.log', print_r("exec NOT enabled",true), FILE_APPEND);
}
if(function_exists('chdir')) {
file_put_contents('deploy.log', print_r("chdir enabled",true), FILE_APPEND);
}else{
    file_put_contents('deploy.log', print_r("chdir NOT enabled",true), FILE_APPEND);
}
file_put_contents('deploy.log', print_r($payload,true), FILE_APPEND);*/
//set repo name
$repo_name = $payload->repository->slug;
file_put_contents('/var/www/html/deploy/deploy.log', print_r($repo_name.":",true), FILE_APPEND);
$web_root_dir = '/var/www/html/lt/'.$repo_name;
$repo_dir = '/home/apache/'.$repo_name.'.git';

$dir = getcwd();
file_put_contents('/var/www/html/deploy/deploy.log',print_r("BEFORECHDIR:".$dir."CHDIRBEFORE'r'n"), FILE_APPEND);
file_put_contents('/var/www/html/deploy/deploy.log', print_r("repo".$repo_dir."repo'r'n",true), FILE_APPEND);
chdir($repo_dir);
file_put_contents('/var/www/html/deploy/deploy.log', print_r("DIR:repo".getcwd()."repo:DIR 'r'n",true), FILE_APPEND);
$test = chdir($repo_dir);
file_put_contents('/var/www/html/deploy/deploy.log', print_r($test,true), FILE_APPEND);
if(count($payload->refChanges) == '0'){
    $update = false;
}elseif($payload->refChanges[0]->refId == 'refs/heads/dev' ){
    $update = true;
}
file_put_contents('/var/www/html/deploy/deploy.log', print_r("This is the update:".$update.": this ends update 'r'n",true), FILE_APPEND);
if ($update) {
    file_put_contents('/var/www/html/deploy/deploy.log', print_r("It's an update",true), FILE_APPEND);
    // Do a git checkout to the web root
    $cmd1 = $git_bin_path  . ' fetch';
    $cmd2 = 'GIT_WORK_TREE=' . $web_root_dir . ' ' . $git_bin_path  . ' checkout -f ';
file_put_contents('/var/www/html/deploy/deploy.log', print_r($cmd1,true), FILE_APPEND);
file_put_contents('/var/www/html/deploy/deploy.log', print_r($cmd2,true), FILE_APPEND);
    $test = chdir($repo_dir);
    file_put_contents('/var/www/html/deploy/deploy.log', print_r($test,true), FILE_APPEND);

    exec("cd ".$repo_dir." && ". $cmd1 ." 2>&1",$out1,$outtest);
    if($outtest !==0 ){
        file_put_contents('/var/www/html/deploy/deploy.log', print_r($out1,true), FILE_APPEND);
    }
    exec("cd ".$repo_dir." && ". $cmd2 ." 2>&1",$out2,$outtest1);
    if($outtest1 !==0 ){
        file_put_contents('/var/www/html/deploy/deploy.log', print_r($out2,true), FILE_APPEND);
    }
    file_put_contents('/var/www/html/deploy/deploy.log', print_r("made it past exec",true), FILE_APPEND);
    //$test = shell_exec($cmd1);
    //$test2 = shell_exec($cmd2);
    file_put_contents('/var/www/html/deploy/deploy.log', print_r($cmd1,true), FILE_APPEND);
    file_put_contents('/var/www/html/deploy/deploy.log', print_r($cmd2,true), FILE_APPEND);

    // Log the deployment
    $commit_hash = exec('cd ' . $repo_dir . ' && ' .$git_bin_path  . ' rev-parse --short HEAD');
    file_put_contents('/var/www/html/deploy/deploy.log', date('m/d/Y h:i:s a') . " Deployed branch: " .  $branch . " Commit: " . $commit_hash . "'n", FILE_APPEND);
}
?>

我可能忽略了什么会导致PHP的exec()函数在使用exec()而不是直接从PHP用户的shell执行时以不足的特权执行?

原来是RedHat 7.2下apache用户的SELinux权限问题。

我创建了另一个用户,并添加了一个bash脚本来执行git获取和签出。

我还修改了sudoers文件,以允许apache作为新创建的用户运行bash脚本。