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?
Advertisement
Answer
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
.
Note: This will only work on versions below V5.
$dotenv = Dotenv::create(__DIR__); $dotenv->overload();
UPDATE for V5
As per @nibnut’s comment, since dotenv V5 (now in Laravel 7.0) this loads the environment variables as immutable. 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
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. This could be used for any project by installing it separately.