![]()
WARNING You're browsing the documentation for an upcoming version of Laravel. The documentation and features of this release are subject to change.
Laravel Envoy
Introduction
Laravel Envoy is a tool for executing common tasks you run on your remote servers. Using Blade style syntax, you can easily setup tasks for deployment, Artisan commands, and more. Currently, Envoy only supports the Mac and Linux operating systems. However, Windows support is achievable using WSL2.
Installation
First, install Envoy into your project using the Composer package manager:
composer require laravel/envoy --devOnce Envoy has been installed, the Envoy binary will be available in your application's
vendor/bindirectory:php vendor/bin/envoyWriting Tasks
Defining Tasks
Tasks are the basic building block of Envoy. Tasks define the shell commands that should execute on your remote servers when the task is invoked. For example, you might define a task that executes the
php artisan queue:restartcommand on all of your application's queue worker servers.All of your Envoy tasks should be defined in an
Envoy.blade.phpfile at the root of your application. Here's an example to get you started:@servers(['web' => ['[email protected]'], 'workers' => ['[email protected]']]) @task('restart-queues', ['on' => 'workers']) cd /home/user/example.com php artisan queue:restart @endtaskAs you can see, an array of
@serversis defined at the top of the file, allowing you to reference these servers via theonoption of your task declarations. The@serversdeclaration should always be placed on a single line. Within your@taskdeclarations, you should place the shell commands that should execute on your servers when the task is invoked.Local Tasks
You can force a script to run on your local computer by specifying the server's IP address as
127.0.0.1:@servers(['localhost' => '127.0.0.1'])Importing Envoy Tasks
Using the
@importdirective, you may import other Envoy files so their stories and tasks are added to yours. After the files have been imported, you may execute the tasks they contain as if they were defined in your own Envoy file:@import('vendor/package/Envoy.blade.php')Multiple Servers
Envoy allows you to easily run a task across multiple servers. First, add additional servers to your
@serversdeclaration. Each server should be assigned a unique name. Once you have defined your additional servers you may list each of the servers in the task'sonarray:@servers(['web-1' => '192.168.1.1', 'web-2' => '192.168.1.2']) @task('deploy', ['on' => ['web-1', 'web-2']]) cd /home/user/example.com git pull origin {{ $branch }} php artisan migrate --force @endtaskParallel Execution
By default, tasks will be executed on each server serially. In other words, a task will finish running on the first server before proceeding to execute on the second server. If you would like to run a task across multiple servers in parallel, add the
paralleloption 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 /home/user/example.com git pull origin {{ $branch }} php artisan migrate --force @endtaskSetup
Sometimes, you may need to execute arbitrary PHP code before running your Envoy tasks. You may use the
@setupdirective to define a block of PHP code that should execute before your tasks:@setup $now = new DateTime; @endsetupIf you need to require other PHP files before your task is executed, you may use the
@includedirective at the top of yourEnvoy.blade.phpfile:@include('vendor/autoload.php') @task('restart-queues') # ... @endtaskVariables
If needed, you may pass arguments to Envoy tasks by specifying them on the command line when invoking Envoy:
php vendor/bin/envoy run deploy --branch=masterYou may access the options within your tasks using Blade's "echo" syntax. You may also define Blade
ifstatements and loops within your tasks. For example, let's verify the presence of the$branchvariable before executing thegit pullcommand:@servers(['web' => ['[email protected]']]) @task('deploy', ['on' => 'web']) cd /home/user/example.com @if ($branch) git pull origin {{ $branch }} @endif php artisan migrate --force @endtaskStories
Stories group a set of tasks under a single, convenient name. For instance, a
deploystory may run theupdate-codeandinstall-dependenciestasks by listing the task names within its definition:@servers(['web' => ['[email protected]']]) @story('deploy') update-code install-dependencies @endstory @task('update-code') cd /home/user/example.com git pull origin master @endtask @task('install-dependencies') cd /home/user/example.com composer install @endtaskOnce the story has been written, you may invoke it in the same way you would invoke a task:
php vendor/bin/envoy run deployRunning Tasks
To run a task or story that is defined in your application's
Envoy.blade.phpfile, execute Envoy'sruncommand, passing the name of the task or story you would like to execute. Envoy will execute the task and display the output from your remote servers as the task is running:php vendor/bin/envoy run deployConfirming Task Execution
If you would like to be prompted for confirmation before running a given task on your servers, you should add the
confirmdirective to your task declaration. This option is particularly useful for destructive operations:@task('deploy', ['on' => 'web', 'confirm' => true]) cd /home/user/example.com git pull origin {{ $branch }} php artisan migrate @endtaskNotifications
Slack
Envoy supports sending notifications to Slack after each task is executed. The
@slackdirective accepts a Slack hook URL and a channel / user name. You may retrieve your webhook URL by creating an "Incoming WebHooks" integration in your Slack control panel.You should pass the entire webhook URL as the first argument given to the
@slackdirective. The second argument given to the@slackdirective should be a channel name (#channel) or a user name (@user):@finished @slack('webhook-url', '#bots') @endfinishedDiscord
Envoy also supports sending notifications to Discord after each task is executed. The
@discorddirective accepts a Discord hook URL and a message. You may retrieve your webhook URL by creating a "Webhook" in your Server Settings and choosing which channel the webhook should post to. You should pass the entire Webhook URL into the@discorddirective:@finished @discord('discord-webhook-url') @endfinishedTelegram
Envoy also supports sending notifications to Telegram after each task is executed. The
@telegramdirective accepts a Telegram Bot ID and a Chat ID. You may retrieve your Bot ID by creating a new bot using BotFather. You can retrieve a valid Chat ID using @username_to_id_bot. You should pass the entire Bot ID and Chat ID into the@telegramdirective:@finished @telegram('bot-id','chat-id') @endfinished