简体   繁体   English

将 php artisan 从一个 Laravel 安装运行到另一个

[英]Run php artisan from one Laravel install to another

We have a Laravel deployment website set up under deploy.mysite.com which handles deployments for a multitude of other websites.我们有一个Laravel部署网站下deploy.mysite.com成立了负责处理部署的其他网站,众说纷纭。

One other website I'm trying to deploy which is also a Laravel site resides under site2.myothersite.com .我正在尝试部署的另一个网站也是 Laravel 网站,它位于site2.myothersite.com下。

Both are on the same server.两者都在同一台服务器上。 Deploy calls a script on site2, this deploy script runs various commands after cd 'ing to the project directory. Deploy 调用site2 上的一个脚本,这个deploy 脚本在cd到项目目录后运行各种命令。 We use the following to update the database structure.我们使用以下内容来更新数据库结构。

php artisan migrate --force

Ordinarily when this is run directly via SSH when in the project root, it runs just fine.通常,当它在项目根目录中直接通过 SSH 运行时,它运行得很好。

However when this is run via the deployment script (using php exec() to run these commands) the process does work - however, instead of updating the project that we've cd 'd into, it updates the database structure of the deployment site!但是,当这是通过部署脚本运行时(使用 php exec()运行这些命令),该过程确实有效 - 但是,它不是更新我们已经cd进入的项目,而是更新部署站点的数据库结构!

It seems as if the php artisan migrate command ignores the fact I've cd 'd into another project and it takes the database values from the current directory.似乎php artisan migrate命令忽略了我已经cd进入另一个项目的事实,它从当前目录中获取数据库值。

How can I go about changing this behaviour?我怎样才能改变这种行为?

After playing around with multiple different solutions I eventually came to realise that the problem was the .env file from the project that I was in was setting the environment variables and then weren't being overwritten by anything therefore was then essentially running the code as the wrong site.在尝试了多种不同的解决方案后,我最终意识到问题在于我所在项目中的.env文件正在设置环境变量,然后没有被任何东西覆盖,因此本质上是运行代码作为错误的网站。

What I did to solve this我做了什么来解决这个问题

In my deploy script I manually loaded up the .env file and overwrote the environment variables with overload rather than just load .在我的部署脚本中,我手动加载了.env文件并用overload而不是仅仅load覆盖了环境变量。 Note: This will only work on versions below V5.注意:这仅适用于 V5 以下的版本。

$dotenv = Dotenv::create(__DIR__);
$dotenv->overload();

UPDATE for V5 V5 更新

As per @nibnut's comment , since dotenv V5 (now in Laravel 7.0) this loads the environment variables as immutable.根据@nibnut 的评论,由于 dotenv V5(现在在 Laravel 7.0 中)这会将环境变量加载为不可变的。 In my case I'm happy to allow these to be mutable, in which case I can do the following instead of the above.在我的情况下,我很高兴允许这些是可变的,在这种情况下,我可以执行以下操作而不是上述操作。

$dotenv = Dotenv::createMutable(__DIR__);
$dotenv->load();

https://github.com/vlucas/phpdotenv#immutability-and-repository-customization https://github.com/vlucas/phpdotenv#immutability-and-repository-customization

This was all I had to do to get my original script working.这就是让我的原始脚本工作所需要做的全部工作。

NOTE: as this is a laravel install at all ends, the dotenv package was already installed.注意:由于这是一个 Laravel 安装,因此dotenv包已经安装。 This could be used for any project by installing it separately.这可以通过单独安装用于任何项目。

Rather than cd to the directory, you could change the command to something similar to this:您可以将命令更改为类似于以下内容的内容,而不是cd到目录:

php /var/www/sitea/artisan migrate --force

That will run the artisan command for the provided directory.这将为提供的目录运行 artisan 命令。

$log = shell_exec("unset DB_HOST &&
       unset DB_PORT && 
       unset DB_PASSWORD && 
       unset DB_USERNAME && 
       unset DB_DATABASE && 
       composer dump-autoload &&
       cd /var/www/yourproject/ && php /var/www/yourproject/artisan migrate:fresh &&
       php /var/www/yourproject/artisan db:seed &&
       php /var/www/yourproject/artisan seed:translation");

novocaine's answer unfortunately no longer works in Laravel 7.x, since the DotEnv file is loaded immutable.不幸的是,novocaine 的答案在 Laravel 7.x 中不再有效,因为 DotEnv 文件加载是不可变的。

My current workaround is to create a temporary .env file in my project (ie ".env.temp") then use the --env argument when calling my artisan commands:我目前的解决方法是在我的项目中创建一个临时 .env 文件(即“.env.temp”),然后在调用我的 artisan 命令时使用 --env 参数:

php artisan migrate --env=temp

This may not work for all use cases, but if you're calling artisan commands in project B from within project A, it should work fine.这可能不适用于所有用例,但如果您从项目 A 中调用项目 B 中的工匠命令,它应该可以正常工作。

novocaine's answer involve reloading .env file. novocaine 的回答涉及重新加载 .env 文件。 But you can also empty environment variables with env -i .但是您也可以使用env -i清空环境变量。

This method will avoid having some variables not overwritten due to different .env file variable name.此方法将避免由于.env文件变量名称不同而导致某些变量未被覆盖。 For example, your deployment site may have a variable called DEPLOY_KEY in the .env that is non-existent on the .env of site2.例如,您的部署位置可能有一个变量叫DEPLOY_KEY.env是在不存在.env SITE2的。 Reloading .env file may leave the DEPLOY_KEY variable available on the site2 script.重新加载.env文件可能会使DEPLOY_KEY变量在 site2 脚本中可用。 This may leads into security issues.这可能会导致安全问题。

So, instead of running所以,而不是运行

exec('php artisan migrate --force');

you'll have just to add env -i :你只需要添加env -i

exec('env -i php artisan migrate --force');

Source: https://unix.stackexchange.com/a/48995/375005来源: https : //unix.stackexchange.com/a/48995/375005

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM