Skip to main content

Preview Environments from the CLI

A preview environment is a fork of an existing environment for one branch or pull request. It is created when the code changes and removed when the branch merges. One command converges it:

shell
mass environment preview pr42 -f preview.yaml

Running mass environment preview with --follow

The command runs forkEnvironment, setEnvironmentDefault, copyInstance, updateInstance, and deployEnvironment from one YAML config. Each step is idempotent, so the same command runs on open, on every push, and after a rebase, and CI tracks no state.

Config​

The smallest config forks demo-production into demo-pr42, seeds every instance from the parent, and deploys:

yaml
project: demo
baseEnvironment: production

Every field:

yaml
project: demo                # required
baseEnvironment: production # required; the parent is <project>-<baseEnvironment>

# Fork options. Default false.
copyEnvironmentDefaults: true
copySecrets: false
copyRemoteReferences: false

# Required when the organization declares custom attributes at the ENVIRONMENT scope.
# Keys and values are strings. --attributes overrides it.
attributes:
region: us-east-1
pr: "${GITHUB_PR}"

# Resources to set as environment defaults. The CLI uses resourceId; resourceType is for readers.
environmentDefaults:
- resourceType: aws-iam-role
resourceId: 161aeb95-e1c5-4f8d-803e-ef82087d7ad4

# Per-instance overrides, by component ID. An instance listed with no fields keeps the fork's seed.
instances:
chatsvc:
version: "latest+dev" # +dev resolves development releases
params:
ingress:
enabled: true
host: chatty-pr-${GITHUB_PR}.example.com
secrets:
- name: STRIPE_KEY
value: ${STRIPE_TEST_KEY}
remoteReferences:
# An imported resource, by UUID
- resourceId: a1b2c3d4-...
field: kubernetes_cluster
# A provisioned resource, as <instance-id>.<field>
- resourceId: demo-production-db.hostname
field: database

The preview environments reference has the full schema.

Steps​

  1. forkEnvironment creates <project>-<ID>, or seeds the existing one again, from the parent, with the copy* options.
  2. setEnvironmentDefault sets each resource in environmentDefaults.
  3. For each entry in instances, copyInstance deep-merges params onto the parent's params, updateInstance sets version, and each secret and remote reference is set.
  4. deployEnvironment deploys every instance in dependency order.

Running it again converges. Manual edits to the preview are reset. A copySecrets: true added later copies the secrets on the next run.

Flag
-f, --fileThe config. Default preview.yaml.
-n, --nameThe environment name. Defaults to the ID.
-d, --descriptionThe environment description.
-a, --attributesCustom attributes. Overrides attributes in the config.
--followStream every deployment's logs until the rollout completes, each line prefixed with the instance ID.

The ID must match ^[a-z0-9]{1,20}$.

Environment variables​

${VAR} and $VAR anywhere in the file, comments included, expand from the process environment before parsing. A variable that is not set stays as written, and the CLI prints a warning that names it. $$ is a literal $.

GitHub Actions​

yaml
name: Preview Environment

on:
pull_request:
types: [opened, reopened, synchronize, closed]

jobs:
preview:
runs-on: ubuntu-latest
env:
MASSDRIVER_API_KEY: ${{ secrets.MASSDRIVER_API_KEY }}
MASSDRIVER_ORGANIZATION_ID: ${{ secrets.MASSDRIVER_ORGANIZATION_ID }}
GITHUB_PR: ${{ github.event.pull_request.number }}
steps:
- uses: actions/checkout@v4
- uses: massdriver-cloud/actions/setup@v5

# Environment IDs cannot contain dashes.
- name: Converge preview environment
if: github.event.action != 'closed'
run: mass environment preview "pr${GITHUB_PR}" -f preview.yaml

# decommission removes instances in reverse dependency order. --follow waits until
# every instance is gone, because delete needs an empty environment.
- name: Remove preview environment
if: github.event.action == 'closed'
run: |
mass environment decommission "demo-pr${GITHUB_PR}" --follow
mass environment delete "demo-pr${GITHUB_PR}"

With the primitives​

For another flow, such as keeping one environment across pull requests and promoting configs from main into it, call the primitives:

shell
mass environment fork ecomm-production pr42 --copy-environment-defaults --copy-secrets
mass environment default ecomm-pr42 <shared-vpc-resource-id>
mass instance promote ecomm-staging-app --to ecomm-pr42-app
mass environment deploy ecomm-pr42

# When the pull request merges
mass environment decommission ecomm-pr42 --follow
mass environment delete ecomm-pr42

Reference​