asp.net-coresonarqubegithub-actionstest-coveragequality-gate

Github Actions add quality gate for test results


Net core and github actions. I have pipeline running in Github actions and I have integrated Sonarqube. My requirement is if the code coverage is less than 70 or any specified number then we should not allow to continue the build process. Build should stop here. So my current code is

SonarQube_API:
    runs-on: windows-latest
    permissions:
      contents: read
      packages: write
    steps:
      - name: Checkout repository
        uses: actions/checkout@v2
        
      - name: Set up Java for Sonar
        uses: actions/setup-java@v3
        with:
          distribution: 'temurin'
          java-version: '17'
          
      - name: Get SonarQube
        shell: pwsh
        run: dotnet tool install --global dotnet-sonarscanner

      - name: Get Coverlet.MSBuild
        shell: pwsh
        run: dotnet add package coverlet.msbuild
        working-directory: '.\JK.Test' 

      - name: .NET sonarscanner begin
        shell: pwsh
        run: |
          dotnet sonarscanner begin `
          /k:"JK-Api" `
          /d:sonar.host.url="${{ secrets.MDN_SONARQUBE_HOST }}" `
          /d:sonar.login="${{ secrets.MDN_SONARQUBE_TOKEN }}" `
          /d:sonar.cs.opencover.reportsPaths=".\JK.Test\coverage.opencover.xml" `
          /v:"1.0"
          
        
      - name: build
        shell: pwsh
        run: |
          dotnet restore JK.Test/JK.Test.csproj
          dotnet build JK.Test/JK.Test.csproj --configuration '${{ env.BUILD_CONFIG }}' --no-restore

      - name: dotnet test
        shell: pwsh
        run: |
          dotnet test ".\JK.Test\JK.Test.csproj" `
            --configuration Release `
            --results-directory "${{ github.workspace }}\TestResults" `
            /p:CollectCoverage=true `
        env:
          CollectCoverage: true
          CoverletOutputFormat: opencover

      - name: .NET sonarscanner end
        shell: pwsh
        run: dotnet sonarscanner end /d:sonar.login="${{ secrets.MDN_SONARQUBE_TOKEN }}"

I am not able to get any informations for my requirement. My requirement is if the code coverage is below for example 70 percent then i would like to fail the build or process should be terminated here itself. Can someone please help me on this. Any help would be appreciated. Thanks you


Solution

  • The quality gate value is configured on the SonarQube UI for the project.

    The sonarqube-quality-gate-action sends the report and wait for the feedback from the platform to fail the pipeline or not. Reference

    On the SonarQube platform, if you have access to the project page and the project configuration, you should be able to manage the quality gate through these kinds of setting options:

    Reference

    Note that you also will need to create a sonar-project.properties file in your project. Reference

    If you have multiple projects in the same repository, you can add different sonar-project.properties file, one for each project (ideally one in each project module package).