41

I need to see code coverage report for a java maven project in Gitlab. According to this, this and some other sources:

  1. I added jacoco to the list of plugins in pom.xml.
  2. Added pages job to my .gitlab-ci.yml file.
  3. Added Total.*?([0-9]{1,3})% to code coverage parsing in project setting.

but there isn't any coverage report or at least I can't see it. There is no coverage percentage or coverage report page.

Content of .gitlab-ci.yml file:

image: maven:latest

variables:
  MAVEN_CLI_OPTS: "--batch-mode --errors --fail-at-end --show-version -DinstallAtEnd=true -DdeployAtEnd=true"
  MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN -Dorg.slf4j.simpleLogger.showDateTime=true -Djava.awt.headless=true"

cache:
  paths:
    - .m2/repository/

build:
  stage: build
  script:
    - mvn $MAVEN_CLI_OPTS compile

test:
  stage: test
  script:
    - mvn $MAVEN_CLI_OPTS test
  artifacts:
    paths:
      - target/site/jacoco/
pages:
  stage: deploy
  dependencies:
    - test
  script:
   - mkdir public
   - mv target/site/jacoco/index.html public
  artifacts:
    paths:
      - public

deploy:
  stage: deploy
  script:
    - mvn $MAVEN_CLI_OPTS verify
  only:
    - master

jacoco plugin in pom.xml:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.5.201505241946</version>
    <executions>
        <execution>
            <id>pre-unit-test</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>post-unit-test</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>

My Project is a private project on gitlab.com.

Pipeline and its all 4 jobs passed successfully.

How can I see the coverage reports?

Community
  • 1
  • 1
AshKan
  • 779
  • 2
  • 8
  • 22
  • Recently I faced the same issue, I documented my solution here https://notes.jfsanchez.net/post/109-gitlab-coverage-from-jacoco-reports – Jorge F. Sanchez Jul 16 '22 at 00:30

9 Answers9

46

It seems you forgot to add the calls to cat in your .gitlab-ci.yml file.

You should have something like that:

script:
    - mvn $MAVEN_CLI_OPTS test
    - cat target/site/jacoco/index.html

That being said, I don't think this is the best way of doing this, as you need to pollute your output with raw HTML in order to retreive the desired coverage value.

I would recommend using the method described in this pull request instead: https://github.com/jacoco/jacoco/pull/488

  • Keep the jacoco parts in your build.xml
  • Use this awk instruction to print the correct code coverage total:

    awk -F"," '{ instructions += $4 + $5; covered += $5 } END { print covered, "/", 
    instructions, "instructions covered"; print 100*covered/instructions, "% 
    covered" }' target/site/jacoco/jacoco.csv
    
  • Replace the Gitlab CI regexp with what the instruction returns: \d+.\d+ \% covered

Edit:

As of Gitlab 8.17, you can define the regexp directly inside the .gitlab-ci.yml file, as stated in the documentation.

It may seem superfluous, but if this regexp is now part of your repository history, you can change it alongside the other tools used to compute it.

SKBo
  • 619
  • 4
  • 15
  • 1
    if you use multi module project you can do `awk -F"," '{​ instructions += $4 + $5; covered += $5 }​ END {​ print 100*covered/instructions, "% tu covered" }​' \`find . -name "jacoco.csv"\`` – Corentin Aug 11 '21 at 14:02
18

GitLab employee here.

If your administrator has GitLab pages set up, you can see the URL that your artifact deployed to by going (on your project) to Settings -> Pages.

There you should see:

Congratulations! Your pages are served under: https://your-namespace.example.com/your-project

Click on that link and you should be good to go! Also we are expanding support for HTML artifacts. This issue and it’s related issues talk about existing and upcoming features that may expand on what you’ve built here.

glytching
  • 44,936
  • 9
  • 114
  • 120
olearycrew
  • 376
  • 2
  • 4
  • 4
    Thank you. I found pages and the link to coverage report html file. But this file contains only simple percentage of code coverage. Do you know how can I see coverage on code? I mean seeing covered code as green lines, uncovered codes as red lines or something like that? – AshKan Jan 06 '18 at 08:21
16

In addition to what @SKBo said I would like to add a small tweak.

Having

cat target/site/jacoco/index.html

will pollute you build output making hard to read what is important.

I would suggest it to:

cat your/path/to/jacoco/report/index.html | grep -o '<tfoot>.*</tfoot>'

That will reduce the noise in a great manner

Maciej
  • 532
  • 2
  • 10
  • 19
  • The link is now `jacoco/report/html/index.html` So, the final command would be `cat your/path/to/jacoco/report/html/index.html | grep -o '.*'` – Skandy Dec 07 '19 at 01:37
9

In order to display the basic Total Coverage Percentage all you need is:

test:
  stage: test
  image: maven:3.6.3-jdk-11
    - mvn $MAVEN_CLI_OPTS test
    - cat target/site/jacoco/index.html | grep -o 'Total[^%]*%'
  coverage: '/Total.*?([0-9]{1,3})%/'
  artifacts:
    paths:
      - target/site/jacoco/jacoco.xml
    expire_in: 1 mos
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'

and if you want to enable the Code Coverage Visualization feature:

visualize_test_coverage:
  stage: visualize_test_coverage
  image: registry.gitlab.com/haynes/jacoco2cobertura:1.0.7
  script:
    - 'python /opt/cover2cover.py target/site/jacoco/jacoco.xml src/main/java > target/site/cobertura.xml'
    - 'python /opt/source2filename.py target/site/cobertura.xml'
  needs: [ "test" ]
  dependencies:
    - test
  artifacts:
    reports:
      coverage_report:
        coverage_format: cobertura
        path: target/site/cobertura.xml
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'

for detailed information check the official Gitlab Docs here

magiccrafter
  • 5,175
  • 1
  • 56
  • 50
8

I use this command in .gitlab-ci.yml

cat target/site/jacoco-merge/index.html | grep -o 'Total[^%]*%' | sed 's/<.*>/ /; s/Total/Jacoco Coverage Total:/'

This prints a nice string without mess and html tags:

Jacoco Coverage Total: 39%

and then you can use the regex mentioned in gitlabs documentation:

Total.*?([0-9]{1,3})%

or you can use:

Jacoco Coverage Total: ([0-9]{1,3})%
Saljack
  • 2,072
  • 21
  • 24
6

I am using this code:

image: maven:latest

sonarqube-check:
  script:
    - mvn verify sonar:sonar -Dsonar.host.url=$SONAR_HOST_URL -Dsonar.login=$SONAR_TOKEN -Dsonar.qualitygate.wait=true
    - cat target/site/jacoco/index.html | grep -o '.*'
  allow_failure: false
  coverage: "/Total.*?([0-9]{1,3})%/"
1

Rather than trying to parse the HTML output, this short awk script extracts the percentage from the jococo.csv file.

#!/bin/sh

# This awk script calculates a code coverage %
# usage: pass the the jacoco.csv file as first argument

awk -F "," '
    {
      instructions += $4 + $5; covered += $5
    }
    END {
      print covered, "/", instructions, "instructions covered";
      printf "%.2f%% covered\n", covered / instructions * 100
    }' "$1"

Prints:

coverage.sh target/site/jacoco/jacoco.csv 
369992 / 469172 instructions covered
78.86% covered

You should adjust the printf format to match your Gitlab coverage regex

istepaniuk
  • 4,016
  • 2
  • 32
  • 60
  • Note that it's also possible to use a `#!/usr/bin/awk` shebang, but it's less portable due to some compatibility issues with different distros and versions of gawk. – istepaniuk May 31 '21 at 12:07
1

full CI example, based on previous answers, working for both unit & integration tests

test:
  stage: test
  image: maven:3.8-openjdk-17-slim
    - mvn $MAVEN_CLI_OPTS verify
      # jacoco code-coverage reporting
    - if [ -f target/site/jacoco/index.html ]; then awk -F"," '{ instructions += $4 + $5; covered += $5 } END { print 100*covered/instructions, "% covered" }' target/site/jacoco/jacoco.csv; fi
    - if [ -f target/site/jacoco-it/index.html ]; then awk -F"," '{ instructions += $4 + $5; covered += $5 } END { print 100*covered/instructions, "% covered" }' target/site/jacoco-it/jacoco.csv; fi
  coverage: '/([0-9.]*) % covered/'
mychalvlcek
  • 3,956
  • 1
  • 19
  • 34
0

add a new job , exec script

awk -F"," '{ instructions += $4 + $5; covered += $5 } END { print int(100*covered/instructions), "% covered" }' target/site/jacoco/jacoco.csv

GET THIS

74 % covered

bz5314520
  • 1
  • 1
  • 2