Laravel basic tutorial-Introduction to the SSH task Envoy task runner
Laravel Envoy provides a mini-concise syntax for defining and executing common tasks on the remote server. You can use the Blade syntax to easily set tasks for deployment and Artisan commands. Currently, Envoy only supports Mac and Linux operating systems.
Install
First, you need to install Envoy through the global command of Composer:
composer global require "laravel/envoy=~1.0"
You need to make sure ~ The/. composer/vendor/bin directory is added to your PATH so that you can directly use the envoy command when using the terminal.
Update Envoy
You can use Composer to maintain the update of Envoy:
composer global update
Compile a task
All Envoy tasks should be defined in the Envoy. blade. php file under the root directory of your project. Here is a simple example:
@servers(['web' => 'user@192.168.1.1'])@task('foo', ['on' => 'web']) ls -al@endtask
As you can see, the @ servers command is defined in the file header and contains an array containing the server list. @ Task command is used to define a task. it contains a task name and an array parameter. the array contains an on key. its value is the server on which the task is to be executed, it should be one or more in the @ servers command list. You should place Bash code inside the @ task command, which will be passed to the remote server to be executed during task execution.
Local task
You can specify a local server to execute local tasks:
@servers(['localhost' => '127.0.0.1'])
Guide
Sometimes, you may want to execute some PHP operations before executing the Envoy task. You can use the @ setup command to declare variables, and you can use PHP to work in it:
@setup $now = new DateTime(); $environment = isset($env) ? $env : "testing";@endsetup
You can also use the @ include command to introduce arbitrary external PHP files:
@include('vendor/autoload.php')
Confirm task
If you want to prompt before the remote server executes the given task, you can add the confirm command when defining your task:
@task('deploy', ['on' => 'web', 'confirm' => true]) cd site git pull origin {{ $branch }} php artisan migrate@endtask
Task variable
If necessary, you can use the command line switch to pass variables to the Envoy task, which allows you to customize your task:
envoy run deploy --branch=master
You can use this option in your task through the echo syntax of Blade:
@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 execute tasks across multiple servers. First, you need to add additional servers in the @ servers command. Each server should be assigned a unique name. After adding an additional server, you need to use the array on key in the task command to list the servers to be executed:
@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, the task is executed serially between servers, which means that the task of the next Server will be executed only after the task is completed on the current server.
Parallel execution
If you want to execute tasks in parallel across servers. You can add the parallel option in 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
A task macro allows you to define a command to execute a group of tasks in sequence. For example, we define a deploy macro to execute 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 is defined, you can run multiple tasks using one command:
envoy run deploy
Run the task
You need to use the run command of Envoy to execute the task defined in the Envoy. blade. php file. You can pass a task name or macro name to the command. Envoy executes the task and synchronously displays the output of the server execution:
envoy run task
Notification HipChat
You can use the @ hipchat command to send a message to the HipChat room of the team after the task is executed. This command receives an API token, the name of the room, and the user name of the sender displayed in the message:
@servers(['web' => '192.168.1.1'])@task('foo', ['on' => 'web']) ls -al@endtask@after @hipchat('token', 'room', 'Envoy')@endafter
If necessary, you can also send custom messages to the HipChat room. When building a message, the available variables of the task are also available in 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. @ Slack command receives a Slack hook URL, a channel name, and the message content you want to send:
@after @slack('hook', 'channel', 'message')@endafter
You can create an Incoming WebHooks on the Slack website to obtain the webhook URL. The hook parameter should be a complete webhook URL, for example:
https://hooks.slack.com/services/ZZZZZZZZZ/YYYYYYYYY/XXXXXXXXXXXXXXX
You can provide one of the following Channel parameters:
- # Send a notification to a channel through a channel
- @ User send a notification to the user