Introduce
Hello everyone, maybe everyone does not know, laravel has a tool to deploy directly to the server 1 extremely fast and convenient that is Envoy.
Envoy has a very clean, well-written way to identify common tasks you’ll run on servers. Using Blade-style syntax, you can easily set up tasks to deploy using Artisan commands with great ease.
Note: Laravel Envoy currently only supports Mac and Linux operating systems.
Setting
First, you need to install Envoy with the Composer command:
1 2 | composer global require laravel/envoy |
Here I use global to install, ie it will only need to install 1 time, the next time do not need to install anymore.
Declaring the task
All your Envoy tasks must be specified in the Envoy.blade.php file in the project’s root directory:
1 2 3 4 5 6 7 | @servers(['web' => [' <a class="__cf_email__" href="/cdn-cgi/l/email-protection" data-cfemail="a4d1d7c1d6e4959d968a95929c8a958a95">[email protected]</a> ']]) @task('foo', ['on' => 'web']) ls -la @endtask |
Above I have used 1 @servers
membrane has been defined at the top of the file, this allows you to refer to these servers in the task notification option.
In @task
declarations, you can write the commands needed to execute on the server.
If you want to execute commands under localhost, then you specify the server’s IP address is 127.0.0.1:
1 2 | @servers(['localhost' => '127.0.0.1']) |
Use setup
Sometimes, you may want to execute some PHP code before performing Envoy tasks. You can use the @setup
command to declare variables and run PHP before any other envoy tasks are executed:
1 2 3 4 5 6 | @setup $now = new DateTime(); $environment = isset($env) ? $env : "testing"; @endsetup |
If you need to require other PHP files before the envoy task is executed, you can use the @include
command like the example below:
1 2 3 4 5 6 7 | @include('vendor/autoload.php') @task('foo') # ... @endtask |
Use variable
If needed, you can pass custom values into Envoy tasks with the command:
1 2 3 | envoy run deploy --branch=master |
You can access options in your task through the “echo” syntax of Blade. You can also use if statement and loop in your task.
For example:
1 2 3 4 5 6 7 8 9 10 | @servers(['web' => '192.168.1.1']) @task('deploy', ['on' => 'web']) cd site @if ($branch) git pull origin {{$branch}} @endif php artisan migrate @endtask |
The code above I checked the branch variable passed first, if there is a branch, it will execute pull code from the branch. Easy right?
Use story
When you have multiple tasks that you want to combine to run once, use the story
, which will group a group of tasks under a single name, this allows you to group small tasks, focusing on large tasks.
For example, a deployment list can run git and composer tasks by listing task names in its story
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @servers(['web' => '192.168.1.1']) @story('deploy') git composer @endstory @task('git') git pull origin master @endtask @task('composer') composer install @endtask |
How to run the story as usual:
1 2 3 | envoy run deploy |
Multiple Servers
Envoy will allow you to easily run a task on multiple servers.
1 2 3 4 5 6 7 | @servers(['web-1' => '192.168.1.1', 'web-2' => '192.168.1.2']) //Khai báo multi servers @task('deploy', ['on' => ['web-1', 'web-2']]) //Thực thi trên nhiều server cd site git pull origin {{ $branch }} php artisan migrate @endtask |
Parallel Execution
By default, tasks are executed on each server. This means that a task will have to be completed running on the first server before proceeding on the second server. If you want to run a task on multiple servers in parallel, you need to add the parallel
option to your task
declaration:
1 2 3 4 5 6 7 8 9 | @servers(['web-1' => '192.168.1.1', 'web-2' => '192.168.1.2']) @task('deploy', ['on' => ['web-1', 'web-2'], 'parallel' => true]) // This is line cd site git pull origin {{ $branch }} php artisan migrate @endtask |
Confirming Task Execution
A very important function while deploying is to confirm
, confirm the action first and then execute.
This option is useful for me to confirm that you really want to perform this task:
1 2 3 4 5 6 7 | @task('deploy', ['on' => 'web', 'confirm' => true]) cd site git pull origin {{ $branch }} php artisan migrate @endtask |
Notifications
Slack
The perspective is set when your project has many parties such as app, api, backend, frontend. There will be times when you need to notify a certain service about the deployment results for the parties to know.
Envoy assisted in sending notifications to Slack after each task was executed. The @slack
command will accept the Slack hook URL and channel
name.
You can retrieve your webhook URL by creating the “WebHooks” integration in your Slack dashboard. You must pass the entire webhook URL into the @slack
directive:
1 2 3 4 5 6 | @finished @slack('webhook-url', '#channel') //Để gửi thông báo tới một kênh @slack('webhook-url', '@user') //Để gửi thông báo cho người dùng @endfinished |
References
https://laravel.com/docs/6.x/envoy
Thanks for reading !.