Skip to content
Latchkey

How to Set a Variable in Bitbucket Pipelines

Bitbucket Pipelines reads variables from the repo/workspace settings and from inline shell exports within a step.

Define non-secret config and secrets as repository, deployment, or workspace variables in settings. Within a step, normal shell export works; to cross steps, write a dotenv artifact.

Repository variable + cross-step value

A repo variable $NODE_ENV is read directly; a computed value is passed downstream via an artifact file.

bitbucket-pipelines.yml
pipelines:
  default:
    - step:
        name: Build
        image: node:20
        script:
          - echo "Building in $NODE_ENV"
          - echo "GIT_SHA=$BITBUCKET_COMMIT" > build.env
        artifacts:
          - build.env
    - step:
        name: Deploy
        script:
          - source build.env
          - ./deploy.sh "$GIT_SHA"

Gotchas

  • Each step runs in a fresh container, so export does not persist between steps - pass values via artifacts.
  • Bitbucket provides built-in vars like $BITBUCKET_COMMIT, $BITBUCKET_BRANCH, $BITBUCKET_BUILD_NUMBER.
  • Define secrets as Secured repository/workspace variables so they are masked in logs.

Related guides

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