Code coverage report using gitlab-ci.yml file

AshKan picture AshKan · Dec 30, 2017 · Viewed 32k times · Source

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?

Answer

SKBo picture SKBo · Feb 13, 2018

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.