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

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]

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.

Related

SonarCloud CI can't find source files for Ruby / SimpleCov coverage

tl;dr - SonarCloud CI on GitHub actions warns that it can't find any of the source files with coverage reported, despite confirming that the files are in the docker filesystem at the path reported.
I have a Ruby / Rails app with rspec specs which produce coverage stats using SimpleCov and its JSON formatter (so my rails_helper.rb starts:
require 'simplecov'
require "simplecov_json_formatter"
SimpleCov.formatter = SimpleCov::Formatter::JSONFormatter
SimpleCov.start('rails') do
add_filter ['/channels/', '/jobs/', '/mailers/']
end
I have SonarCloud CI set up to scan using GitHub Actions, with the following sonar-project.properties in the root:
sonar.projectKey=asilano_my-app
sonar.organization=asilano
sonar.ruby.coverage.reportPaths=coverage/coverage.json
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
sonar.sources=app,lib
sonar.tests=spec
and the following GitHub workflow:
name: Test and Deploy
on:
pull_request:
types: [opened, synchronize, reopened]
branches:
- 'main'
- 'staging'
push:
branches:
- 'main'
- 'staging'
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
steps:
- uses: actions/checkout#v2
- uses: ruby/setup-ruby#v1
with:
bundler-cache: true
- name: Install PostgreSQL client
run: |
sudo apt-get -yqq install libpq-dev
- name: Build App
env:
PGHOST: localhost
PGUSER: postgres
PGPASSWORD: postgres
RAILS_ENV: test
RAILS_MASTER_KEY: ${{ secrets.TEST_MASTER_KEY }}
run: |
bin/rails db:setup
yarn install
- name: Run Tests
env:
PGHOST: localhost
PGUSER: postgres
PGPASSWORD: postgres
RAILS_ENV: test
RAILS_MASTER_KEY: ${{ secrets.TEST_MASTER_KEY }}
run: |
bundle exec rspec
- name: Where Am I?
run: |
head coverage/coverage.json
ls -l /home/runner/work/my-app/my-app/app/lib/some_file.rb
- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action#master
env:
GITHUB_TOKEN: ${{ secrets.SONAR_GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
(main and staging are both long-lasting branches in SonarCloud)
The Where Am I? step is to try and debug the problems I'm having. It shows that the top of coverage.json reads:
{
"meta": {
"simplecov_version": "0.21.2"
},
"coverage": {
"/home/runner/work/my-app/my-app/app/lib/some_file.rb": {
"lines": [
1,
1,
1,
and confirms via ls that the mentioned path exists:
-rw-r--r-- 1 runner docker 1729 Oct 24 08:15 /home/runner/work/my-app/my-app/app/lib/some_file.rb
However, the SonarCloud scan step warns that the coverage file mentions some_file.rb, but can't find it in the filesytem:
INFO: Sensor SimpleCov Sensor for Ruby coverage [ruby]
WARN: File '/home/runner/work/my-app/my-app/app/lib/some_file.rb' is present in coverage report but cannot be found in filesystem
...and then repeating for every file in the app.
Why not? Why can't the SonarCloud scanner find some_file.rb on the path reported in the coverage file, even though I've confirmed it's where it should be?
I had the same issue with GitHub actions, rails, and simplecov. You need to replace the paths generated by simplecov on coverage/coverage.json. To do this run this step before your sonarcloud scan. Also, you could check this post https://community.sonarsource.com/t/code-coverage-doesnt-work-with-github-action/16747
- name: Running rails tests
run: bundle exec rspec
- name: fix code coverage paths
working-directory: ./coverage
run: |
sed -i 's#'$GITHUB_WORKSPACE'#/github/workspace/#g' coverage.json
- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action#master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

How to set up a monorepo in SonarCloud with a Java + Angular application

I have developed an application where the backend is developed using Java language (with maven) and the frontend is using Angular language. I host both parts in one project in github.
Now I am interested on the use of SonarQube on SonarCloud. For this purpose, I am following the information gathered from the community sonarsource and the standard documentation from sonarcloud. The idea is to use GiHub Actions for analyzing the projects.
What I have created is a .github/workflows/build.yml on the root folder with the content:
name: Build
on:
push:
branches:
- master
pull_request:
types: [opened, synchronize, reopened]
jobs:
sonarcloud:
name: SonarCloud
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Set up JDK 11
uses: actions/setup-java#v1
with:
java-version: 11
- name: Cache SonarCloud packages
uses: actions/cache#v1
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Cache Maven packages
uses: actions/cache#v1
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
- name: Build and analyze
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=kendo-tournament-backend
with:
projectBaseDir: ./backend/
- name: SonarCloud Frontend Scan
uses: SonarSource/sonarcloud-github-action#master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
with:
projectBaseDir: ./frontend/
Where basically, I create some steps for executing the maven project, and another extra step to execute the frontend. On both of them, I included the projectBaseDir with the path to both projects' folders as specified here.
Also, as suggested on the documentation, I have included a sonar-project.properties on the root folder of the frontend folder with:
sonar.projectKey=kendo-tournament-frontend
sonar.organization=softwaremagico
# This is the name and version displayed in the SonarCloud UI.
#sonar.projectName=Kendo Tournament Manager Frontend
#sonar.projectVersion=1.0
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
#sonar.sources=.
# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8
And for the backend, I have updated the root pom.xml with the:
<properties>
<sonar.organization>softwaremagico</sonar.organization>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
</properties>
As required.
But, no analyses scan is launch for any of both projects. And SonarCloud looks like is ignoring the configuration.
Probably, something is missing but I cannot imagine what. What steps are needed to set up a monorepository correctly using Java and Angular in Github?
Ok, after the example obtained from here. The changes I have made are:
Two different workflows on github, one for backend and one for frontend. Not one workflow with all steps together.
Include two different sonar-project.properties. One inside the backend folder, and one inside the frontend folder. Now I have added the sonar.sources line as follows:
sonar.projectKey=kendo-tournament-backend
sonar.organization=softwaremagico
sonar.sources=.
That ensures that is only for this folder.
For launching CircleCi with Sonar (for backend) edit file .circleci/config.yml:
version: 2.1
jobs:
build:
docker:
- image: 'circleci/openjdk:11-jdk'
working_directory: ~/KendoTournamentManager/backend
steps:
- checkout:
path: ~/KendoTournamentManager
- run:
name: Analyze on SonarCloud
command: mvn verify sonar:sonar -Dsonar.projectKey=kendo-tournament-backend
workflows:
main:
jobs:
- build:
context: SonarCloud
And now seems working fine:

How to stop GitHub Action build when SonarQube scan fails

I have a scan step built into my GitHub Action build and that is working fine. I reach out to my company's SonarQub instance and the scan is initiated. The problem I am having is trying to stop a build if there is a failure. For the life of me I can't seem to find a way to do that. Also, when I watch the scan it appears as though the next steps might be happening before it finishes (not positive on that but thought I would mention it). Any ideas??
name: Build, test, & deploy
on: [push]
jobs:
sonarqube:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
# Disabling shallow clone is recommended for improving relevancy of reporting
fetch-depth: 0
# Triggering SonarQube analysis as results of it are required by Quality Gate check
- name: SonarQube Scan
uses: sonarsource/sonarqube-scan-action#master
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
- name: SonarQube Quality Gate check
uses: sonarsource/sonarqube-quality-gate-action#master
# Force to fail step after specific time
timeout-minutes: 5
env:
SONAR_TOKEN: ${{ secrets.ADAM_SONAR_TOKEN }}
build:
name: Project build & package
if: "!contains(github.even.head_commit.message, '[skip-ci]')"
runs-on: ubuntu-latest
env:
#environment var for this job
#### the rest of the build is below this area - I didn't think it was necessary to include
You should use needs in your build job:
build:
needs: sonarqube
name: Project build & package
You can find information here: https://docs.github.com/en/actions/using-jobs/using-jobs-in-a-workflow
The answer is using "needs": https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idneeds

Migrating to GitHub Actions from Travis keeps failing

I am currently completing a migration from travis CI to Github Actions CI however my build keeps failing when the container tries to connect to my test DB.
Its a very generic error
UNKNOWN_CODE_PLEASE_REPORT: An internal error has occurred. Please retry or report your issues.
at Handshake.Object.<anonymous>.Sequence._packetToError (node_modules/***/lib/protocol/sequences/Sequence.js:47:14)
at Handshake.Object.<anonymous>.Handshake.ErrorPacket (node_modules/***/lib/protocol/sequences/Handshake.js:123:18)
at Protocol.Object.<anonymous>.Protocol._parsePacket (node_modules/***/lib/protocol/Protocol.js:291:23)
at Parser._parsePacket (node_modules/***/lib/protocol/Parser.js:433:10)
at Parser.write (node_modules/***/lib/protocol/Parser.js:43:10)
at Protocol.Object.<anonymous>.Protocol.write (node_modules/***/lib/protocol/Protocol.js:38:16)
at Socket.<anonymous> (node_modules/***/lib/Connection.js:88:28)
at Socket.<anonymous> (node_modules/***/lib/Connection.js:526:10)
Additional Notes: My test DB is hosted on Azure (mysql server) and my tests execute fine on local and previously on travis. Project details: NodeJs project running TypeOrm for MySql.
This is what the current base version of my yml looks like:
on:
pull_request:
branches: [master, develop]
defaults:
run:
working-directory: ./Server
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Use Node.js
with:
node_version: 14.x
uses: actions/setup-node#v1
- name: Run tests
run: |
npm install
npx jest --coverage --config ./jest.config.js
env:
TEST_DB_NAME: ${{ secrets.TEST_DB_NAME }}
TEST_DB_PORT: ${{ secrets.TEST_DB_PORT }}
TEST_DB_TYPE: ${{ secrets.TEST_DB_TYPE }}
TEST_HOST: ${{ secrets.TEST_HOST }}
TEST_PASSWORD: ${{ secrets.TEST_PASSWORD }}
TEST_PORT: ${{ secrets.TEST_PORT }}
ACCESS_SECRET_KEY: ${{ secrets.ACCESS_SECRET_KEY }} ```
Answering my own question:
I was missing an extra env key/variable related to an external AP. This was causing the failure.

Github Actions, Python Coverage and Sonar Qube

I want to create a Github workflow that does the following:
test my code with pytest
trigger Sonar Qube Cloud to analyze to the code and show my test coverage!
As far as I understand, SonarQ needs a file coverage.xml to display the code coverage. This can be generated with
pytest --cov=./ --cov-report=xml --doctest-modules
According to this article coverage.xml should be available under /github/workspace/coverage.xml.
Thus, I specify my sonar-project.properties in the root folder of the project:
sonar.organization=pokemate
sonar.projectKey=PokeMate_name-generator
sonar.sources=.
sonar.python.coverage.reportPath=/github/workspace/coverage.xml
my actions file build.yml:
on:
push:
branches:
- master
- develop
- sonar-qube-setup
jobs:
build:
runs-on:
- ubuntu-latest
steps:
# Checkout repo
- uses: actions/checkout#v2
# Dependencies
- name: Set up Python 3.7
uses: actions/setup-python#v1
with:
python-version: 3.7
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
# Test
- name: Test with pytest
run: |
pytest --cov=./ --cov-report=xml --doctest-modules
# Sonar Qube
- name: SonarCloud Scan
uses: sonarsource/sonarcloud-github-action#master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
However, on SonarQ it still shows 0% test coverage, which is probably because it cannot find the coverage.xml. Any idea how to make this work?
The error came from the missing s in reportPaths in the sonar-project.properties file.

Resources