mercredi 24 mars 2021

Gitlab coverage badge always unknow

The problem

I'm working on a Laravel package project and setting up a pipeline to test it (against php 7.3, 7.4 and 8.0) on merge request and publishing it to my package registry on main branch.

I'm trying to set up a badge for code coverage, but i can't make it work correctly since it is always dipslayng unknown percentage.

What i've done

The pipeline

stages:
  - dependancies
  - linting
  - test
  - publish

# Dependancies job
download dependancies:
  stage: dependancies
  # job stuff

# Linting jobs
phpcs:
  stage: linting
  # job stuff

# test jobs templates
.testing_template: &testing
  stage: test
  script:
    - php -dxdebug.mode=coverage vendor/bin/phpunit --coverage-text --colors=never
  only:
    - merge_requests

# Testing jobs
php 8:
  image: mileschou/xdebug:8.0
  <<: *testing

php 7.4:
  image: mileschou/xdebug:7.4
  <<: *testing

php 7.3:
  image: mileschou/xdebug:7.3
  <<: *testing


#  Publish to package registry
publish:
  only:
    - master
    - tags
  # job stuff

So my pipeline works as follow:

  • on the feature branch
    • download dependancies
    • lint code
    • test against php 7.3, 7.4 and 8.0
  • on the main branch and on tags
    • publish the package to the packages registry

So the part executed on the merge request is as follow

enter image description here

Moreover, i can see code coverage for each test job in the pipeline job section

enter image description here

(I'm not sure why the tests results in different code coverage percentages since the codebase is the same, but is obviously related to the php version and probably specific optimizations done in code parsing.)

The badge

Since this is a Laravel package project i'm testing it with PhpUnit, so i used the given regex for the test coverage parsing setting (taken from the Gitlab docs).

^\s*Lines:\s*\d+.\d+\%

I want a badge that show the code coverage status on the master branch, not on the feature branch, so i've created a coverage badge with the badge image url as follow (i added a reference to a specific test job, i.e. php 7.4)

https://gitlab.com/%{project_path}/badges/%{default_branch}/coverage.svg?job=php%207.4

but it keeps being displayed as

enter image description here.

I've tought that Gitlab can't find the desired php 7.4 job since it is not executed on the master branch but on a feature branch, so i tried replacing the %{default_branch} placeholder with the %{commit_sha} to reference a specific commit, but this doesn't work neither since (i suppose) it reference the merge commit (that triggered just the publish job) and not the one that triggered the entire pipeline.

I'm stuck

I cannot figure out how to get the code coverage percentage for the master branch.

I don't want to execute testing again on the master branche since i've already tested the feature branch before merging it.

How can i do? Am I missing something?

1 commentaire:

  1. Dans ma boite, on exécute les tests sans calcul du coverage sur l'ensemble des branches et on exécute le coverage uniquement sur master. ça permet de gagner en rapidité de feedback lors des développements car le coverage prend plus de temps et au passage on a bien comme vous le souhaitez le badge sur master.

    RépondreSupprimer