Laravel Envoy

What is it?

It is a tool which provides us for running and automating the common tasks in server efficiently .This may involve things like:
Tasks that should run on remote server.
Eg 1: When we want to compile a C program online, we should compile the program on the server by running a command. (“in Server’s Terminal”).

This can be clearly said as: “when we write the code in C and when we click compile button .This button compiles the code in the server by executing the compile command for C language”.
Eg  2: Things like running GIT commands etc…

Configure envoy in ubuntu:

First we need to install laravel envoy command globally so that no matter in which directory we are we can run the command.
composer global require “laravel/envoy=~1.0”
Provide Executable permission to envoy:
chmod +x ~/.composer/vendor/bin/envoy
For easier access create symbolic link
sudo ln -s ~/.composer/vendor/bin/envoy /usr/bin/envoy
Now, lets try running “envoy” from our terminal. This should give us the following response,

Laravel Envoy version 1.1.0

Usage:
command [options] [arguments]

Options:
    -h, –help                        Display this help message
    -q, –quiet                       Do not output any message
    -V, –version                  Display this application version
    –ansi                                Force ANSI output
    –no-ansi                         Disable ANSI output
    -n, –no-interaction     Do not ask any interactive question
    -v|vv|vvv, –verbose    Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:

help     Displays help for a command
init       Create a new Envoy file in the current directory.
list        Lists commands
run       Run an Envoy task.
ssh        Connect to an Envoy server.
tasks    Lists all Envoy tasks and macros.

Creating Tasks:

To create tasks we can use Blade style syntax but Laravel Envoy doesn’t require Blade template engine, it just uses Blade syntax to define tasks. To start lets create an “Envoy.blade.php” in the root folder of our project and we will create a simple task as below

@servers([‘homestead’ => ‘vagrant@192.168.10.10’])
@task(‘list’, [‘on’ => ‘homestead’])
              ls -la
@endtask

As we can see, an array of @servers is defined at the top of the file. we can refer these servers with the “on” option of our task declarations. Within our @task declarations we should place the Bash code that will be run on our server when the task is executed.

The init command may be used to easily create a stub Envoy file:
envoy init vagrant@192.168.10.10

Running Tasks:

To run a task, we need to run  the below command for Envoy installation:
vagrant@homestead:~/apex$ envoy run list

1

Multiple Servers:

We can easily run tasks across multiple servers by adding additional servers to our @servers declaration. For each server we should assign a unique name. Once we have defined all our additional servers, simply we need to list the servers in the task declaration’s as array

@servers([‘web-1′ => ‘192.168.91.92’, ‘web-2’ => ‘192.168.91.93’])
@task(‘deploy’, [‘on’ => [‘web-1’, ‘web-2’])
      cd default
      git pull origin {{ $branch }}
      php artisan queue:listen
@endtask

We can run this by,
vagrant@homestead:~/apex$ envoy run deploy —branch=testing
By default, the task will be executed on each server serially. Meaning, the task will finish running on the first server before proceeding to execute on the next server.

Parallel Execution:

If we would like to run few tasks across multiple servers in parallel then we can add the parallel option to our task declaration as below

@servers([‘web-1′ => ‘192.168.91.92’, ‘web-2’ => ‘192.168.91.93’])
@task(‘deploy’, [‘on’ => [‘web-1’, ‘web-2’], ‘parallel’ => true])
      cd default
      git pull origin {{ $branch }}
      php artisan queue:listen
@endtask

Using Envoy, we can easily setup tasks for deployment, Artisan commands and many more. Currently, Envoy supports Mac and Linux operating systems only. Hope this has given a you a clear and easy way to do tasks.

Sridhar,
Mallow Technologies.

Leave a Reply

%d bloggers like this: