Skip to content

Integrate SonarCloud code analysis to GitHub Actions when build wrapper or relative paths support is required. Use https://github.com/SonarSource/sonarcloud-github-action otherwise

License

Notifications You must be signed in to change notification settings

leonardocarreras/sonarcloud-github-c-cpp

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Analyze your code for free with SonarCloud

This SonarSource project, available as a GitHub Action, helps you configure and scan your C, C++, and Objective-C projects with SonarCloud, to produce Clean Code.

SonarCloud 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. SonarCloud supports the most popular programming languages, including Java, JavaScript, TypeScript, C#, Python, C, C++, and many more.

Requirements

  • Create your account on SonarCloud. Sign up for free now if it's not already the case! SonarCloud Sign Up
  • The repository to analyze is set up on SonarCloud. Set it up in just one click.

Usage

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.organization=<replace with your SonarCloud organization key>
sonar.projectKey=<replace with the key generated when setting up the project on SonarCloud>

# relative paths to source directories. More details and properties are described
# in https://sonarcloud.io/documentation/project-administration/narrowing-the-focus/
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:
  sonarcloud:
    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@v3
      with:
        # Disabling shallow clone is recommended for improving relevancy of reporting
        fetch-depth: 0
    - name: Install sonar-scanner and build-wrapper
      uses: sonarsource/sonarcloud-github-c-cpp@v2
    - name: Run build-wrapper
      run: |
      #here goes your compilation wrapped with build-wrapper; See https://docs.sonarcloud.io/advanced-setup/languages/c-c-objective-c/#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:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
      run: sonar-scanner --define sonar.cfamily.build-wrapper-output="${{ env.BUILD_WRAPPER_OUT_DIR }}" #Consult https://docs.sonarcloud.io/advanced-setup/ci-based-analysis/sonarscanner-cli/ for more information and options

You can change the build-wrapper and sonar-scanner installation path by using the optional input installation-path like this:

uses: sonarsource/sonarcloud-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/sonarcloud-github-c-cpp@v2
with:
  cache-binaries: false

See also example configurations

Secrets

Following secrets are required for successful invocation of sonar-scanner:

  • SONAR_TOKENRequired this is the token used to authenticate access to SonarCloud. You can generate a token on your Security page in SonarCloud. You can set the SONAR_TOKEN environment variable in the "Secrets" settings page of your repository.
  • GITHUB_TOKEN – Provided by Github (see Authenticating with the GITHUB_TOKEN).

Example of pull request analysis

Want to see more examples of SonarCloud in action? You can explore current Open Source projects in SonarCloud that are using the Clean as You Code methodology.

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

  • You want to analyze code written in a language other than C or C++. Use SonarCloud GitHub Action instead
  • You want to run the action on a 32-bits system - build wrappers support only 64-bits OS

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 with the tag sonarcloud.

License

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

About

Integrate SonarCloud code analysis to GitHub Actions when build wrapper or relative paths support is required. Use https://github.com/SonarSource/sonarcloud-github-action otherwise

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Shell 100.0%