Mastering Automated Preview Environments in Laravel Cloud

Overview: Why Preview Environments Matter

solves the "it works on my machine" problem by automating the deployment of fresh environments for every feature. Instead of manually provisioning servers or sharing a cluttered staging environment, you can now trigger a unique, isolated instance of your application every time you push a branch or open a pull request. This ensures that every stakeholder—from designers to QA—sees exactly what you see before code hits production.

Prerequisites

To follow this guide, you should be familiar with

workflows (branching and pull requests) and have a basic understanding of
Laravel
project structure. Note that these automated features currently require a Growth or Business plan on the platform.

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

connected account settings to allow environment creation.

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

, your 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

build environment can successfully compile your assets.

Mastering Automated Preview Environments in Laravel Cloud

Fancy watching it?

Watch the full video and context

3 min read