DEV Community

Julia for Light Cloud

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

SaaS Part 5: Add a Password-Protected Staging Site

SaaS Part 5: Add a Password-Protected Staging Site

To add a staging site to a SaaS on Light Cloud, push a staging branch. Light Cloud creates a Staging environment with its own address. Then give it its own PostgreSQL database by setting DATABASE_URL on that environment, and set a visitor password on its Security tab. Staging then runs the same code on the same infrastructure as production, but test sign-ups never reach your customers' data.

In my run the Staging environment appeared 2 seconds after the push, and the password page was up 12 seconds after I saved the password.

What you will build

A second copy of the app from part 4, on its own address and behind a password:

Production Staging
Branch main staging
Address your domain from part 4 staging-tutorial-saas-starter-yourworkspace.light-cloud.io
Database saas-db saas-staging-db
Visitors everyone only people with the password

The app's Environments tab with Production on main and Staging on staging, both Deployed, Staging highlighted

Before you start

  • The app from part 4, running.
  • A plan with two databases. Lite includes one, so I ran this on Starter. Pricing lists what each plan includes.
  • Git and a clone of your fork:
$ git clone https://github.com/YOUR-USERNAME/tutorial-saas-starter.git
$ cd tutorial-saas-starter


PS> git clone https://github.com/YOUR-USERNAME/tutorial-saas-starter.git
PS> cd tutorial-saas-starter

Enter fullscreen mode Exit fullscreen mode

Why staging needs its own database

A new environment starts with the variables you set when you created the app in part 1: APP_NAME, EMAIL_FROM, AUTH_SECRET and DATABASE_URL. So a fresh staging environment talks to the production database until you change that. Variables you added later on Production , like the Stripe keys and APP_URL, stay on Production.

That's why the database comes first in this tutorial, before the branch.

Step 1: Create the staging database

  1. In the Light Cloud console, click New... , New Database.
  2. Name it saas-staging-db, keep Size at Dev , and click Create database. Mine was ready within seconds.
  3. On its Credentials tab, choose Connect from: Node.js and copy the Connection String , as in part 1.

Make a second AUTH_SECRET too. Staging is where you share passwords and test accounts with other people, so keep its secrets apart from production's:

$ openssl rand -hex 32


$ openssl rand -hex 32

Enter fullscreen mode Exit fullscreen mode

On Windows, run it in Git Bash.

Step 2: Push the staging branch

$ git switch -c staging
$ git push -u origin staging


PS> git switch -c staging
PS> git push -u origin staging

Enter fullscreen mode Exit fullscreen mode

Auto-create environment per branch is on by default, so Light Cloud builds the branch by itself. The app's Environments tab lists Staging next to Production , and the sidebar shows staging under the app. My first staging build was live 7 minutes after the push.

Open Staging , then Settings , and look at Environment Variables. The card says which ones came from the app:

The Environment Variables card of Staging: Inherited from defaults APP_NAME, EMAIL_FROM, AUTH_SECRET, DATABASE_URL, highlighted, with the values hidden

That first build ran on the production database. The migrations had nothing to do there, so it changed nothing, but it's the reason for the next step.

Step 3: Give staging its own variables

  1. On the Environment Variables card of Staging , click Edit. The editor lists the inherited variables with their values, ready to change.
  2. Set APP_NAME to Rota Planner (staging), so nobody mistakes one for the other.
  3. Replace AUTH_SECRET with the new secret, and DATABASE_URL with the staging connection string.
  4. Click Save.

The staging editor with APP_NAME Rota Planner (staging), and the new AUTH_SECRET and DATABASE_URL hidden, Save highlighted

The editor shows the inherited values in plain text, production's database password among them. Edit it where nobody is looking over your shoulder.

Saving redeploys staging. On start it runs the migrations against the empty staging database. The Logs tab shows Database migrations are up to date. again, and saas-staging-db now has the app's 10 tables and no users.

Staging has no Stripe variables, so billing is off there. If you want to test payments on staging, add a second webhook endpoint in Stripe's test mode that points at the staging address, and set the four STRIPE_ variables on Staging , with that endpoint's signing secret.

Step 4: Set a password

  1. On Staging , open the Security tab.
  2. Under Password Protection , click Set password.
  3. Enter a password twice and click Save password.

The Password Protection card with both password fields filled and Save password highlighted

Store the password before you save. The console says so too: it's hashed on save and can't be shown again. The card then says This site is password protected :

The Password Protection card saying This site is password protected, with Change and Remove

It applies to staging only, and without a redeploy. Open the staging address in a private window and you get the password page first:

The password page: tutorial-saas-starter, Enter the password to view this site, a Password field and Continue, Protected by Light Cloud

From the terminal you can see what search engines get:

$ curl -I https://staging-tutorial-saas-starter-yourworkspace.light-cloud.io/
HTTP/2 401
x-robots-tag: noindex, nofollow


PS> curl.exe -I https://staging-tutorial-saas-starter-yourworkspace.light-cloud.io/
HTTP/2 401
x-robots-tag: noindex, nofollow

Enter fullscreen mode Exit fullscreen mode

Put a password on a staging site covers the password page in more detail.

Step 5: Try a change on staging

Make a visible change on the staging branch. In src/lib/plans.ts, change the Pro plan's description:

    description: "For small teams shipping every week, now with email support.",


$ git commit -am "Pro plan: mention email support"
$ git push


PS> git commit -am "Pro plan: mention email support"
PS> git push

Enter fullscreen mode Exit fullscreen mode

Staging redeploys and shows the new text on its pricing page:

The Pro plan card on staging: For small teams shipping every week, now with email support

Production still shows the old one:

The Pro plan card on production: For small teams shipping every week

Step 6: Promote staging to production

When staging looks right, merge it into main:

$ git switch main
$ git pull
$ git merge staging
$ git push


PS> git switch main
PS> git pull
PS> git merge staging
PS> git push

Enter fullscreen mode Exit fullscreen mode

Production redeploys from main with its own variables, database and domain. Mine took 4 minutes 18 seconds, shorter than the first build in part 1. Keep the staging branch for the next change. If you delete it, Light Cloud removes the staging environment with it.

Troubleshooting

Staging shows the same users as production

Staging still uses the inherited DATABASE_URL. Set it on the Staging environment, save, and let the redeploy finish.

The staging password page doesn't appear

You opened production, or a browser that already entered the password. Try a private window on the staging address.

Top comments (0)