Skip to content

CI Integration

cmakefmt is designed to be a zero-friction CI step. Use --check to fail the build when files are not formatted, or --in-place to auto-format and commit the result.

The official cmakefmt/cmakefmt-action installs the right binary for the runner OS and adds it to PATH. No Rust toolchain required.

Fail the workflow if any CMake files are not formatted:

- uses: cmakefmt/cmakefmt-action@v1
with:
args: '--check .'

Format files in-place. Combine with a commit step to push the changes automatically:

- uses: cmakefmt/cmakefmt-action@v1
with:
args: '--in-place .'

Install cmakefmt without running it, then call it yourself with custom flags:

- uses: cmakefmt/cmakefmt-action@v1
with:
args: ''
- run: cmakefmt --check --report-format github .
- uses: cmakefmt/cmakefmt-action@v1
with:
version: '0.2.0'
args: '--check .'
name: Format check
on: [push, pull_request]
jobs:
cmakefmt:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
- uses: cmakefmt/cmakefmt-action@v1
with:
args: '--check .'

Download the pre-built Linux binary directly from GitHub Releases:

cmakefmt:
stage: lint
image: ubuntu:latest
before_script:
- apt-get update -qq && apt-get install -y -qq curl
- |
LATEST=$(curl -sI https://github.com/cmakefmt/cmakefmt/releases/latest \
| grep -i '^location:' \
| sed 's|.*/tag/v||;s/[[:space:]]//g')
curl -sSL \
"https://github.com/cmakefmt/cmakefmt/releases/download/v${LATEST}/cmakefmt-x86_64-unknown-linux-musl.tar.gz" \
| tar -xz -C /usr/local/bin
script:
- cmakefmt --check .

Or install via Cargo if you already have a Rust image:

cmakefmt:
stage: lint
image: rust:latest
cache:
paths:
- $CARGO_HOME/bin/
script:
- cargo install cmakefmt-rust --quiet
- cmakefmt --check .

Add a local hook to your .pre-commit-config.yaml. This runs cmakefmt --check on every staged CMake file before the commit is created:

repos:
- repo: local
hooks:
- id: cmakefmt
name: cmakefmt
language: system
entry: cmakefmt --check
files: '(CMakeLists\.txt|\.cmake)$'
pass_filenames: true

Install the hook once per clone:

Terminal window
pre-commit install

You can also run it manually across all files:

Terminal window
pre-commit run cmakefmt --all-files

When working on large repositories, limit formatting checks to files changed since a given ref to keep CI fast:

Terminal window
# Check files changed since the last tag
cmakefmt --check --changed v0.1.0
# Check only staged files (useful locally)
cmakefmt --check --staged