我正在尝试在 Laravel 中构建一个简单的部署程序,它将接收一个 webhook,执行部署并存储一些关于所述部署的信息(来自 Hook )。
我正在使用 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父目录):.git
转储到命令行输出中。 这个 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();
}
最佳答案
这是因为 PHP 的 exec
没有提供单独捕获 stderr
输出的简单方法。
您还需要捕获 stderr
。
将 stderr
重定向到 stdout
应该可以解决问题。将 2>&1
附加到命令的末尾。
exec('cd ' . $this->deploymentMapping->full_file_path . '; git pull 2>&1', $output, $result);
它将用预期的输出填充数组$output
,每个数组键一行。
要了解更多关于 2>&1
的工作原理,您可以关注 this thread.
关于php - Laravel 作业将 exec() 错误转储到命令行而不是 $output,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46993733/