DEV Community

Cover image for Add coverage rate to a php app gitlab-ci
Mehdi Meshkatian
Mehdi Meshkatian

Posted on

Add coverage rate to a php app gitlab-ci

First of all lets make some changes to out dockerfile and add this section into it :

RUN LC_ALL=C.UTF-8 apt-add-repository ppa:ondrej/php -y

Enter fullscreen mode Exit fullscreen mode

now we can install php extensions easy pizzy !!

so we add the build section in out gitlab-ci.yml

build:
  stage: Build
  image: docker:20.10.16
  services:
    - docker:dind
  script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker build --pull -t $IMAGE_TAG .
    - docker push $IMAGE_TAG
Enter fullscreen mode Exit fullscreen mode

now we have our image ready just after this section finished

after that we may create a test section and install xdebug that its required just in this stage :

code-coverage:
  stage: Test
  image: $IMAGE_TAG
  script:
    - apt-get update
    - apt-get install -y php8.2-xdebug
    - export XDEBUG_MODE=coverage

Enter fullscreen mode Exit fullscreen mode

we should avoid install xdebug directly in dockerfile because its reduce the performance !

Image description

Just one more section and then our coverage indicator is ready

add this line so it will run the code coverage test and extract out the coverage percent to gitlab and also make a copy of tests results so we can check them in merge requests

code-coverage:
  stage: Test
  image: $IMAGE_TAG
  script:
    - apt-get update
    - apt-get install -y php8.2-xdebug
    - export XDEBUG_MODE=coverage
    - ./vendor/bin/phpunit --do-not-cache-result --log-junit phpunit-report.xml --coverage-cobertura phpunit-coverage.xml --coverage-text --colors=never
  artifacts:
    when: always
    reports:
      junit: phpunit-report.xml
      coverage_report:
        coverage_format: cobertura
        path: phpunit-coverage.xml
  coverage: '/^\s*Lines:\s*\d+.\d+\%/'
Enter fullscreen mode Exit fullscreen mode

now lets create a merge request and we what happens after pipelines passed :
Image description

Image description

Image description

Top comments (0)