CI/CD Integration
CppDepend in GitLab CI/CD
Analyze your C/C++ code on every merge request, fail the pipeline on new critical issues, and publish the HTML report where the team reads it.
How it works
GitLab only has to start the CppDepend command line tool. It reads the .cdproj project, analyzes the code, evaluates the rules and quality gates, and writes the results and the HTML report to an output folder. The exit code tells GitLab whether the gates passed.
Prerequisites
- A GitLab Runner that can run CppDepend.
CppDepend.Console.exeis a Windows executable, so use a Windows runner (shell executor with PowerShell is the simplest). - CppDepend installed on that runner, for example in
C:\Program Files\CppDepend, with a valid license for CI use. - A
.cdprojproject file committed to the repository (see the next step). - Runner tags so that only the right machines pick the job, for example
windowsandcppdepend.
1 Create the project file
Create the project once in the CppDepend GUI (VisualCppDepend): choose the source folders, the build outputs and the rules, then save the .cdproj. Commit it next to your sources, for example at the root of the repository.
- The path of the project file given on the command line must be absolute (relative paths are not supported). In the pipeline, build it from
$CI_PROJECT_DIR. - If the paths stored in the project differ from the runner workspace, override the source location with
/InDirsand the output location with/OutDir. - The GUI Launchpad also has a GitLab CI card that generates a starter job. Use it as a starting point and compare with the job below.
2 The pipeline job
Add this to .gitlab-ci.yml. It runs the analysis, keeps the whole output folder as an artifact, and stops the job with the exit code of CppDepend.
stages:
- analyze
- publish
variables:
CPPDEPEND_EXE: 'C:\Program Files\CppDepend\CppDepend.Console.exe'
PROJECT_FILE: '$CI_PROJECT_DIR\MyApp.cdproj'
OUT_DIR: '$CI_PROJECT_DIR\CppDependOut'
cppdepend:
stage: analyze
tags: [windows, cppdepend]
script:
- '& "$env:CPPDEPEND_EXE" "$env:PROJECT_FILE" /InDirs "$env:CI_PROJECT_DIR" /OutDir "$env:OUT_DIR" /Silent'
- 'if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }'
artifacts:
when: always # keep the report even when a quality gate fails
expire_in: 30 days
paths:
- CppDependOut/
expose_as: 'CppDepend report'when: alwaysThe report is most useful when the job fails. Without it GitLab drops the artifacts of a failed job.3 Fail the pipeline on quality gates
CppDepend returns zero on success and a non-zero code when a quality gate fails, so GitLab fails the job without extra work. Define the gates in the project, for example no new critical issues and coverage must not decrease, so legacy issues do not block the team.
Warn instead of failing
To mark the job as a warning (orange) while the team is getting used to the gates, allow the gate exit code:
cppdepend:
allow_failure:
exit_codes: 1$LASTEXITCODE in the job log, and use that value in exit_codes. Do not ignore every exit code: that also hides real analysis errors.4 Publish the report
Option A: job artifacts
With expose_as set (see step 2), merge requests show a CppDepend report link that opens the artifact browser. Download the folder or browse to the HTML report.
Option B: GitLab Pages for the default branch
Publish the report of the main branch as a small internal site that anyone with access can open.
pages:
stage: publish
tags: [windows]
needs: [cppdepend]
script:
- 'New-Item -ItemType Directory -Force public | Out-Null'
- 'Copy-Item -Recurse -Force CppDependOut\* public'
artifacts:
paths:
- public
rules:
- if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'Custom reports
Custom reports created with the Report Designer are written at the end of the analysis into the output folder (ReportTemplates and the Report_<name>.html files). They are included by the artifact path above.
5 Run on merge requests
Analyze merge request pipelines and the default branch, and skip everything else, to keep runner time low:
workflow:
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
- if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'Combined with quality gates that look at new issues only, a merge request is blocked only by what the change itself introduced.
6 Baseline and trends
CppDepend separates new issues from the legacy baseline and shows trends against it, which needs the results of a previous analysis. A fresh GitLab workspace does not have them, so keep them between pipelines:
- Persistent output folder on the runner. Pass a fixed path such as
D:\cppdepend\myappto/OutDir. Every run then compares with the previous result. Simple, but one runner machine is shared by all branches. - Artifact of the default branch. Download the results of the last successful default-branch pipeline into
OUT_DIRbefore the analysis (jobneedswithartifacts: truefrom a scheduled pipeline, or the jobs API).
Security
- Store license keys and tokens as masked, protected CI/CD variables. Never commit them to the repository or print them in the job log.
- Run the job on runners you control. The analysis reads the full source tree.
- Artifacts and Pages contain code information: set expiry (
expire_in) and visibility on purpose.
Console options
First argument: the absolute path of the .cdproj file, in double quotes when it contains spaces.
| Option | Effect |
|---|---|
/Silent | No console output and no calls to Console.Read(). Recommended in CI. |
/OutDir dir | Overrides the output folder of the project. Absolute path. |
/InDirs dir | Overrides the source location, for example the GitLab workspace. |
/FullAnalysis | Runs the full analysis. |
/LogTrendMetrics | Forces the logging of trend metrics. |
/XslForReport file | Builds the report with your own XSL file. Absolute path. |
/EmitVisualNDependBinXml | Writes VisualCppDepend.bin.xml, an XML file of the dependency graph. |
/ViewReport | Opens the HTML report. For local runs, not for CI. |
/HideConsole | Hides the console window. |
/Help | Prints the help. |
Troubleshooting
| Symptom | Likely cause and fix |
|---|---|
| Job stays pending | No runner with the windows / cppdepend tags is online. Check Settings, CI/CD, Runners. |
| Project not found | The project path must be absolute. Build it from $CI_PROJECT_DIR, and quote it. |
| Analysis finds no code | Paths stored in the project do not exist in the runner workspace. Pass /InDirs. |
| Job fails but the report looks fine | A quality gate failed. Open the Quality Gates page of the report. Use allow_failure: exit_codes only if you accept it. |
| No report in the merge request | Artifacts need when: always for failed jobs and a path that exists: check that /OutDir and artifacts:paths match. |
| No trends, everything is new | The previous results are not available in the workspace. See Baseline and trends. |
| Backslash problems in the YAML | Use single quotes around Windows paths and PowerShell commands, as in the examples. |
Thanks for your feedback. Tell us more or request a feature.
