Envoy Task Runner
Brief introduction
Laravel Envoy provides a mini-concise syntax for the definition and execution of common tasks for remote servers. You can set tasks easily for deployment, Artisan commands, and so on, by using the Blade syntax style. Currently, Envoy only supports MAC and Linux operating systems.
Installation
First, you need to install Envoy via the Composer Global command:
Composer global require "laravel/envoy=~1.0"
You need to make sure that the ~/.composer/vendor/bin directory is added to your PATH so that you can use the envoy command directly when using the terminal.
Update Envoy
You can use Composer to maintain Envoy updates:
Composer Global Update
Writing tasks
All Envoy tasks should be defined in the Envoy.blade.php file in the root directory of your project. Here's a simple example:
@servers ([' web ' = ' user@192.168.1.1 ']) @task (' foo ', [' on ' = ' web ']) Ls-al@endtask
As you can see, the @servers directive is defined in the header of the file and contains an array that contains a list of servers. The @task directive is used to define the task, which contains a task name, and an array parameter, which contains an on key whose value is the server to be executed by the task, which should be one or more of the @servers instruction list. You should place the Bash code inside the @task directive, which is passed to the remote server to be executed when the task executes.
Local tasks
You can specify that the server is local to perform local tasks:
@servers ([' localhost ' = ' 127.0.0.1 '])
Guide
Sometimes, you might want to perform some PHP operations before performing a Envoy task. You can use @setup directives to declare variables, and you can use PHP inside them to work:
@setup $now = new DateTime (); $environment = Isset ($env)? $ENV: "Testing"; @endsetup
You can also use the @include directive to introduce arbitrary external PHP files:
@include (' vendor/autoload.php ')
Confirm Task
If you want to prompt before the remote server executes a given task, you can add the Confirm directive to your task definition:
@task (' Deploy ', [' on ' + ' web ', ' confirm ' = ' = True]) CD site Git pull origin {{$branch}} php artisan mi Grate@endtask
Task variables
If you want, you can use command-line switches to pass variables to the Envoy task, which allows you to customize your tasks:
Envoy Run Deploy--branch=master
You can use this option in your task through Blade's echo syntax:
@servers ([' web ' = ' 192.168.1.1 ']) @task (' Deploy ', [' on ' = ' web ']) CD site Git pull origin {{$branch}}
php Artisan Migrate@endtask
Multiple servers
You can easily perform tasks across multiple servers. First, you need to add additional servers to the @servers instructions. Each server should be assigned a unique name. After you add additional servers, you need to use the array on key to list the servers to be executed in the task instructions that you want to execute:
@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
By default, tasks are executed serially across the server, which means that the next server's task is performed only after the current server performs the task.
Parallel execution
If you want to perform tasks in parallel across servers. You can add the parallel option to the task command:
@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
Task macro
Task macros allow you to define a command to perform a set of tasks sequentially. For example, we define a deploy macro to perform 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 you have defined a macro, you can run multiple tasks with one command:
Envoy Run Deploy
Running tasks
You need to use the Envoy Run command to perform the tasks defined in the Envoy.blade.php file. You can pass the name of a task or macro name into the command. Envoy executes the task and displays the output that the server executes synchronously:
Envoy Run Task
Notice
Hipchat
You can use @hipchat instructions to send a message to the team's Hipchat room after the task has completed. This command receives an API token, the name of the room, and the user name of the sender as shown in the message:
@servers ([' web ' = ' 192.168.1.1 ']) @task (' foo ', [' on ' + ' web ']) ls-al@endtask@after @hipchat (' token ', ' Guest ', ' Envoy ') @endafter
If you need to, you can also send a custom message to the Hipchat room. When building a message, the variables available to the task are also available in the message:
@after @hipchat (' token ', ' guest ', ' Envoy ', ' $task ran in the $env environment. ') @endafter
Slack
In addition to Hipchat, Envoy also supports sending notifications to Slack. @slack instructions receive a slack hook URL, a channel name, and the message content you need to send:
@after @slack (' Hook ', ' channel ', ' message ') @endafter
You can get the Webhook URL by creating a Incoming WebHooks on the Slack website. The hook parameter should be a complete webhook URL, such as:
Https://hooks.slack.com/services/ZZZZZZZZZ/YYYYYYYYY/XXXXXXXXXXXXXXX
You can provide one of the following parameters as a channel:
- #channel send notifications to the channel
- @user Send notifications to users