How to Deploy Laravel Application on Dev and Production Based on Different Branches With Laravel Envoy

profile picture

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]']])

    $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';


    cd {{ $dir }}
    echo 'Retrieve Changes'
    git reset --hard HEAD;git clean -df
    git pull

    cd {{ $dir }}
    echo "Starting deployment"
    composer install

    cd {{ $dir }}
    echo "Migrating"
    php artisan migrate --force

    cd {{ $dir }}
    echo "Clear Cache"
    php artisan cache:clear
    echo "Restart Queue"
    php artisan queue:restart

    cd {{ $dir }}
    echo "Build Assets"
    npm run prod

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.


about me

profile picture

I consider myself as an IT Business Artisan. Or Consultant CTO if you prefer. I'm a self-taught Web Developper, coach and teacher. My main work is helping and guiding digital startups.

more about me

follow me


A weekly email with the latests articles

2020 My Dynamic Production SPRL All rights Reserved.