Ejecutador de tareas Envoy

Introducción

Laravel Envoy proporciona una sintaxis limpia y mínimalista para la definición de tareas comunes que se ejecuta en los servidores remotos. Utilizando la sintaxis Blade, puedes configurar tareas para puesta en producción, comandos Artisan y más. Currently, Envoy only supports the Mac and Linux operating systems.

Instalación

En primer lugar, instala Envoy utilizando el comando global de Composer:

composer global require "laravel/envoy=~1.0"

Asegúrate de colocar el directorio ~/.composer/vendor/bin en tu PATH de forma que el ejecutable envoy sea encontrado cuando ejecutes el comando envoy en tu terminal.

Actualizar Envoy

You may also use Composer to keep your Envoy installation up to date:

composer global update

Writing Tasks

All of your Envoy tasks should be defined in an Envoy.blade.php file in the root of your project. Here's an example to get you started:

@servers(['web' => 'user@192.168.1.1'])

@task('foo', ['on' => 'web'])
    ls -la
@endtask

As you can see, an array of @servers is defined at the top of the file, allowing you to reference these servers in the on option of your task declarations. Within your @task declarations, you should place the Bash code that will be run on your server when the task is executed.

Bootstrapping

Sometimes, you may need to execute some PHP code before evaluating your Envoy tasks. You may use the @setup como directiva para declarar variables y realizar todo el trabajo PHP dentro del archivo Envoy:

@setup
    $now = new DateTime();

    $environment = isset($env) ? $env : "testing";
@endsetup

También puedes utilizar @include to include any outside PHP files:

@include('vendor/autoload.php');

Confirming Tasks

If you would like to be prompted for confirmation before running a given task on your servers, you may add the confirm directive to your task declaration:

@task('deploy', ['on' => 'web', 'confirm' => true])
    cd site
    git pull origin {{ $branch }}
    php artisan migrate
@endtask

Task Variables

If needed, you may pass variables into the Envoy file using command line switches, allowing you to customize your tasks:

envoy run deploy --branch=master

You may use the options in your tasks via Blade's "echo" syntax:

@servers(['web' => '192.168.1.1'])

@task('deploy', ['on' => 'web'])
    cd site
    git pull origin {{ $branch }}
    php artisan migrate
@endtask

Múltiples servidores

You may easily run a task across multiple servers. First, add additional servers to your @servers declaration. Each server should be assigned a unique name. Once you have defined your additional servers, simply list the servers in the task declaration's on array:

@servers(['web-1' => '192.168.1.1', 'web-2' => '192.168.1.2'])

@task('deploy', ['on' => ['web-1', 'web-2']])
    cd site
    git pull origin {{ $branch }}
    php artisan migrate
@endtask

Por defecto, la tarea será ejecutada en cada servidor en serie. Lo que significa, que una tarea terminará de ser ejecutada en el primer servidor antes de proceder a ejecutarse en el siguiente.

Ejecución en paralelo

If you would like to run a task across multiple servers in parallel, add the parallel option to your task declaration:

@servers(['web-1' => '192.168.1.1', 'web-2' => '192.168.1.2'])

@task('deploy', ['on' => ['web-1', 'web-2'], 'parallel' => true])
    cd site
    git pull origin {{ $branch }}
    php artisan migrate
@endtask

Macros de tareas

Macros allow you to define a set of tasks to be run in sequence using a single command. For instance, a deploy macro may run the git and composer tasks:

@servers(['web' => '192.168.1.1'])

@macro('deploy')
    git
    composer
@endmacro

@task('git')
    git pull origin master
@endtask

@task('composer')
    composer install
@endtask

Once the macro has been defined, you may run it via single, simple command:

envoy run deploy

Ejecutar tareas

To run a task from your Envoy.blade.php file, execute Envoy's run command, passing the command the name of the task or macro you would like to execute. Envoy will run the task and display the output from the servers as the task is running:

envoy run task

Notificaciones

HipChat

After running a task, you may send a notification to your team's HipChat room using Envoy's @hipchat directive. The directive accepts an API token, the name of the room, and the username to be displayed as the sender of the message:

@servers(['web' => '192.168.1.1'])

@task('foo', ['on' => 'web'])
    ls -la
@endtask

@after
    @hipchat('token', 'room', 'Envoy')
@endafter

If you wish, you may also pas a custom message to send to the HipChat room. Any variables available to your Envoy tasks will also be available when constructing the message:

@after
    @hipchat('token', 'room', 'Envoy', "{$task} ran in the {$env} environment.")
@endafter

Slack

In addition to HipChat, Envoy also supports sending notifications to Slack. The @slack directive accepts a Slack hook URL, a channel name, and the message you wish to send to the channel:

@after
    @slack('hook', 'channel', 'message')
@endafter

Puedes obtener la URL de tu webhook creando una integración en el sitio web de Slack IncomingWebhooks. El argumento hook debe incluir la URL completa del webhook proporcionada por el Incoming Webhook Slack Integration. Por ejemplo:

https://hooks.slack.com/services/ZZZZZZZZZ/YYYYYYYYY/XXXXXXXXXXXXXXX

You may provide one of the following as the channel argument:

  • Para enviar la notificación a un canal: #channel
  • Para enviar la notificación a un usuario: @user