Laravel Envoy is a simple and elegant task runner for deploying PHP applications. It allows you to define deployment scripts in a clean syntax and easily automate tasks such as deploying code, clearing caches, running migrations, and more. Envoy is particularly useful for streamlining the deployment process, ensuring consistency, and reducing human error.
Key Features of Laravel Envoy:
- Simple Syntax: Define tasks using a straightforward and expressive syntax, making it easy to read and write.
- SSH Support: Execute tasks on remote servers via SSH, allowing for seamless deployment to production environments.
- Flexible Configuration: Customize tasks and configurations to suit your specific deployment needs.
- Built-in Helpers: Comes with built-in methods for common deployment tasks, like running Artisan commands, uploading files, and more.
- Environment Variables: Manage environment variables easily for different deployment stages.
Installation
To get started with Laravel Envoy, follow these steps:
- Install Envoy: You can install Laravel Envoy globally using Composer:
1composer global require laravel/envoy - Ensure Global Composer Bin is in Your PATH: Make sure that the Composer global bin directory is in your system’s PATH. You can check this by running:
1echo $PATH~/.composer/vendor/bin
or~/.config/composer/vendor/bin
- Create an Envoy.blade.php File: In the root of your project, create an
Envoy.blade.php
file. This file will contain your deployment tasks.
Defining Tasks
Here’s how you can define tasks in your Envoy.blade.php
file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
@task('deploy', ['on' => 'server']) echo 'Deploying the application...'; // Navigate to the application directory cd /path/to/your/application; // Pull the latest changes from the repository git pull origin main; // Install dependencies composer install --no-dev; // Run migrations php artisan migrate --force; // Clear caches php artisan cache:clear; echo 'Deployment completed successfully!'; @endtask |
Running Tasks
To run a defined task, you can use the Envoy command in your terminal:
1 |
envoy run deploy |
Configuring SSH Connections
You can configure your SSH connections within the Envoy.blade.php
file by using the @on
directive. Here’s an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
@setup $servers = [ 'production' => [ 'host' => 'your-server.com', 'user' => 'your-username', 'identityFile' => '/path/to/your/private/key', ], ]; @endsetup @task('deploy', ['on' => 'production']) // Your deployment tasks here @endtask |
Example of Multiple Tasks
You can also define multiple tasks and execute them in sequence:
1 2 3 4 5 6 7 8 9 10 |
@task('build', ['on' => 'production']) echo 'Building the assets...'; npm install; npm run production; @endtask @task('deploy', ['on' => 'production']) // Your deployment tasks here @include('build'); @endtask |
Conclusion
Laravel Envoy is a powerful tool that simplifies the deployment process for PHP applications. Its elegant syntax, combined with SSH support and flexible configuration options, makes it an excellent choice for developers looking to automate their deployment tasks efficiently.
Additional Considerations
- Documentation: For more detailed instructions and advanced features, refer to the official Laravel Envoy documentation.
- Error Handling: Consider implementing error handling within your tasks to manage failures gracefully during deployment.