Laravel Envoy to Deploy your Application to Hosting Server

Laravel Envoy is an open-source package to efficiently deploy your apps to the server or even run tasks in the local environment. It is one of the most helpful tools in the Laravel ecosystem that every developer utilize every day. This tool provides a precise, minimal syntax for defining any tasks you run on your remote servers.

By making the use of Blade style syntax, you can quickly set up tasks for deployment actions, run inbuilt Artisan commands, and many more. Envoy currently has support for Mac, Linux only.

Similar to any other composer packages you install with your Laravel application, Laravel Envoy also follows the same process.

If you don’t already have it installed, then follow the below simple command.

$ composer global require laravel/envoy

After a successful installation, now your turn is to start to set up your tasks needed to deploy your application. Test your set up like below, shows the screen like below with more other information.

$ envoy -v

Laravel Envoy 2.3.1

You can initialize the Envoy.blade.php file by using the command below.

$ envoy init [email protected]

Adding Tasks

Before starting to write any tasks, define a new file Envoy.blade.php in the root of your project directory.

@servers(['web' => '[email protected]'])

@task('deploy')
    cd /path/to/site
    git pull origin master
@endtask

As you can see above, the @servers directive in a single line, referencing the array of servers to point around when you deploy the application.

The @task directive keeps the bash script to run on your server in the deployment process. For the initialization of variables necessary for the deployment script, you can also use the @setup directive to assign variables based on your application structure.

If your project logic requires to verify certain things, and that requires to include any PHP files in between the deployment process, you could always use the @include directive at the top of your Envoy.blade.php file.

@include('vendor/autoload.php')

@setup
    $branch = isset($branch) ? $branch : "main";
    $env = isset($env) ? $env : "production";
    $account = isset($account) ? $account : "sujipthapa";
@endsetup

@servers(['local' => '127.0.0.1', 'production' => '192.168.1.1'])

Define Variables

As per your requirement, Laravel Envoy also supports passing variables with values to your tasks within the command line.

$ envoy run deploy --branch=main --account=sujipthapa

Adding Stories

Adding @stories directive helps you to group up your small tasks into a flow of actions, which gives you a way to maintain the sequence to execute scripts on your server in your deployment process.

For instance, you may add a story label as a post-deploy, which groups your tasks like git pull, composer install, migration, etc.

Notify

You may instantly send a notification to different channels like Telegram, Discord, Slack right inside from the Envoy setup file.

It just requires to define your simple setup for that process like below.

@finished
    @slack('slack-webhoo-url', '#deploy')
@endfinished

Overview

As from the above in-depth run down, below can you can try to understand the Envoy set up with few useful directives and usual tasks defined for your to as quick overview.

@setup
    $branch = isset($branch) ? $branch : "main";
    $env = isset($env) ? $env : "production";
    $account = isset($account) ? $account : "sujipthapa";
@endsetup

@servers(['local' => '127.0.0.1', 'production' => '192.168.1.1'])

@task('cd_server_path', ['on' => 'production'])

    echo ">> cd /home/{{ $account }}/project"

    cd /home/{{ $account }}/project

@endtask


@task('git_commands')

    echo ">> git checkout {{ $branch }} ..."

    git checkout {{ $branch }}

    echo ">> git pull origin {{ $branch }} --force ..."

    git pull origin {{ $branch }} --force

@endtask

@task('run_composer')

    echo ">> composer depencencies..."

    composer install --no-interaction --quiet --no-dev --prefer-dist --optimize-autoloader

@endtask

@task('run_migrate')
    php artisan migrate --env={{ $env }} --force --no-interaction
@endtask

@story('deploy')
    cd_server_path
    git_commands
    run_composer
    run_migrate
@endstory

Finally, your turn is to run the deploy command.

$ envoy run deploy

I suggest you follow the official Laravel Envoy docs as more options and new features roll out every single day, as it is an open-source project, so many people contribute and use the ecosystem.

Conclusion

Thanks for reaching up to the end of the article. Please share this article to your circle, if you loved reading it.

Follow me on Twitter for more updates or visit the blog more often to view new articles at a later date.