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
exportdoes 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
How to Set an Environment Variable in GitHub ActionsSet environment variables in GitHub Actions at the workflow, job, or step level with env:, and export dynamic…
How to Use Secrets in Bitbucket PipelinesUse secrets in Bitbucket Pipelines with secured repository, deployment, and workspace variables that are mask…