Sonarqube code coverage is 0% though scan was successful in Github Action for php app

1.1k Views Asked by At

I have a github action which runs the php unit test followed by Sonarqube scanner but the Sonarqube code coveage is always 0%

Phpunit Test is ok Sonaiqube ok as well but no CodeCoverage

These is my Github action script eliminated some jobs related to unitest in here :

name: front-data-stage-unittest
on:
  pull_request:
    branches: [ master ]

jobs:
  Test:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        php: ['8.1']
    name: PHP ${{ matrix.php }}

    steps:
      - name: Checkout repository and submodules
        uses: actions/checkout@v3
        with:
          submodules: recursive
          token: ${{ secrets.SUBMODULE_TOKEN }}
      - name: Install PHP
        uses: shivammathur/setup-php@master
        with:
          php-version: ${{ matrix.php }}
          extensions: mbstring, dom, fileinfo, mysql
          coverage: xdebug

      - uses: php-actions/composer@v5
        with:
          php_version: 8.1
          args: --profile --ignore-platform-reqs --optimize-autoloader

      - name: Execute PHPUnit tests
        run: vendor/bin/phpunit --coverage-clover=coverage.xml

      - name: SonarQube Scan
        uses: SonarSource/sonarqube-scan-action@master
        with:
          args: >
            -Dsonar.php.coverage.reportPaths=coverage.xml
        env:
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
          SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}

sonar-project.properties:

sonar.projectKey=tara_app
sonar.php.coverage.reportPaths=coverage.xml

Possible cause: looking at the logs looks like its not finding the file but anyone worked on generating or making it work via github action

14:06:11.466 INFO: 1157/1193 files analyzed, current file: app/Http/Controllers/V2/PerformanceController.php
14:06:13.823 INFO: 1193/1193 source files have been analyzed
14:06:13.825 WARN: PHPUnit xml test report not found: tests/report/test.xml
14:06:13.826 INFO: No PHPUnit coverage reports provided (see 'sonar.php.coverage.reportPaths' property)
14:06:13.826 INFO: Sensor PHP sensor [php] (done) | time=88263ms
14:06:13.826 INFO: Sensor Analyzer for "php.ini" files [php]
1

There are 1 best solutions below

2
On

The filesystem is not preserved between jobs. Thus your reports generated from your Test jobs are not available to your run-sonarqube job.

If you want to share files between jobs you need to use artifacts.

After your phpunit step add a step using actions/upload-artifact and then add a step before your scan using actions/download-artifact to pull the report into that job.