Any CI/CD system
Run Infracost in any CI/CD system (TeamCity, CodeBuild, Harness, Buildkite, or anything else that can run a container or a binary) against any of the major VCS platforms, including self-hosted instances.
If you can install apps on GitHub, GitLab or Azure Repos, use an Infracost app instead. It needs no pipeline configuration and is the recommended path. If your CI is GitHub Actions, GitLab CI, Azure Pipelines, Bitbucket Pipelines or Jenkins, see the dedicated page. Those are this guide with the pipeline metadata detected for you.
What you need in the pipeline:
- The
infracost-cicontainer image, or the binary plusgit. INFRACOST_CLI_AUTHENTICATION_TOKENset as a pipeline secret.- For posting comments: a VCS token with the right scope. See the token table in step 3. Wrong scopes are the most common setup failure.
No Terraform binary, no terraform plan, and no plan JSON. infracost-ci reads your infrastructure code directly.
The pipeline does three things: estimate the cost difference on a pull request (step 2), upload a baseline from the default branch (step 4), and report merged pull requests (step 5).
1. Get the tool
The container image is the simplest route and carries git and the IaC parsers:
docker pull ghcr.io/infracost/ci:2
Tags are latest, the major series (2), the minor series (2.0) and the exact version (2.0.0). 2 is the one to use: it picks up new resources, pricing fixes and parser updates, and never crosses a breaking change. Only the exact version is immutable, so pin by digest if you need the bytes pinned.
The entrypoint is infracost-ci, but it execs bash and sh as given, so a CI that holds the container open with a shell needs nothing. One that holds it open some other way — the Jenkins Docker agent uses cat — needs the entrypoint cleared with --entrypoint=.
The image defaults to root, which sidesteps uid mismatches against a mounted workspace. Under a runAsNonRoot policy, give the user a writable home, because infracost-ci caches there:
docker run --rm --user 65532 -e HOME=/tmp -e INFRACOST_CLI_AUTHENTICATION_TOKEN \
-e INFRACOST_VCS_REPOSITORY_URL -v "$PWD:/src" -w /src \
ghcr.io/infracost/ci:2 scan --path .
Binary
If your runner cannot run a container, install the binary instead. Binaries for Linux, macOS and Windows are published on each release with a checksums.txt, and need no token to download.
Linux and macOS:
BASE="${INFRACOST_CI_BASE_URL:-${INFRACOST_SCANNER_BASE_URL:-https://github.com/infracost/ci/releases}}"
REF="latest/download" # or "download/v2.0.0" to pin
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m); case "$ARCH" in x86_64) ARCH=amd64 ;; aarch64) ARCH=arm64 ;; esac
SHA=$(command -v sha256sum || echo "shasum -a 256") # macOS has no sha256sum
# A mirror set up under the legacy variable serves the old archive name.
if [ -n "${INFRACOST_CI_BASE_URL:-}" ] || [ -z "${INFRACOST_SCANNER_BASE_URL:-}" ]; then
ARCHIVE="infracost-ci_${OS}_${ARCH}.tar.gz"
else
ARCHIVE="infracost-scanner_${OS}_${ARCH}.tar.gz"
fi
# Chained: an unverified archive must never reach tar.
curl -fsSL -O "${BASE}/${REF}/${ARCHIVE}" &&
curl -fsSL -O "${BASE}/${REF}/checksums.txt" &&
grep " ${ARCHIVE}$" checksums.txt | $SHA -c - &&
tar -xzf "$ARCHIVE"
Windows (PowerShell):
# PowerShell 5.1 may default below TLS 1.2, and the progress stream makes a
# multi-megabyte -OutFile download very slow.
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$ProgressPreference = "SilentlyContinue"
$Base = if ($env:INFRACOST_CI_BASE_URL) { $env:INFRACOST_CI_BASE_URL } elseif ($env:INFRACOST_SCANNER_BASE_URL) { $env:INFRACOST_SCANNER_BASE_URL } else { "https://github.com/infracost/ci/releases" }
$Ref = "latest/download" # or "download/v2.0.0" to pin
# An emulated x64 host on ARM64 reports AMD64; ARCHITEW6432 holds the real one.
$Machine = if ($env:PROCESSOR_ARCHITEW6432) { $env:PROCESSOR_ARCHITEW6432 } else { $env:PROCESSOR_ARCHITECTURE }
$Arch = if ($Machine -eq "ARM64") { "arm64" } else { "amd64" }
$Archive = "infracost-ci_windows_$Arch.zip"
Invoke-WebRequest -UseBasicParsing -Uri "$Base/$Ref/$Archive" -OutFile $Archive
Invoke-WebRequest -UseBasicParsing -Uri "$Base/$Ref/checksums.txt" -OutFile checksums.txt
$Expected = (Select-String -Path checksums.txt -Pattern " $Archive$").Line.Split(" ")[0]
$Actual = (Get-FileHash -Algorithm SHA256 $Archive).Hash.ToLower()
if ($Expected -ne $Actual) { throw "checksum mismatch for $Archive" }
Expand-Archive -Path $Archive -DestinationPath . -Force
Assets are infracost-ci_<os>_<arch>.tar.gz (linux, darwin), infracost-ci_windows_<arch>.zip and checksums.txt. Set INFRACOST_CI_BASE_URL to serve the same layout from your own mirror, for a runner with no route to GitHub. INFRACOST_SCANNER_BASE_URL remains supported for existing installations.
Binary installs need git on PATH, and the first run downloads the plugins.
checksums.txt comes from the same host as the archive, so verification proves the download was not corrupted, not that the host is honest. Only point INFRACOST_CI_BASE_URL at a host you trust.
2. Estimate the cost difference
infracost-ci diff takes two checkouts of the same repository: the base branch and the head branch. It does not take a plan file:
# A reused workspace still has the last run's worktrees registered.
git worktree prune
git fetch origin "$TARGET_BRANCH"
git worktree add base "origin/$TARGET_BRANCH"
git worktree add head HEAD
infracost-ci diff --base-path base --head-path head
That single command computes the diff, posts the pull request comment, uploads the run to Infracost Cloud, and exits non-zero if a guardrail blocks. There is no separate comment or upload step.
Your checkout needs full history, because a shallow clone may not contain the target branch. Set your CI's equivalent of GIT_DEPTH: 0.
On a self-hosted runner the workspace is reused between builds. Checkout deletes base/ and head/, but git keeps them registered, so the second push to a pull request fails with fatal: 'base' is a missing but already registered worktree. git worktree prune clears the stale registrations. It is a no-op on a fresh workspace, so leave it in either way.
3. Tell Infracost about the pull request
On GitHub Actions, GitLab CI, Bitbucket Pipelines, Azure Pipelines and Jenkins this is detected from the platform's own variables and you can skip this section. Everywhere else, set these before running diff:
| Variable | Example | Notes |
|---|---|---|
INFRACOST_VCS_PROVIDER | gitlab | One of github, gitlab, azure_repos, bitbucket. Self-hosted and Enterprise instances use the same value. |
INFRACOST_VCS_REPOSITORY_URL | https://gitlab.example.com/acme/infra | The repository web URL. Never a clone URL with credentials in it. |
INFRACOST_VCS_PULL_REQUEST_ID | 42 | The pull request number. On GitLab this is the project-scoped iid. |
INFRACOST_VCS_BRANCH | increase-iops | The branch being estimated. |
INFRACOST_VCS_BASE_BRANCH | main | The branch the pull request merges into. |
Infracost builds the pull request URL from the provider and the repository URL, so you do not need to know each provider's URL shape. Set INFRACOST_VCS_PULL_REQUEST_URL instead of the ID if you prefer; set both and they must agree.
Recommended, because they appear on the dashboard run: INFRACOST_VCS_PULL_REQUEST_TITLE, INFRACOST_VCS_PULL_REQUEST_AUTHOR, INFRACOST_VCS_PULL_REQUEST_LABELS (comma-separated) and INFRACOST_VCS_PIPELINE_RUN_ID.
Commit SHA, message, author and timestamp are read from the head checkout, which is why git must be on PATH. The container image has it.
Anything you set always wins over a detected value, so you can also use these to correct detection. Run with --debug to print one line naming what was inferred and what your environment overrode.
INFRACOST_CI_PLATFORM pins the detected platform: github_actions, gitlab_ci, bitbucket, jenkins, azure_devops_TfsGit, azure_devops_GitHub or azure_devops_GitHubEnterprise. Any other name turns inference off, which is what you want on a platform that mimics another one's variables.
Token scopes
Set these up before the first run:
| Provider | Token that works |
|---|---|
| GitHub | GITHUB_TOKEN with repo scope (fine-grained: pull requests read/write). |
| GitLab | GITLAB_TOKEN, a project, group or personal access token with api scope. The built-in CI_JOB_TOKEN cannot post merge request notes; this is the most common GitLab setup failure. A group-level setting can disable project tokens as well as group tokens. The error only mentions group tokens, even when the token you used was a project one. If you hit that, use a personal access token. |
| Azure Repos | SYSTEM_ACCESSTOKEN with "Contribute to pull requests" granted to the build identity, or AZURE_DEVOPS_EXT_PAT with Code read & write. |
| Bitbucket | BITBUCKET_TOKEN. Cloud: a repository or workspace access token with pull requests: write, or an Atlassian API token as email:api-token for Basic auth. App passwords are retired. Data Center: an HTTP access token with repo write. |
Self-managed and Enterprise
GitHub Enterprise Server, self-managed GitLab, Bitbucket Data Center and Azure DevOps Server are derived from the repository URL, with no extra configuration. For a private certificate authority, point INFRACOST_CI_VCS_TLS_CA_CERT_FILE at a PEM bundle. It applies to the VCS only: the dashboard, pricing and events clients use the system pool.
INFRACOST_CI_VCS_TLS_INSECURE_SKIP_VERIFY skips VCS certificate verification altogether and warns when it is on. Your comment token then travels over an unauthenticated connection, so prefer the CA file.
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 variable of the job, and do not set it on jobs that build pull requests from forks. See Modules on a private network for the format and what stays blocked.
4. Upload a baseline from the default branch
Run this from your default-branch pipeline so Infracost Cloud tracks baseline costs and issues. This is what populates Issue Explorer and Visibility → Repos, and keeps them current after merges.
export INFRACOST_VCS_REPOSITORY_URL="https://gitlab.example.com/acme/infra"
export INFRACOST_VCS_BRANCH="main"
infracost-ci scan --path .
scan takes a single directory, unlike diff. It still needs the repository URL, so that the baseline lands on the right repository, and it exits immediately without one on a CI platform that infers nothing. Do not set the pull request variables on this run — INFRACOST_VCS_PULL_REQUEST_ID, _URL, _TITLE, _AUTHOR or _LABELS — which is what makes a run a pull request run.
INFRACOST_VCS_BRANCH is not optional here, even though step 3 calls it detected. Buildkite, Jenkins, TeamCity and Woodpecker all check out a detached commit by default, which leaves no branch name to read. scan stops and names the variable rather than guessing. A CI that checks out a named branch, such as CircleCI, is unaffected.
5. Report merged pull requests
Infracost Cloud calculates cost prevention (money saved by changes made before merging) from merged pull requests, so it needs to know when one merges.
If your CI can trigger on a merge or close event, call status directly:
infracost-ci status --status MERGED --pr-number 42
Valid statuses are OPEN, MERGED and CLOSED. The pull request must match the one the estimate ran against; status takes the same identity flags as diff.
Send MERGED when the pull request is merged, and CLOSED when it is closed without merging. Only MERGED counts towards cost prevention. A CLOSED pull request is treated as abandoned and contributes nothing. Leaving a merged pull request at OPEN keeps it out of the calculation too, so its savings go unreported.
If your CI only triggers on pushes, extract the number from the merge commit on the default branch:
| Provider | Pattern to match against git log -1 --pretty=%B |
|---|---|
| GitHub | Merge pull request #([0-9]+) (squash merges: \(#([0-9]+)\)$ on the subject line) |
| GitLab | See merge request .+!([0-9]+) |
| Azure Repos | Merged PR ([0-9]+): |
| Bitbucket | pull request #([0-9]+) |
PR=$(git log -1 --pretty=%B | grep -oE 'See merge 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"
Both guards matter under set -e, which most CIs use. grep exits 1 when it matches nothing, and a bare [ -n "$PR" ] && … is itself a failing command when $PR is empty — so a direct push to the default branch would fail the step.
Fast-forward merges and customized commit message templates break the pattern match. The pull request simply won't get merged status; nothing else fails.
Guardrails and blocking
Guardrails and policies evaluate on every run. When one is set to block, the comment posts first and then diff exits 1, so the pipeline goes red with the reason already visible on the pull request. An unblock from the Infracost Cloud dashboard takes effect on the next run, so rerun the pipeline or push a commit.
A failing job does not stop a merge on its own. Every VCS needs to be told to require the check: a GitHub branch ruleset, GitLab's Pipelines must succeed, a Bitbucket branch restriction, or an Azure Repos Build validation policy. Without that, a blocking guardrail turns the build red and the pull request stays mergeable.
Comment and upload options
--tag sets the marker diff uses to find its own comment, default infracost-comment. Each run finds the comment carrying its own tag and updates it in place.
Changing --tag on a repository that already has an Infracost comment means the next run cannot find the old one, so it posts a second comment.
--comment-out-file <path> writes the comment to a file instead of posting it, or to stdout with -. Use it when the job that scans cannot hold a pull request write token, and a later job posts the comment. No VCS token is needed to write, and the dashboard run shows no comment, because none was posted. The pull request identity in step 3 is still required: the run is still uploaded as a pull request run, and a token already in the environment is still used to read pull request metadata. The file carries the --tag marker, so the comment a later job posts is the one the next run finds and updates.
INFRACOST_CI_DISABLE_DASHBOARD stops the run being uploaded to Infracost Cloud. Guardrails and policies are still fetched and still evaluate, and the comment still posts, but without a link to a dashboard run, nothing appears in the dashboard, and status becomes a no-op.
Commands
| Command | What it does |
|---|---|
infracost-ci diff --base-path <dir> --head-path <dir> | Cost difference between two checkouts, posts the comment, uploads the run. |
infracost-ci scan --path <dir> | Estimate a single directory and upload it. Used for baselines. |
infracost-ci status --status MERGED | Report a pull request as merged or closed. |
infracost-ci plugins install|list|detect | Install the parsers, report their versions, or list the projects they identify. No auth token needed. |
Known limitations
- Pull requests closed without merging stay open in the dashboard unless your pipeline reports them: no run happens on that event. App integrations detect it via webhooks.
- Merged-status detection from commit messages depends on your merge settings (see step 5).
- On a CI platform with no detection, every value in step 3 must be set by hand.
- Terraform
git::module sources resolve;hg::sources do not in the container image, because Mercurial is not in it.
Your code stays local
Code is parsed and processed in whatever environment the pipeline runs in. Only the cost estimate and the metadata from step 3 are sent to Infracost Cloud; your code never leaves your infrastructure.