DEV Community

Julia for Light Cloud

Posted on Originally published at blog.light-cloud.com on

SaaS Part 6: Preview Every Pull Request

SaaS Part 6: Preview Every Pull Request

To preview every pull request of a SaaS on Light Cloud, push a branch and open a pull request. Light Cloud builds the branch into its own environment, comments the live link on the pull request, reports a GitHub check, and removes the environment when the branch is deleted. The work for a SaaS is making sure previews start with safe variables: a test database, a separate AUTH_SECRET, and no live Stripe keys.

In my run the preview environment and the pull request comment were both there 2 seconds after I opened the pull request, and the preview was live 5 minutes later.

What you will build

A pull request on your fork with:

  • a comment from Light Cloud with the preview's address;
  • a Light Cloud check with the build steps;
  • a preview of the branch on its own address;
  • automatic cleanup when the branch is deleted.

    Before you start

  • The app from part 5, with its staging database.

  • Your fork cloned on your laptop, as in part 5.

Step 1: Check the branch settings

Open the app (not an environment) and its Settings tab. Four switches control previews, and all four are on by default:

The app's Settings: Branch Automation with Auto-create environment per branch and Auto-cleanup when branch is deleted, and GitHub with Report deployments as GitHub checks and Comment preview links on pull requests, all four highlighted

A preview URL for every branch explains each one.

Step 2: Know what a preview starts with

A preview environment starts with the app's defaults: the variables you set when you created the app in part 1. For this app that's APP_NAME, EMAIL_FROM, AUTH_SECRET and DATABASE_URL, so a preview runs on the production database with production's secret. The Stripe keys and APP_URL you added later on Production stay there, so a preview can't take payments.

Step 3: Open a pull request

Make a change on a new branch. In src/lib/plans.ts, change the Free plan's description:

    description: "For trying it out on your own. No card needed.",


$ git switch main
$ git pull
$ git switch -c free-plan-5-projects
$ git commit -am "Free plan: say no card is needed"
$ git push -u origin free-plan-5-projects


PS> git switch main
PS> git pull
PS> git switch -c free-plan-5-projects
PS> git commit -am "Free plan: say no card is needed"
PS> git push -u origin free-plan-5-projects

Enter fullscreen mode Exit fullscreen mode

On GitHub, click Compare & pull request on the banner, then Create pull request.

The app's Environments tab gets a third environment, named after the branch:

The Environments tab with Production, Staging and Free Plan 5 Projects on the branch free-plan-5-projects, its commit Free plan: say no card is needed highlighted

Its Settings show the variables it inherited, production's DATABASE_URL and AUTH_SECRET among them:

The preview's Environment Variables: Inherited from defaults APP_NAME, EMAIL_FROM, AUTH_SECRET and DATABASE_URL, with AUTH_SECRET and DATABASE_URL highlighted

Step 4: Open the preview

Light Cloud comments on the pull request with a table: Status , Preview and Updated. While the branch builds it says Building , and then Ready with the address, https://free-plan-5-projects-tutorial-saas-starter-yourworkspace.light-cloud.io. The Light Cloud check on the pull request turns green at the same time.

Open the address. The pricing page shows the new Free plan text, while production still shows the old one:

The Free plan card on the preview: For trying it out on your own. No card needed.

Billing on the preview isn't available, because the Stripe variables live on Production only. A preview should never be able to charge a card.

Step 5: Merge and clean up

Merge the pull request on GitHub with Merge pull request , then click Delete branch. Two things follow:

  1. The merge reaches main, so production redeploys with the new text.
  2. Deleting the branch removes its environment. The comment on the pull request changes to Preview removed , and the preview's address stops answering. In my run both happened within seconds. <!-- IMAGE: s6-06-preview-removed.webp, the comment updated to Preview removed. Needs a GitHub sign-in in the capture browser. --> ## Troubleshooting

The preview shows production's users

That's the inherited DATABASE_URL from step 2. Set DATABASE_URL on the preview environment itself to a test database and push a new commit to the branch, so the preview redeploys.

No comment appears on the pull request

The Light Cloud GitHub App needs the pull request, checks and deployments permissions. The app's Settings tab says so if one is missing.

The pull request's CI check fails

The repository's own tests run on every pull request too. They check the plan limits, so a change to the number of projects needs the tests in test/plans.test.ts changed with it.

Top comments (0)