2017-10-28 76 views
2

我试图在Laravel中构建一个简单的部署器,它将接收webhook,执行部署并存储关于部署的一些信息(从挂钩)。

我使用Laravel 5.2作业调度程序来执行以下操作:

public function deploy() 
{ 
    $result = 0; 
    $output = array(); 
    exec('cd ' . $this->deploymentMapping->full_file_path . '; git pull', $output, $result); 
    return array(
     'result' => $result, 
     'output' => $this->outputToString($output) 
    ); 
} 

我的exec()功能的理解是,这不应该直接倾倒任何错误,但是将它们存储在$output

但是,当我在命令行中运行我服务器上的php artisan queue:work时,为了测试作业调度程序,我立即将fatal: Not a git repository (or any of the parent directories): .git转储到命令行输出中。 这个混帐错误是正确的,但它使作业“失败”,就好像exec()抛出一个错误。它是否正确?我的工作应该是报告以自己的方式错误,如下所示:

public function handle() 
{ 
    $deploymentAttempt = new DeploymentAttempt(); 
    $deploymentAttempt->deployment_id = $this->deploymentID; 
    $gitResponse = $this->deployer->deploy(); //the above function 
    $deploymentAttempt->success = !($gitResponse['result'] > 0); 
    $deploymentAttempt->message = $gitResponse['output']; 
    $deploymentAttempt->save(); 
} 

回答

1

这是因为PHP的exec不提供一个简单的方法来分别捕捉stderr输出。您也需要捕获stderr

重定向stderrstdout应该做的伎俩。将2>&1附加到命令的末尾。

exec('cd ' . $this->deploymentMapping->full_file_path . '; git pull 2>&1', $output, $result); 

它将填充阵列$output与预期的输出,每阵列键一行。

要了解更多有关如何2>&1运作的,你可以按照this thread.

+0

谢谢!导致作业失败的错误实际上不是StdOut/StdErr的这个问题,它实际上是我编写'$ deploymentAttempt-> success'而不是'$ deploymentAttempt-> successful'。我是丁丁。谢谢你的帮助 :) – k4kuz0