Giter Club home page Giter Club logo

sonarqube-scan-action's Introduction

Scan your code with SonarQube QA

This SonarSource project, available as a GitHub Action, scans your projects with SonarQube, and helps developers produce Clean Code.

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.

Requirements

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 an access token to run the analysis (more information below under Environment variables).

Read more information on how to analyze your code here.

Usage

Project metadata, including the location of 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
# at https://docs.sonarqube.org/latest/project-administration/narrowing-the-focus/ 
sonar.sources=.

The workflow YAML file will usually look something like this:

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
    steps:
    - uses: actions/checkout@v4
      with:
        # Disabling shallow clones is recommended for improving the relevancy of reporting
        fetch-depth: 0
    - name: SonarQube Scan
      uses: sonarsource/sonarqube-scan-action@<action version> # Ex: v2.1.0, See the latest version at https://github.com/marketplace/actions/official-sonarqube-scan
      env:
        SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
        SONAR_HOST_URL: ${{ vars.SONAR_HOST_URL }}

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:

    - name: SonarQube Scan
      uses: sonarsource/sonarqube-scan-action@<action version> # Ex: v2.1.0, See the latest version at https://github.com/marketplace/actions/official-sonarqube-scan
      env:
        SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
        SONAR_HOST_URL: ${{ vars.SONAR_HOST_URL }}
        LC_ALL: "ru_RU.UTF-8"

If your SonarQube server uses a self-signed certificate, you can pass a root certificate (in PEM format) to the Java certificate store:

    - name: SonarQube Scan
      uses: sonarsource/sonarqube-scan-action@<action version> # Ex: v2.1.0, See the latest version at https://github.com/marketplace/actions/official-sonarqube-scan
      env:
        SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
        SONAR_HOST_URL: ${{ vars.SONAR_HOST_URL }}
        SONAR_ROOT_CERT: ${{ secrets.SONAR_ROOT_CERT }}

You can change the analysis base directory by using the optional input projectBaseDir like this:

- name: SonarQube Scan
  uses: sonarsource/sonarqube-scan-action@<action version> # Ex: v2.1.0, See the latest version at https://github.com/marketplace/actions/official-sonarqube-scan
  with:
    projectBaseDir: app/src

In case you need to add additional analysis parameters, and you do not wish to set them in the sonar-project.properties file, you can use the args option:

- name: SonarQube Scan
  uses: sonarsource/sonarqube-scan-action@<action version> # Ex: v2.1.0, See the latest version at https://github.com/marketplace/actions/official-sonarqube-scan
  with:
    projectBaseDir: app/src
    args: >
      -Dsonar.python.coverage.reportPaths=coverage.xml
      -Dsonar.tests=tests/
      -Dsonar.verbose=true

More information about possible analysis parameters can be found in the documentation.

Environment variables

  • SONAR_TOKENRequired this is the token used to authenticate access to SonarQube. You can read more about security tokens here. 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).
  • SONAR_HOST_URLRequired this tells the scanner where SonarQube 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 root certificate (in PEM format) that is used to validate the SonarQube server certificate. 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).

Alternatives for Java, .NET, and C/C++ projects

This GitHub Action will not work for all technologies. If you are in one of the following situations, you should use the following alternatives:

  • Your code is built with Maven. Read the documentation about our Scanner for Maven.
  • Your code is built with Gradle. Read the documentation about our Scanner for Gradle.
  • You want to analyze a .NET solution. Read the documentation about our Scanner for .NET.
  • You want to analyze C or C++ code. Starting from SonarQube 10.6, this GitHub Action will scan C and C++ out of the box. If you want to have better control over the scan configuration/setup, you can switch to the SonarQube C and C++ GitHub Action.

Error cleaning up workspace

In some cases, the checkout action may fail to clean up the workspace. This is a known problem for GitHub actions implemented as a docker container (such as sonarqube-scan-action) when self-hosted runners are used. Example of the error message: File was unable to be removed Error: EACCES: permission denied, unlink '/actions-runner/_work//project/.scannerwork/.sonar_lock' To work around the problem, sonarqube-scan-action attempts to fix the permission of the temporary files that it creates. If that doesn't work, you can manually clean up the workspace by running the following action:

- name: Clean the workspace
  uses: docker://alpine
  with:
    args: /bin/sh -c "find \"${GITHUB_WORKSPACE}\" -mindepth 1 ! -name . -prune -exec rm -rf {} +"

You can find more info here.

Have questions or feedback?

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

License

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

Container images built with this project include third-party materials.

sonarqube-scan-action's People

Contributors

7ph avatar aeolun avatar alx99 avatar antoine-vinot-sonarsource avatar belen-pruvost-sonarsource avatar bsvobodny avatar claire-villard-sonarsource avatar coliff avatar csaba-feher-sonarsource avatar david-cho-lerat-sonarsource avatar dependabot[bot] avatar geoffray-adde-sonarsource avatar henryju avatar istvan-bodo-sonarsource avatar jacek-poreda-sonarsource avatar julien-carsique-sonarsource avatar kbdharun avatar lukasz-jarocki-sonarsource avatar matteo-mara-sonarsource avatar mdvorak avatar njannasch avatar philippe-perrin-sonarsource avatar sylvain-combe-sonarsource avatar tobias-trabelsi-sonarsource avatar tomverin avatar wouter-admiraal-sonarsource avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.