Laravel Envoy is a task automation tool that simplifies deployment and task execution in Laravel applications. It allows you to define and run tasks using a simple syntax, making it easy to automate repetitive tasks such as deployments, running tests, and managing server processes.
Key Features
- Task Automation: Define and automate repetitive tasks using a straightforward syntax.
- SSH Support: Execute commands on remote servers over SSH, making it suitable for deployment processes.
- Simplified Syntax: Use a clean and expressive syntax for defining tasks, reducing complexity.
- Integration with Laravel: Seamlessly integrates with your Laravel application and environment.
- Parallel Execution: Execute tasks on multiple servers in parallel, improving efficiency.
Installation
To install Laravel Envoy, you can use Composer:
1 |
composer global require laravel/envoy |
Ensure that the Composer vendor/bin
directory is in your system’s PATH to access the envoy
command easily.
Configuration
- Create an Envoy.blade.php File:
In the root of your Laravel project, create a file named
Envoy.blade.php
. This file will contain your task definitions.bash
1touch Envoy.blade.php - Define Your Tasks:
In
Envoy.blade.php
, you can define your tasks using the Blade syntax. Here’s an example of a basic deployment task:php
1234567@task('deploy', ['on' => 'server_name'])cd /path/to/your/projectgit pull origin maincomposer install --no-interaction --prefer-distphp artisan migrate --forcephp artisan config:cache@endtaskIn this example:
- The
@task
directive defines a task nameddeploy
. - The
['on' => 'server_name']
option specifies the server on which the task will run. - The commands inside the task will execute on the specified server.
- The
Running Tasks
To run a task, use the envoy run
command followed by the task name:
1 |
envoy run deploy |
Using Multiple Servers
You can easily define tasks that run on multiple servers. For example:
1 2 3 4 5 6 7 8 9 10 11 12 |
@servers(['web' => 'user@webserver.com', 'database' => 'user@dbserver.com']) @task('deploy', ['on' => 'web']) cd /path/to/your/project git pull origin main composer install --no-interaction --prefer-dist @endtask @task('migrate', ['on' => 'database']) cd /path/to/your/project php artisan migrate --force @endtask |
In this example, you define two tasks, one for deploying on the web server and another for running migrations on the database server.
Parallel Execution
To execute tasks in parallel, you can use the @parallel
directive. Here’s an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
@task('deploy', ['on' => 'web']) cd /path/to/your/project git pull origin main composer install --no-interaction --prefer-dist @endtask @task('migrate', ['on' => 'database']) cd /path/to/your/project php artisan migrate --force @endtask @parallel deploy migrate @endparallel |
Conclusion
Laravel Envoy is an excellent tool for automating tasks and deployments in Laravel applications. Its simple syntax and support for SSH make it easy to manage your deployment process and automate repetitive tasks. By defining tasks in a clean and readable way, you can improve your workflow and ensure consistent deployments.