Skip to content
Latchkey

How to Deploy Through Test, Staging, and Production Environments in Bitbucket Pipelines

Tag each deploy step with deployment: test, deployment: staging, or deployment: production so each tier uses its own environment-scoped variables and shows in the deploy dashboard.

Bitbucket has three built-in deployment environment tiers: Test, Staging, and Production. Setting deployment: on a step binds it to that environment, gives it the environment variables you defined there, and tracks the release on the Deployments tab.

Steps

  • In Repository settings to Deployments, add variables per environment.
  • Set deployment: test / staging / production on each step.
  • Order the steps so promotion flows test to staging to production.
  • Read the same variable name in each step; the value differs per environment.

Pipeline

bitbucket-pipelines.yml
pipelines:
  branches:
    main:
      - step:
          name: Deploy to test
          deployment: test
          script:
            - ./deploy.sh --url $DEPLOY_URL
      - step:
          name: Deploy to staging
          deployment: staging
          script:
            - ./deploy.sh --url $DEPLOY_URL
      - step:
          name: Deploy to production
          deployment: production
          trigger: manual
          script:
            - ./deploy.sh --url $DEPLOY_URL

Gotchas

  • Each environment name (test/staging/production) can be used once per pipeline ordering by default.
  • Production deploys must happen after staging in the same branch unless you relax ordering rules.
  • Variables defined on an environment override repository variables of the same name.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →