Mastering Automated Preview Environments in Laravel Cloud
Overview: Why Preview Environments Matter
Prerequisites
To follow this guide, you should be familiar with
Key Libraries & Tools
- Laravel Cloud: The hosting platform providing the infrastructure automation.
- GitHub Integration: Connects your repository to the deployment pipeline.
- Composer: Manages PHP dependencies (ensure your versions are up to date to avoid deployment errors).
Setting Up the Automation
First, navigate to your application settings. If you haven't granted the necessary permissions, you'll see a warning. Fix this by updating your
Once permissions are green, create a New Automation. You can name it based on its purpose (e.g., "Feature-Branch-Previews"). Select the environment you want to replicate, such as main. This serves as the blueprint for your new preview instances.
Configuring Replication Rules
You can trigger previews based on two primary events: New Pull Requests or New Branches. For granular control, use branch name filtering to only trigger builds for names starting with feature/ or fix/.
# Trigger a preview by pushing a new branch
git checkout -b feature/pokemon-api-update
git push origin feature/pokemon-api-update
When configuring the replication, decide what data follows the code. You can replicate the entire database, specific object storage, or provide custom environment variables. If your app doesn't store sensitive user data, using the same database for previews can speed up the testing of data-driven features.
Syntax Notes & Best Practices
When setting up custom deploy commands, ensure they are compatible with the environment. For instance, if you're using a specific version of composer.json must reflect supported versions to prevent build failures.
Tips & Gotchas
One common mistake is selecting New Pull Request when you intended to trigger on a New Branch push. If your environment doesn't appear after a push, double-check your automation trigger settings. Also, watch out for outdated dependencies; if your application is older, run a composer update before pushing to ensure the

Fancy watching it?
Watch the full video and context