Bitbucket Pipelines
Run Infracost in Bitbucket Pipelines to post cloud cost estimates and FinOps best practices on every pull request.
Bitbucket Pipelines will not enable until two-step verification is on. That is Bitbucket's own setting, separate from Atlassian account MFA, and turning it on asks for an Atlassian password — which an account created through Google login does not have until you set one.
1. Add the two variables
Under Repository settings → Repository variables, add both as Secured:
| Variable | Value |
|---|---|
INFRACOST_CLI_AUTHENTICATION_TOKEN | Your Infracost CLI token |
BITBUCKET_TOKEN | A repository or workspace access token with pull requests: write, or email:api-token for Basic auth |
BITBUCKET_TOKEN is required, not optional. Bitbucket has no predefined variable for the pull request title or author, so diff reads them from the pull request API with the same token it comments with.
For the Basic auth form, use an Atlassian API token with your account email, as email:api-token. Atlassian has retired app passwords, so the older user:app-password pairs no longer work.
2. Add the pipeline
image: ghcr.io/infracost/ci:2
# Full history: the shallow default may not contain the destination branch.
clone:
depth: full
pipelines:
pull-requests:
'**':
- step:
name: Infracost
script:
# BITBUCKET_GIT_HTTP_ORIGIN is http, so set the https web URL.
- export INFRACOST_VCS_REPOSITORY_URL="https://bitbucket.org/$BITBUCKET_REPO_FULL_NAME"
# FETCH_HEAD, not origin/<branch>: the clone has no remote-tracking
# ref for the destination branch.
- git fetch origin "$BITBUCKET_PR_DESTINATION_BRANCH"
- git worktree add base FETCH_HEAD
- git worktree add head HEAD
- infracost-ci diff --base-path base --head-path head
branches:
main:
- step:
name: Infracost baseline
script:
- export INFRACOST_VCS_REPOSITORY_URL="https://bitbucket.org/$BITBUCKET_REPO_FULL_NAME"
- infracost-ci scan --path .
diff takes two checkouts of the same repository, computes the cost difference, posts or updates the pull request comment, and uploads the run to Infracost Cloud. No Terraform binary and no plan file: the parsers read your infrastructure code directly.
The branches: main step keeps baseline costs and issues current, which populates Issue Explorer and Visibility → Repos. scan takes a single directory, unlike diff.
Three things are specific to Bitbucket:
- The repository URL is set by hand.
BITBUCKET_GIT_HTTP_ORIGINis anhttp://URL, which would key a second repository on the dashboard. difflives inpull-requests:.BITBUCKET_PR_IDonly exists there, which is why the branch pipeline runsscaninstead.- The base worktree points at
FETCH_HEAD. The clone has no remote-tracking ref for the destination branch.
Bitbucket ignores the image's entrypoint and runs the script: lines itself, so each line names the binary.
3. Report merged pull requests
Infracost Cloud calculates cost prevention (money saved by changes made before merging) from merged pull requests. Bitbucket Pipelines only trigger on pushes, so read the merge commit on the default branch.
Add this as a second step under the existing branches: main: key from step 2. A second main: key in the same file is invalid YAML, and Bitbucket keeps only one of the two, so the baseline step silently stops running:
branches:
main:
- step:
name: Infracost baseline
script:
- export INFRACOST_VCS_REPOSITORY_URL="https://bitbucket.org/$BITBUCKET_REPO_FULL_NAME"
- infracost-ci scan --path .
- step:
name: Infracost merged status
script:
- export INFRACOST_VCS_REPOSITORY_URL="https://bitbucket.org/$BITBUCKET_REPO_FULL_NAME"
- "PR=$(git log -1 --pretty=%B | grep -oE 'pull request #[0-9]+' | grep -oE '[0-9]+$') || true"
- '[ -n "$PR" ] && infracost-ci status --status MERGED --pr-number "$PR" || echo "no pull request in the commit message"'
Fast-forward merges and customized merge commit templates break the pattern match. The pull request simply won't get merged status; nothing else fails.
Bitbucket Data Center
Set INFRACOST_VCS_REPOSITORY_URL to the repository's full web URL on your Bitbucket Data Center instance — the address your browser shows when you view the repository, not a value from Infracost Cloud:
export INFRACOST_VCS_REPOSITORY_URL="https://bitbucket.acme.com/projects/ACME/repos/infra"
Alternatively, pass --bitbucket-repo ACME/infra and --bitbucket-server-url https://bitbucket.acme.com. The comment token is an HTTP access token with repo write.
Modules on a private network
If your Terraform modules live on a git server on a private network, Infracost blocks the download by default. List the server in INFRACOST_CI_PRIVATE_SOURCE_ALLOWLIST as a repository or workspace variable, and do not set it on pipelines that run for pull requests from forks. See Modules on a private network for the format and what stays blocked.
Guardrails and blocking
Guardrails and policies evaluate on every run. When one blocks, the comment posts first and then diff exits 1, so the pipeline goes red with the reason already on the pull request. An unblock from the dashboard takes effect on the next run.
A red pipeline does not stop a merge on its own. To block one, add a merge check under Repository settings → Branch restrictions requiring the build to pass.
Your code stays local
Code is parsed inside the runner. Only the cost estimate and the pull request metadata reach Infracost Cloud.