Setting up a staging environment
A staging environment is where you, your testers and your clients check changes before they go live. On Hosty it's an ordinary development environment with a name you keep, such as stage, so you set it up yourself, without the admins.
The steps work the same for any development environment you want to keep for a while, such as uat or demo. Only production is different: see Adding a custom domain.
1. Pick a name and a branch
Name the environment after a Git branch, such as stage. The CI example deploys every branch to the environment of the same name, so a push to stage deploys the environment stage. If your CI deploys only some branches, add this one.
The name is part of the domains of the environment, so pick one you won't change. See Naming environments for what a name can have.
2. Set up the environment in hosty.yaml (optional)
A staging environment often needs settings of its own, such as the mode of the application, API keys of test accounts or cron jobs, which are off on development environments. Set them in an override:
applications:
backend:
overrides:
- condition:
environments: [stage]
php:
environmentVariables:
APP_MODE: stage
crontab:
enabled: trueSee Overrides, and Different secrets per environment for keys and passwords.
3. Protect it with a password
The domains of development environments are open to the internet. Anyone who finds the address can open the site, and search engines can index it. Hosty doesn't add a password, so your application has to: ask for a login and password with HTTP basic authentication on every environment except production. For example:
- Drupal: the Shield module.
- WordPress: a plugin, or a check of $_SERVER['PHP_AUTH_USER'] and $_SERVER['PHP_AUTH_PW'] early in wp-config.php.
- Next.js: a check of the Authorization header in the middleware.
- Django: a middleware that checks the Authorization header.
Keep the login and password in the secrets of your CI and pass them as sensitive environment variables, such as BASIC_AUTH_USER and BASIC_AUTH_PASSWORD. Make the application ask for the password only when they are set, and leave them out on production.
Leave /.healthz open: the load balancer checks it and sends no traffic to a pod that doesn't answer. nginx and Apache answer it for PHP and Python applications before the request reaches your code, but a Node.js application answers it itself.
4. Pick a domain (optional)
Each application of the environment gets a domain of the hosting:
<application>-<environment>.<project>.<hosted zone>For example, backend-stage.example.<hosted zone>. It stays the same as long as the name of the environment does, so testers can bookmark it. If that's all you need, skip this step.
A shorter domain of the hosting
Any domain one level under <project>.<hosted zone> already has a certificate and points to the hosting, so you can use, for example, stage.example.<hosted zone> without asking anyone. Add it to hosts in an override of the environment:
applications:
backend:
overrides:
- condition:
environments: [stage]
hosts:
- stage.example.<hosted zone>hosts replaces the generated domain. To keep it too, list it as well.
Your own domain
To run staging on a domain of your own, such as stage.example.com:
- Ask the admins for a certificate of the domain and add the DNS record they send you, as for production: see Adding a custom domain.
- Once the certificate is issued, add the domain to hosts in an override of the environment, as above.
- Deploy the environment.
- Point the domain to the load balancer of development environments, ingress-dev.<cluster>.<hosted zone>. This isn't the load balancer of production:
Type | Name | Value |
|---|---|---|
CNAME | stage.example.com | ingress-dev.<cluster>.<hosted zone> |
Hosty CLI stops the deployment if a domain in hosts has no issued certificate yet.
5. Deploy
Push the branch. CI builds the images and deploys the environment, and the first deployment imports the data of production, which takes a few minutes, longer for a large database. To see the domains of the environment, run:
hosty domains stageWhat to keep in mind
A staging environment is a development environment, with everything that comes with it (see Working with environments):
- Data. It starts with a copy of production, without the tables excluded from development environments. The data is imported only on the first deployment. To get fresh data, delete the environment and deploy it again, which deletes everything changed on staging.
- No backups. Only production is backed up. Don't keep anything on staging you can't lose.
- Access. Everyone on the project can see it, run commands in it and delete it.
- Resources. It runs one pod on the servers shared by the development environments of the cluster, so it's slower than production and isn't fit for load tests.
- Cleanup. If CI deletes the environments of deleted branches, as in Cleaning up old environments, staging lives as long as its branch.