由于我的应用程序是通过Laravel Vapor部署的,我很难找出为什么我的事件没有在前端(Laravel Echo+Pusher("触发",即使它在本地运行得很好。
Pusher调试控制台实际上显示了事件实际上是由Laravel应用程序发送到Pusher的。
由于我几乎浪费了大约半天的时间来找出问题所在(幸运的是,我看到我的消息实时显示在我的本地环境中,而不是在后台发布内容后进行后台发布(,我想我会再花10分钟在这里写一篇帖子,这样一些人(希望(就不需要浪费那么多时间了。
问题实际上如下:
- Vapor首先在
.vapor
目录中本地构建应用程序 - 在这个构建过程中加载的
.env
中的PUSHER密钥是本地.env
(!(中的密钥,这意味着如果在.env.{environment}
中设置MIX_PUSHER_APP_KEY
和MIX_PUSHER_APP_CLUSTER
环境变量(这是从运行vapor env:pull {environment}
中获得的(,它不会更改任何内容
我用一种快速(肮脏(的方式解决了这个问题:
- 添加更多环境变量:
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=eu
# Necessary in `php artisan pusher:credentials`
PUSHER_LOCAL_APP_KEY=
PUSHER_LOCAL_APP_CLUSTER=eu
# Necessary in `php artisan pusher:credentials`
PUSHER_STAGING_APP_KEY=
PUSHER_STAGING_APP_CLUSTER=eu
# Necessary in `php artisan pusher:credentials`
PUSHER_PRODUCTION_APP_KEY=
PUSHER_PRODUCTION_APP_CLUSTER=eu
MIX_PUSHER_APP_KEY="${PUSHER_LOCAL_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_LOCAL_APP_CLUSTER}"
- 向Vapor构建过程添加一个新命令(在
yarn run production
之前(:php artisan pusher:credentials {environment}
。因此,对于分段,您将使用php artisan pusher:credentials staging
命令如下所示:
<?php
namespace AppConsoleCommands;
use IlluminateConsoleCommand;
use IlluminateConsoleConfirmableTrait;
use IlluminateSupportStr;
final class SwapPusherCredentials extends Command
{
use ConfirmableTrait;
private array $keyPatterns = [
'PUSHER_%sAPP_KEY',
'PUSHER_%sAPP_CLUSTER',
];
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'pusher:credentials {environment=production}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Sets the pusher credentials based on the current environment';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @throws Exception
*/
public function handle()
{
// Basically we're fixing a bug (?) in Vapor. Since we're building the front end locally,
// we need to swap the pusher keys before building. If we don't do this, the front end
// will be built with local pusher keys. When posting messages not local it's broken
$environment = Str::upper($this->argument('environment'));
$this->updatePusherEnvironmentVariables($environment);
}
/**
* @param string $environment
* @throws Exception
*/
private function updatePusherEnvironmentVariables(string $environment)
{
foreach ($this->keyPatterns as $pattern) {
// 'PUSHER_LOCAL_APP_KEY' || 'PUSHER_STAGING_APP_KEY' etc.
$variableToSet = sprintf($pattern, $environment . '_');
// 'PUSHER_APP_KEY'
$targetVariableName = sprintf($pattern, '');
if (!env($targetVariableName, false)) {
throw new Exception('Missing environment value for ' . $targetVariableName);
}
$this->setVariable($targetVariableName, $variableToSet);
}
}
private function setVariable(string $targetVariableName, string $variableToSet)
{
file_put_contents($this->laravel->environmentFilePath(), preg_replace(
$this->replacementPattern($targetVariableName),
$replacement = '${' . $variableToSet . '}',
file_get_contents($this->laravel->environmentFilePath())
));
$this->info("Successfully set MIX_{$targetVariableName} to {$replacement}!");
}
private function replacementPattern(string $targetVariableName)
{
// Don't remove notsurebutfixes, when removed it doesn't match the first entry
// So it will match all keys except PUSHER_LOCAL_* for some reason.
$escaped = '=notsurebutfixes|${' . $this->insertEnvironmentIntoKey('LOCAL',
$targetVariableName) . '}|${' . $this->insertEnvironmentIntoKey('STAGING',
$targetVariableName) . '}|${' . $this->insertEnvironmentIntoKey('PRODUCTION',
$targetVariableName) . '}';
return "/^MIX_$targetVariableName{$escaped}/m";
}
// Insert the environment after PUSHER_ in any PUSHER_* like variable passed in.
// So basically PUSHER_APP_KEY => PUSHER_LOCAL_APP_KEY
private function insertEnvironmentIntoKey($environment, $key)
{
$parts = explode('_', $key, 2);
return $parts[0] . '_' . $environment . '_' . $parts[1];
}
}
就是这样。现在.vapor
目录中的.env
文件将在部署过程中更新为使用PUSHER_STAGING_APP_KEY
和PUSHER_STAGING_APP_CLUSTER
!
通过提供.env文件来解决它
当通过GitHub操作部署代码时,在构建资产时,repo中没有.env文件可供使用。当你通过本地命令部署时,它会使用你的.env.
让我解释一下:
当您通过本地命令部署时:
vapor deploy production
您会注意到它在您的项目中创建了一个.svapor目录。这将是你的项目的精确副本,用于构建和推动你的项目。.svapor文件夹还包含项目的.env文件的副本。
因此,当它构建您的资产时,例如VITE:
npm run build
env已找到并用于构建您的资产。
这将解析未填充的import.meta.env.VITE_PUSHER_APP_KEY和import.meta.eenv.VITE_PPUSHER_APP_CLUSTER。
当您使用GuthHub操作进行部署时,您需要提供.env.
以下是如何在GitHub操作部署脚本中从vapor中提取env的摘录:
- name: Pull ENV
run: vapor env:pull staging
env:
VAPOR_API_TOKEN: ${{ secrets.VAPOR_API_TOKEN }}
- name: Move ENV
run: mv .env.staging .env
上面的代码片段应该在您的vapor部署代码之前:
vapor deploy production --commit="${CI_COMMIT_ID}" --message="${CI_MESSAGE}"