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

We have a Laravel deployment website set up under deploy.mysite.com which handles deployments for a multitude of other websites.

One other website I'm trying to deploy which is also a Laravel site resides under 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. 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.

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!

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.

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.

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;

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

This was literally 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. 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:

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

That will run the artisan command for the provided directory.

$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");