Mastering Build and Deploy Hooks in Laravel Vapor
Overview of Deployment Automation
Deploying a application requires more than just moving files to a server. You must compile assets, manage dependencies, and sync database schemas. handles these complexities through two distinct mechanisms: Build Hooks and Deploy Hooks. Understanding the timing of these hooks is the difference between a seamless launch and a broken production environment. Build hooks prepare your artifact locally, while deploy hooks execute logic directly within the environment once the code is live.
Prerequisites
To follow this guide, you should be comfortable with the following:
- Basic framework architecture.
- A functioning account and the Vapor CLI.
- Familiarity with syntax for configuration files.
- Understanding of or for asset and dependency management.
Key Libraries & Tools
- Laravel Vapor CLI: The primary tool for initiating deployments and viewing real-time logs.
- vapor.yaml: The central configuration file where hook logic is defined.
- AWS (Amazon Web Services): The underlying infrastructure where your application and databases reside.
- Artisan: Laravel's command-line interface used within deploy hooks for tasks like migrations.
Code Walkthrough: Configuring Hooks
Open your vapor.yaml file to define how each environment behaves. You can customize commands per environment, such as choosing between development or production asset builds.
id: 1
name: my-app
environments:
staging:
build:
- 'composer install'
- 'npm install && npm run development'
- 'php artisan event:cache'
deploy:
- 'php artisan migrate --force'
production:
build:
- 'composer install --no-dev'
- 'npm install && npm run production'
In this configuration, the build section runs on your local machine or CI/CD environment before the code is zipped and uploaded. This is the ideal time for npm run production because it minimizes the final package size. The deploy section runs after the code is uploaded to . We use php artisan migrate here because the command needs to interact with the database that exists within the cloud environment, not your local machine.
Syntax Notes
uses standard list syntax for hooks. Each command is a string item in a sequence. Vapor executes these commands in order; if any command returns a non-zero exit code, the deployment aborts immediately. This safety feature prevents broken code from reaching your users.
Practical Examples: Database Integration
Running migrations via deploy hooks requires a linked database. In the Vapor dashboard, create a database resource (e.g., my-staging-db). Once created, link it in your vapor.yaml so the environment knows where to run the migration commands:
staging:
database: my-staging-db
deploy:
- 'php artisan migrate --force'
Tips & Gotchas
One common pitfall is forgetting that build hooks lack access to your production database. Never put php artisan migrate in a build hook; it will fail because your local machine cannot (and should not) reach your cloud database directly. Conversely, avoid running heavy asset compilation in deploy hooks, as this consumes resources and increases deployment time. Always keep your build artifacts lean and your deployment logic focused on environment-specific tasks.
- 23%· products
- 15%· companies
- 15%· products
- 15%· products
- 8%· products
- Other topics
- 23%

Learn Laravel Vapor #08: Deployment hooks
WatchLaravel // 9:32
The official YouTube channel of Laravel, the clean stack for Artisans and agents. We will update you on what's new in the world of Laravel, from the framework to our products Cloud, Forge, and Nightwatch.