|
| 1 | +--- |
| 2 | +title: Other CI systems |
| 3 | +description: Run clang-format and clang-tidy on the files a branch changes in GitLab CI, Jenkins, Azure Pipelines and Bitbucket Pipelines, with the cpp-linter command. |
| 4 | +hide: |
| 5 | + - navigation |
| 6 | +--- |
| 7 | + |
| 8 | +# Other CI systems |
| 9 | + |
| 10 | +[cpp-linter-action](getting-started.md#on-every-pull-request) is for GitHub Actions. On any other |
| 11 | +CI system, run the `cpp-linter` command: it reads `git diff`, checks the C and C++ files your |
| 12 | +branch changes with the LLVM version you pin, and writes a report. |
| 13 | + |
| 14 | +What you get outside GitHub: |
| 15 | + |
| 16 | +- The findings in the job log, and a Markdown report in `cpp-linter.md` that the job keeps. |
| 17 | +- A failed job when there are findings. |
| 18 | +- No comments or reviews on the merge request or pull request. cpp-linter posts those on GitHub |
| 19 | + only. |
| 20 | + |
| 21 | +## The script |
| 22 | + |
| 23 | +Save this as `ci/cpp-linter.sh`. It is the same for every CI system; only the job definition |
| 24 | +around it differs. |
| 25 | + |
| 26 | +```bash title="ci/cpp-linter.sh" |
| 27 | +#!/usr/bin/env bash |
| 28 | +# Checks the files this branch changes with clang-format and clang-tidy. |
| 29 | +# Usage: ci/cpp-linter.sh <target branch> |
| 30 | +set -euo pipefail |
| 31 | + |
| 32 | +target="$1" |
| 33 | +llvm=21 |
| 34 | + |
| 35 | +python3 -m venv .venv |
| 36 | +. .venv/bin/activate |
| 37 | +pip install --quiet 'cpp-linter<2' clang-tools |
| 38 | +clang-tools install clang-format clang-tidy --version "$llvm" --directory .venv/bin --no-progress-bar |
| 39 | + |
| 40 | +git fetch --quiet origin "+refs/heads/$target:refs/remotes/origin/$target" |
| 41 | +base="$(git merge-base "origin/$target" HEAD)" |
| 42 | + |
| 43 | +cpp-linter --version="$llvm" --style=file --tidy-checks='' \ |
| 44 | + --files-changed-only=true --diff-base="$base" \ |
| 45 | + --summary-output-file=cpp-linter.md 2>&1 | tee cpp-linter.log |
| 46 | + |
| 47 | +# cpp-linter exits with 0 when it finds problems, so fail the job here. |
| 48 | +grep -q ':0 checks-failed$' cpp-linter.log |
| 49 | +``` |
| 50 | + |
| 51 | +- The script needs Python 3.11 or later, git and a full clone: `git merge-base` cannot find the |
| 52 | + point where your branch started in a shallow clone. Each job below turns shallow cloning off. |
| 53 | +- `clang-tools` downloads static clang-format and clang-tidy binaries into the virtual |
| 54 | + environment, with clang's builtin headers. Nothing is installed outside `.venv`. |
| 55 | +- `--style=file` and `--tidy-checks=''` use your `.clang-format` and `.clang-tidy`. |
| 56 | +- `--files-changed-only=true` checks every line of the files the branch changes. Add |
| 57 | + `--lines-changed-only=true` to report only on the changed lines. |
| 58 | +- clang-tidy needs a `compile_commands.json` to find your include paths and flags. Build it |
| 59 | + before the script runs and add `--database=build` (the directory that holds it). |
| 60 | +- `'cpp-linter<2'` keeps the Python package. Version 2, a rewrite in Rust, is in release |
| 61 | + candidates and changes some options. |
| 62 | + |
| 63 | +Run it on your machine first, from a branch of your repository: |
| 64 | + |
| 65 | +```bash |
| 66 | +bash ci/cpp-linter.sh main |
| 67 | +``` |
| 68 | + |
| 69 | +## GitLab CI |
| 70 | + |
| 71 | +```yaml title=".gitlab-ci.yml" |
| 72 | +cpp-linter: |
| 73 | + image: python:3.13 |
| 74 | + rules: |
| 75 | + - if: $CI_PIPELINE_SOURCE == "merge_request_event" |
| 76 | + variables: |
| 77 | + GIT_DEPTH: 0 |
| 78 | + script: |
| 79 | + - bash ci/cpp-linter.sh "$CI_MERGE_REQUEST_TARGET_BRANCH_NAME" |
| 80 | + artifacts: |
| 81 | + when: always |
| 82 | + paths: |
| 83 | + - cpp-linter.md |
| 84 | +``` |
| 85 | +
|
| 86 | +## Jenkins |
| 87 | +
|
| 88 | +For a multibranch pipeline, where Jenkins sets `CHANGE_TARGET` on pull request builds: |
| 89 | + |
| 90 | +```groovy title="Jenkinsfile" |
| 91 | +pipeline { |
| 92 | + agent { docker { image 'python:3.13' } } |
| 93 | + stages { |
| 94 | + stage('cpp-linter') { |
| 95 | + when { changeRequest() } |
| 96 | + steps { |
| 97 | + sh 'bash ci/cpp-linter.sh "$CHANGE_TARGET"' |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + post { |
| 102 | + always { |
| 103 | + archiveArtifacts artifacts: 'cpp-linter.md', allowEmptyArchive: true |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +## Azure Pipelines |
| 110 | + |
| 111 | +```yaml title="azure-pipelines.yml" |
| 112 | +pr: |
| 113 | + - main |
| 114 | +
|
| 115 | +pool: |
| 116 | + vmImage: ubuntu-latest |
| 117 | +
|
| 118 | +steps: |
| 119 | + - checkout: self |
| 120 | + fetchDepth: 0 |
| 121 | + - task: UsePythonVersion@0 |
| 122 | + inputs: |
| 123 | + versionSpec: '3.13' |
| 124 | + - script: bash ci/cpp-linter.sh "$(System.PullRequest.targetBranchName)" |
| 125 | + displayName: cpp-linter |
| 126 | + condition: eq(variables['Build.Reason'], 'PullRequest') |
| 127 | + - publish: cpp-linter.md |
| 128 | + artifact: cpp-linter |
| 129 | + condition: and(always(), eq(variables['Build.Reason'], 'PullRequest')) |
| 130 | +``` |
| 131 | + |
| 132 | +The `pr` trigger works for repositories on GitHub and Bitbucket Cloud. For a repository in Azure |
| 133 | +Repos, start the pipeline from a branch policy's build validation instead. |
| 134 | + |
| 135 | +## Bitbucket Pipelines |
| 136 | + |
| 137 | +```yaml title="bitbucket-pipelines.yml" |
| 138 | +image: python:3.13 |
| 139 | +
|
| 140 | +clone: |
| 141 | + depth: full |
| 142 | +
|
| 143 | +pipelines: |
| 144 | + pull-requests: |
| 145 | + '**': |
| 146 | + - step: |
| 147 | + name: cpp-linter |
| 148 | + script: |
| 149 | + - bash ci/cpp-linter.sh "$BITBUCKET_PR_DESTINATION_BRANCH" |
| 150 | + artifacts: |
| 151 | + - cpp-linter.md |
| 152 | +``` |
| 153 | + |
| 154 | +## What was tested |
| 155 | + |
| 156 | +The script was run as written, on Linux, with cpp-linter 1.16.0 and LLVM 21: it fails on a branch |
| 157 | +with findings and passes on a clean one. The four job definitions use each system's documented |
| 158 | +variables for the target branch, but were not run on those systems. If one does not work for |
| 159 | +you, [open an issue](https://github.com/cpp-linter/cpp-linter.github.io/issues). |
| 160 | + |
| 161 | +## Only clang-format, without Python |
| 162 | + |
| 163 | +The [Docker images](https://github.com/cpp-linter/clang-tools-docker) hold the clang tools and |
| 164 | +nothing else to install. This GitLab job checks the formatting of every source file under `src`: |
| 165 | + |
| 166 | +```yaml title=".gitlab-ci.yml" |
| 167 | +clang-format: |
| 168 | + image: ghcr.io/cpp-linter/clang-tools:21 |
| 169 | + script: |
| 170 | + - find src -name '*.cpp' -o -name '*.h' | xargs -r clang-format --dry-run --Werror |
| 171 | +``` |
0 commit comments