Skip to content

SonarQube Scan for C and C++

Actions
Scan your C and C++ code with SonarQube to detect bugs, vulnerabilities and code smells
v2.0.0
Latest
Verified creator
Star (2)

Verified

GitHub has manually verified the creator of the action as an official partner organization. For more info see About badges in GitHub Marketplace.

Scan your C, C++, and Objective-C code with SonarQube Server Tests

Warning

This action is deprecated and will be removed in a future release.

Please use the sonarqube-scan-action and its install-build-wrapper sub-action instead.

More specifically, if the action is used to install both the SonarScanner CLI and the Build Wrapper:

  • replace sonarqube-github-c-cpp with the latest version of SonarSource/sonarqube-scan-action/install-build-wrapper
  • if the step calling the action is named Install sonar-scanner and build-wrapper, or something similar, rename it to Install Build Wrapper
  • replace the step calling sonar-scanner with a step using SonarSource/sonarqube-scan-action
  • the arguments passed to sonar-scanner should be passed to the action via the args input parameter

If the action is used to install the SonarScanner CLI, and the Build Wrapper is not required:

  • remove the sonarqube-github-c-cpp step altogether
  • replace the step calling sonar-scanner with a step using SonarSource/sonarqube-scan-action
  • the arguments passed to sonar-scanner should be passed to the action via the args input parameter

Check the C++ section in the README of the sonarqube-scan-action for complete examples.

This SonarSource project, available as a GitHub Action, scans your C, C++, and Objective-C projects with SonarQube Server.

Logo Logo

SonarQube Server is a widely used static analysis solution for continuous code quality and security inspection.

It helps developers detect coding issues in 30+ languages, frameworks, and IaC platforms, including Java, JavaScript, TypeScript, C#, Python, C, C++, and many more.

The solution also provides fix recommendations leveraging AI with Sonar's AI CodeFix capability.

Requirements

To run an analysis on your code, you first need to set up your project on SonarQube Server. Your SonarQube Server instance must be accessible from GitHub, and you will need an access token to run the analysis (more information below under Environment variables).

Read more information on how to analyze your code here.

Usage

sonar.projectKey=<replace with the key generated when setting up the project on SonarQube Server>

# relative paths to source directories. More details and properties are described
# at https://docs.sonarsource.com/sonarqube-server/latest/project-administration/analysis-scope/ 
sonar.sources=.

The workflow, usually declared under .github/workflows, looks like the following:

on:
  # Trigger analysis when pushing to your main branches, and when creating a pull request.
  push:
    branches:
      - main
      - master
      - develop
      - 'releases/**'
  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@<action version> # Ex: v4.0.0, See the latest version at https://github.com/marketplace/actions/sonarqube-scan-for-c-and-c
    - name: Run build-wrapper
      run: |
        # here goes your compilation wrapped with build-wrapper; See https://docs.sonarsource.com/sonarqube-cloud/advanced-setup/languages/c-family/overview/#analysis-steps-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:
        SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
      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 Server 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 Server 10.6, like this:

run: sonar-scanner --define sonar.cfamily.compile-commands="${{ env.BUILD_WRAPPER_OUT_DIR }}/compile_commands.json"

See also example configurations of C++ projects for SonarQube Server.

Action parameters

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@<action version>
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@<action version>
with:
  cache-binaries: false

See also example configurations

Environment variables

  • SONAR_TOKENRequired this is the token used to authenticate access to SonarQube. You can read more about security tokens in the documentation. You can set the SONAR_TOKEN environment variable in the "Secrets" settings page of your repository, or you can add them at the level of your GitHub organization (recommended). automating-your-workflow-with-github-actions/authenticating-with-the-github_token)).*
  • SONAR_HOST_URL – this tells the scanner where SonarQube Server is hosted. You can set the SONAR_HOST_URL environment variable in the "Variables" settings page of your repository, or you can add them at the level of your GitHub organization (recommended).
  • SONAR_ROOT_CERT – Holds an additional certificate (in PEM format) that is used to validate the certificate of SonarQube Server or of a secured proxy to it. You can set the SONAR_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).

Here is an example of how you can pass a certificate (in PEM format) to the Scanner truststore:

- uses: sonarsource/sonarqube-github-c-cpp@<action version>
  env:
    SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
    SONAR_ROOT_CERT: ${{ secrets.SONAR_ROOT_CERT }}

If your source code file names contain special characters that are not covered by the locale range of en_US.UTF-8, you can configure your desired locale like this:

- uses: sonarsource/sonarqube-github-c-cpp@<action version>
  env:
    SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
    LC_ALL: "ru_RU.UTF-8"

Do not use this GitHub action if you are in the following situations

Additional information

This action installs coreutils if run on macOS.

Have question or feedback?

To provide feedback (requesting a feature or reporting a bug) please post on the SonarSource Community Forum.

License

The action file and associated scripts and documentation in this project are released under the LGPLv3 License.

SonarQube Scan for C and C++ is not certified by GitHub. It is provided by a third-party and is governed by separate terms of service, privacy policy, and support documentation.

About

Scan your C and C++ code with SonarQube to detect bugs, vulnerabilities and code smells
v2.0.0
Latest

Verified

GitHub has manually verified the creator of the action as an official partner organization. For more info see About badges in GitHub Marketplace.

SonarQube Scan for C and C++ is not certified by GitHub. It is provided by a third-party and is governed by separate terms of service, privacy policy, and support documentation.