Set SonarCloud on GitHub actions

So at the moment we are using SonarCloud with our CI/CD pipeline in bitbucket, it has worked well, however we are migrating to GitHub wich uses GitHub actions. I have followed the poorly documented steps to use SonarCloud alas the coverage it’s marked as 0.0% even with the uni tests been performed.

This is the script for it:

name: Python Tests
on:
  # Trigger regressions tests when pushing in develop or pull requests, 
  # and when creating a pull request.
  push:
    branches:
      - master
      - develop
  pull_request:
      types: [opened, synchronize, reopened]
jobs:
  tests:
    runs-on: ubuntu-latest
    strategy:
      max-parallel: 4
      matrix:
        python-version: [3.8]
    steps:
      - uses: actions/checkout@v2
      - name: Install Dependencies
        env: 
          ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
          IMAGE_TAG: ${{ github.sha }}
        run: |
          docker-compose -f ci.yml build
      - name: Run Tests   
        run: |
          mkdir -p test-reports
          docker-compose -f ci.yml up -d db elasticsearch redis
          docker-compose -f ci.yml run django coverage run -m pytest > test-reports/cover_report.xml
          docker-compose -f ci.yml run django coverage report || true
          docker-compose -f ci.yml run django coverage html || true
          docker-compose -f ci.yml run django coverage xml || true
      - uses: actions/upload-artifact@v3
        with:
          name: test-reports
          path: test-reports/cover_report.xml
  Pylint:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: [3.8]
    steps:
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install pylint
      - name: Analysing the code with pylint
        run: |
          python -m pylint --fail-under=10 `find -regextype egrep -regex '(.*.py)$'` |
          tee pylint.txt
  sonarcloud:
    runs-on: ubuntu-latest
    needs: tests
    steps:
    - uses: actions/checkout@v2
      with:
        # Disabling shallow clone is recommended for improving relevancy of reporting
        fetch-depth: 0
    - uses: actions/download-artifact@v3
      with:
        name: test-reports
    - name: SonarCloud Scan
      uses: sonarsource/sonarcloud-github-action@master
      with:
        args: >
          -Dsonar.organization=<company_name>
          -Dsonar.projectKey=<company_key>
          -Dsonar.sources=path
          -Dsonar.exclusions=path/migrations/**
          -Dsonar.tests=tests
          -Dsonar.sourceEncoding=UTF-8
          -Dsonar.python.version=3.8
          -Dsonar.dynamicAnalysis=reuseReports
          -Dsonar.python.coverage.reportPaths=cover_xml/*coverage*xml
          -Dsonar.python.xunit.reportPath=test-reports/*cover_report*xml
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

Can someone please help me, What I’m missing? Why is the coverage on SonarCloud been marked as 0.0% when it should be at least 70.0%?

Hey there.

In the scanner logs, you should find information in the logs regarding the processing of your coverage reports, like below:

INFO: Sensor Cobertura Sensor for Python coverage [python]
INFO: Python test coverage
INFO: Parsing report ‘/github/workspace/test-coverage.xml’
WARN: Invalid directory path in ‘source’ element: /home/runner/work/my-repo/my-repo
INFO: Sensor Cobertura Sensor for Python coverage [python] (done) | time=527ms

Anything interesting there? You might find more information adding -X to your arguments (which turns on DEBUG level logging)

Hi, I didn’t found anything interesting in my logs, but I did in those you posted.

This line ‘/github/workspace/test-coverage.xml’

I was missing the ‘‘/github/workspace/’ prior to the coverage.xml report

it’s working now.

Thank you