How to Deploy Laravel Application on Dev and Production Based on Different Branches With Laravel Envoy
So my typical workflow when working on a client's application is to have a "dev" environment on my server, and the production environment is sometimes also on my server.
I push my code on the branch "develop" of the project's git repository, and the code is automatically deployed on the dev environment. Then I merge the branch in "master" and it automatically deploys on the production environment.
Pretty usual stuff. The only different between the "dev" and the "production" environment is the directory path where the application is hosted.
On my server I host the production app in /var/www/html/my-laravel-app
and the dev version is under /var/www/html/dev/my-laravel-app
In my Continuous Integration / Continuous Delivery pipelines, I use Laravel Envoy and in order to trigger the deployment you need to run envoy run deploy
.
Now I need to run this command but I also need to specify on which environment I want the deployment to happen.
Here are the commands:
Deploy on production : envoy run deploy
Deploy on dev : envoy run deploy --env=dev
And here is my Envoy.blade.php file:
@servers(['web' => ['[email protected]']])
@setup
$env = isset($env) ? $env : 'production';
if ($env === 'dev') {
$dir = '/var/www/html/dev/my-laravel-app';
} else if ($env === 'production') {
$dir = '/var/www/html/my-laravel-app';
}
@endsetup
@story('deploy')
pull_changes
run_composer
migrate
clear_cache
build_assets
@endstory
@task('pull_changes')
cd {{ $dir }}
echo 'Retrieve Changes'
git reset --hard HEAD;git clean -df
git pull
@endtask
@task('run_composer')
cd {{ $dir }}
echo "Starting deployment"
composer install
@endtask
@task('migrate')
cd {{ $dir }}
echo "Migrating"
php artisan migrate --force
@endtask
@task('clear_cache')
cd {{ $dir }}
echo "Clear Cache"
php artisan cache:clear
echo "Restart Queue"
php artisan queue:restart
@endtask
@task('build_assets')
cd {{ $dir }}
echo "Build Assets"
yarn
npm run prod
@endtask
Also you'll note I don't implement a Zero Deployment strategy, but you could easily.
The important thing here is to note the $env variable that only defines the directory where we are going to run the deployment commands.
I consider myself as an IT Business Artisan. Or Consultant CTO. I'm a self-taught Web Developper, coach and teacher. My main work is helping and guiding digital startups.
more about meBTC
bc1qgw9a8hyqqwvcls9ln7vhql4mad0qkveutr2td7
ETH
0x3A720717Da03dB6f3c4a4Ff08BC64100205A79d0
2025 © My Dynamic Production SRL All rights Reserved.