GitHub Action
SonarQube Scan for C and C++
This SonarSource project, available as a GitHub Action, sets up the tools needed to configure and run scans of C, C++, and Objective-C projects with SonarQube.
SonarQube is a widely used static analysis solution for continuous code quality and security inspection. It helps developers identify and fix issues in their code that could lead to bugs, vulnerabilities, or decreased development velocity. SonarQube supports the most popular programming languages, including Java, JavaScript, TypeScript, C#, Python, C, C++, and many more.
To run an analysis on your code, you first need to set up your project on SonarQube. Your SonarQube instance must be accessible from GitHub, and you will need a Project analysis token or a Global analysis token to run the analysis (more information below under Environment variables).
Read more information on how to analyze your code here.
Project metadata, including the location to the sources to be analyzed, must be declared in the file sonar-project.properties
in the base directory:
sonar.projectKey=<replace with the key generated when setting up the project on SonarQube>
# relative paths to source directories. More details and properties are described
# in https://docs.sonarsource.com/sonarqube/latest/project-administration/analysis-scope/
sonar.sources=.
The workflow, usually declared in .github/workflows/build.yml
, looks like:
on:
# Trigger analysis when pushing in master or pull requests, and when creating
# a pull request.
push:
branches:
- master
pull_request:
types: [opened, synchronize, reopened]
name: Main Workflow
jobs:
sonarqube:
runs-on: ubuntu-latest
env:
BUILD_WRAPPER_OUT_DIR: build_wrapper_output_directory # Directory where build-wrapper output will be placed
steps:
- uses: actions/checkout@v4
with:
# Disabling shallow clone is recommended for improving relevancy of reporting
fetch-depth: 0
- name: Install sonar-scanner and build-wrapper
uses: sonarsource/sonarqube-github-c-cpp@v2
env:
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
SONAR_ROOT_CERT: ${{ secrets.SONAR_ROOT_CERT }}
- name: Run build-wrapper
run: |
# here goes your compilation wrapped with build-wrapper; See https://docs.sonarsource.com/sonarqube/latest/analyzing-source-code/languages/c-family/#using-build-wrapper for more information
# build-preparation steps
# build-wrapper-linux-x86-64 --out-dir ${{ env.BUILD_WRAPPER_OUT_DIR }} build-command
- name: Run sonar-scanner
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
run: sonar-scanner --define sonar.cfamily.compile-commands="${{ env.BUILD_WRAPPER_OUT_DIR }}/compile_commands.json" #Consult https://docs.sonarsource.com/sonarqube/latest/analyzing-source-code/scanners/sonarscanner/ for more information and options
If you are using SonarQube 10.5 or earlier, use sonar.cfamily.build-wrapper-output
instead of sonar.cfamily.compile-commands
in the run
property of the last step, as Build Wrapper does not generate a compile_commands.json file before SonarQube 10.6, like this:
run: sonar-scanner --define sonar.cfamily.build-wrapper-output="${{ env.BUILD_WRAPPER_OUT_DIR }}"
You can change the build-wrapper
and sonar-scanner
installation path by using the optional input installation-path
like this:
uses: sonarsource/sonarqube-github-c-cpp@v2
with:
installation-path: my/custom/directory/path
Also, the absolute paths to the installed build-wrapper and sonar-scanner binaries are returned as outputs from the action.
Moreover, by default the action will cache sonar-scanner installation. However, you can disable caching by using the optional input: cache-binaries
like this:
uses: sonarsource/sonarqube-github-c-cpp@v2
with:
cache-binaries: false
If your SonarQube server uses a self-signed certificate, you can pass a root certificate (in PEM format) to the java certificate store:
uses: sonarsource/sonarqube-github-c-cpp@v2
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
SONAR_ROOT_CERT: ${{ secrets.SONAR_ROOT_CERT }}
See also example configurations
Following secrets are required for successful invocation of sonar-scanner:
SONAR_TOKEN
– Required this is the token used to authenticate access to SonarQube. You can read more about security tokens here. You can set theSONAR_TOKEN
environment variable in the "Secrets" settings page of your repository, or you can add them at the level of your GitHub organization (recommended).GITHUB_TOKEN
– Provided by Github (see Authenticating with the GITHUB_TOKEN).
Environment variables:
SONAR_HOST_URL
– Required this tells the scanner where SonarQube is hosted. You can set theSONAR_HOST_URL
environment variable in the "Secrets" settings page of your repository, or you can add them at the level of your GitHub organization (recommended).SONAR_ROOT_CERT
– Holds an additional root certificate (in PEM format) that is used to validate the SonarQube server certificate. You can set theSONAR_ROOT_CERT
environment variable in the "Secrets" settings page of your repository, or you can add them at the level of your GitHub organization (recommended).
- You want to analyze code that doesn't have C, C++, or Objective-C? Use the SonarQube Scan GitHub Action instead
- You want to run the action on a 32-bits system - build wrappers support only 64-bits OS
This action installs coreutils
if run on macOS
To provide feedback (requesting a feature or reporting a bug) please post on the SonarSource Community Forum with the tag sonarqube
.
The action file and associated scripts and documentation in this project are released under the LGPLv3 License.