[Laravel5.2 documentation] service -- EnvoyTaskRunner
1. Introduction
Laravel Envoy provides a clean and simplified syntax for defining common tasks running on a remote host. With the Blade style syntax, you can easily configure tasks, Artisan commands, and more for development. Currently, Envoy only supports Mac and Linux operating systems.
1.1 Installation
First, use Composer's global command to install Envoy:
composer global require "laravel/envoy=~1.0"
Ensure ~ /. The composer/vendor/bin directory is in the system PATH. Otherwise, the command cannot be executed because envoy cannot be found in the terminal.
Update Envoy
You can also use Composer to keep the latest version of Envoy installed:
composer global update
2. write a task
All Envoy tasks are defined in the Envoy. blade. php file under the Project root directory. The following is an example for you to start:
@servers(['web' => 'user@192.168.1.1'])@task('foo', ['on' => 'web']) ls -la@endtask
As you can see, the @ servers array is defined at the top of the file, allowing you to reference these servers using the on option in the task declaration. in the @ task declaration, place the Bash code to be run on the server.
Start
Sometimes, you need to execute some PHP code before evaluating the Envoy task. you can use the @ setup command in the Envoy file to declare variables and PHP code to be executed:
@setup $now = new DateTime(); $environment = isset($env) ? $env : "testing";@endsetup
You can also use @ include to introduce external php files:
@include('vendor/autoload.php');
Confirm task
If you want to display a confirmation prompt before running a given task on the server, you can use the confirm command in the task declaration:
@task('deploy', ['on' => 'web', 'confirm' => true]) cd site git pull origin {{ $branch }} php artisan migrate@endtask
2.1 Task variables
If necessary, you can use the command line switch to pass variables to the Envoy file to allow you to customize tasks:
envoy run deploy --branch=master
You can use this option in the Blade "echo" syntax in the task:
@servers(['web' => '192.168.1.1'])@task('deploy', ['on' => 'web']) cd site git pull origin {{ $branch }} php artisan migrate@endtask
More than 2.2 Servers
You can easily run the same task on multiple hosts. First, add an additional server to the @ servers Declaration. each server should be assigned a unique name. After the server is defined, simply list all servers in the task declaration:
@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
By default, the task is executed on each server in sequence, which means that the task starts to run on the second server after it is completed on the first server.
Parallel operation
If you want to run in parallel on multiple servers, add the parallel option to the 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
2.3 Task macro
Macros allow you to define multiple tasks in sequence using a single command. For example, the deploy macro runs 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
After a macro is defined, you can run it using the following command:
envoy run deploy
3. run the task
To run a task from the Envoy. blade. php file, run the Envoy run command and then pass the command name or macro of the task you want to execute. Envoy will run the command and print the output from the service:
envoy run task
4. notifications4.1 HipChat
After running a task, you can use the @ hipchat command of Envoy to send a notification to the HipChat room of the team, which receives an API token, room name, and user name:
@servers(['web' => '192.168.1.1'])@task('foo', ['on' => 'web']) ls -la@endtask@after @hipchat('token', 'room', 'Envoy')@endafter
If needed, you can also transmit messages that are customized and sent to the HipChat room. all valid variables in the Envoy task are also valid when building messages:
@after @hipchat('token', 'room', 'Envoy', "{$task} ran in the {$env} environment.")@endafter
4.2 Slack
In addition to HipChat, Envoy also supports sending notifications to Slack. @ Slack command receives a Slack hook URL, channel name, and the message you want to send to the channel:
@after @slack('hook', 'channel', 'message')@endafter
You can obtain the hook URL by creating Incoming WebHooks integrated into the Slack website. The hook parameter is a complete webhook URL provided by Incoming Webhooks Slack integration. for example:
https://hooks.slack.com/services/ZZZZZZZZZ/YYYYYYYYY/XXXXXXXXXXXXXXX
You can provide one of the following two channel parameters:
- Send a message to the channel: # channel
- Send message to user: @ user