17

In spring project, I use jacoco plugin to measure test coverage.

I see the html report like below:

enter image description here

Now I would like to add a badge to github project with this percentage, something like this:

enter image description here

Any idea how can I combine jacoco with github actions?

emi
  • 2,786
  • 1
  • 16
  • 24
tostao
  • 2,803
  • 4
  • 38
  • 61
  • 1
    I think if you want to use JaCoCo but are unable to find a Github Action that can parse and expose the value you want, it could be a good start to see what the `codecov/codecov-action@v1` is doing and replicate it for yourself and then use it in your project. I will see if I can do this but unfortunately cannot promise! – comdotlinux Dec 06 '19 at 09:00
  • Did you solve the badge images generation? Thanks – JRichardsz Dec 13 '21 at 14:21
  • No, somehow I have it on my private todo list, but currently I am focused on other things. – tostao Dec 13 '21 at 20:27
  • 1
    There is in the meantime a better alternative: https://github.com/marketplace/actions/jacoco-report-extended – khmarbaise Nov 12 '22 at 21:00
  • 1
    There is a lot in nodejs https://github.com/jrichardsz/badges4cov or python https://github.com/cicirello/jacoco-badge-generator but nothing in pure java – JRichardsz Jan 30 '23 at 20:03

3 Answers3

15

You can use GitHub actions to generate a badge using GitHub Workflow (no need to other servers). You could write your own jobs/steps or use my just published action: https://github.com/marketplace/actions/badge-action .

First, you need to parse the coverage result file and extract the value (81 in your example). Here, I used parse-coverage-report as an example command (you'll need to create it by yourself). Finally, save this value as a GitHub workflow output:

on: [push]

jobs:
  coverage:
    runs-on: ubuntu-latest
    name: Generate test coverage badge
    steps:

    - name: Generate a coverage value
      id: coverage
      # Generates a GitHub Workflow output named `lines`
      run: |
        COVERAGE="$( parse-coverage-report )"
        echo "##[set-output name=lines;]${COVERAGE}%"

    # Use the output from the `coverage` step
    - name: Generate the badge SVG image
      uses: emibcn/badge-action@v1
      with:
        label: 'Test coverage'
        status: ${{ steps.coverage.outputs.lines }}
        color: 'blue,555,daf'
        path: badge.svg

This saves the badge as file badge.svg. Now, you decide wether to upload this badge to the same repository, to an S3 or whatever you prefer. Being that a coverage report, I suppose you'll like to upload that to same's repo 1) same branch it was extracted from or 2) dedicated branch badges:

1) Push to same branch it was extracted from

    - name: Extract branch name
      shell: bash
      run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
      id: extract_branch
    - name: Create badges dir if necessary
      run: mkdir -p .github/badges
    - name: Generate the badge SVG image
      uses: emibcn/badge-action@v1
      with:
        label: 'Test coverage'
        status: ${{ steps.coverage.outputs.lines }}
        color: 'blue,555,daf'
        path: .github/badges/badge.svg
    - name: Commit badge
      run: |
        git config --local user.email "action@github.com"
        git config --local user.name "GitHub Action"
        git add .github/badges/badge.svg
        git commit -m "Add/Update badge"
    - name: Push badge commit
      uses: ad-m/github-push-action@master
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}
        branch: ${{ steps.extract_branch.outputs.branch }}

The extract_branch step has been taken from https://stackoverflow.com/a/58035262/2928168 .

2) Push to dedicated branch badges

First, create and push the dedicated branch badges with (extracted from StackOverflow):

git checkout master

# Use a fresh start
git checkout --orphan badges

# Unstage all the files in your working tree.
git rm --cached $(git ls-files)

# Create a dedicated README file, so it's clear what's going on here
echo '# Badges branch' > README.md
git add README.md
git commit -m 'Add dedicated README'
git push origin badges
    - name: Extract branch name
      shell: bash
      run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
      id: extract_branch
    - uses: actions/checkout@v1
      with:
        ref: badges
        path: badges
    - name: Create badges dir if necessary
      env:
        BRANCH: ${{ steps.extract_branch.outputs.branch }}
      run: mkdir -p badges/${BRANCH}
    - name: Generate the badge SVG image
      uses: emibcn/badge-action@v1
      with:
        label: 'Test coverage'
        status: ${{ steps.coverage.outputs.lines }}
        color: 'blue,555,daf'
        path: badges/${{ steps.extract_branch.outputs.branch }}/badge.svg
    - name: Commit badge
      env:
        BRANCH: ${{ steps.extract_branch.outputs.branch }}
      run: |
        pushd badges
            git config --local user.email "action@github.com"
            git config --local user.name "GitHub Action"
            git add "${BRANCH}/badge.svg"
            git commit -m "Add/Update badge"
        popd
    - name: Push badge commit
      uses: ad-m/github-push-action@master
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}
        branch: badges
        directory: badges

UPDATE

If you coverage report is a typical clover coverage.xml file, you can use this action to parse and output the coverage value. For example:

- name: Check test coverage
  uses: johanvanhelden/gha-clover-test-coverage-check@v1
  id: coverage
  with:
    percentage: "50"
    filename: "coverage.xml"
# Use the output from the `coverage` step
- name: Generate the badge SVG image
  uses: emibcn/badge-action@v1
  id: badge
  with:
    label: 'Coverage'
    status: ${{ steps.coverage.outputs.coverage }}
    path: ./badges/test-coverage.svg
    color: ${{ steps.coverage.outputs.coveragelines > 75 && 'green' || 'red' }}

UPDATE 2

You can make your badge change its background color depending on the coverage value, even using gradients:

# Use the output from the `coverage` step
- name: Generate the badge SVG image
  uses: emibcn/badge-action@v1
  id: badge
  with:
    label: 'Coverage'
    status: ${{ steps.coverage.outputs.coverage }}
    path: ./badges/test-coverage.svg
    color: ${{
          steps.coverage.outputs.coverage > 90 && 'green'              ||
          steps.coverage.outputs.coverage > 80 && 'yellow,green'       ||
          steps.coverage.outputs.coverage > 70 && 'yellow'             ||
          steps.coverage.outputs.coverage > 60 && 'orange,yellow'      ||
          steps.coverage.outputs.coverage > 50 && 'orange'             ||
          steps.coverage.outputs.coverage > 40 && 'red,orange'         ||
          steps.coverage.outputs.coverage > 30 && 'red,red,orange'     ||
          steps.coverage.outputs.coverage > 20 && 'red,red,red,orange' ||
          'red' }}

UPDATE 3

Updated the 2 workflows:

  • Same branch: Save badges into .github/badges/
  • Dedicated branch: Use a sub directory in the workflow to manage the badges, so workflow environment remains usable for further steps (for example, saving some cache).

UPDATE 4: Working examples

You can see working examples in some repositories workflows (add yours by editing the answer or commenting on it):

emi
  • 2,786
  • 1
  • 16
  • 24
  • 2
    Update the 2 options, with better path choices (Update 3). – emi Mar 14 '21 at 09:16
  • question is related jacoco which is a java framework. I don't see anything related to that in your answer :( – JRichardsz Dec 13 '21 at 14:20
  • 2
    @JRichardsz Jacoco can output the coverage report into an XML file which can later be consumed by other applications: https://www.eclemma.org/jacoco/trunk/doc/report-mojo.html – emi Dec 13 '21 at 19:22
  • 2
    @JRichardsz Also, the question is about how to generate a badge using GitHub Actions, which I think is widely covered in the answer. – emi Dec 13 '21 at 19:25
  • Maybe it would help if you explain how plugin works. For example I found one based on python which needs as input the jacoco.csv to generate the png badges. Then github action is used just to execute the python code. You are right in the sense that question didn't ask for that explanation but on my case, I cannot try or use something that I did not understand before. – JRichardsz Dec 13 '21 at 21:04
  • @JRichardsz From the README of the action: *This action generates a SVG badge using GitHub Actions and GitHub Workflow CPU time (no 3rd parties servers). The badge is generated using the NPM package gradient-badge and Github Badge Action to read and write the GitHub Actions inputs and outputs.* – emi Jan 19 '22 at 10:23
  • @JRichardsz In the answer's first **UPDATE** there is an example of how to parse the coverage XML output to extract the coverage value: *If you coverage report is a typical clover `coverage.xml` file, you can use [this action](https://github.com/marketplace/actions/clover-test-coverage-check) to parse and output the coverage value.* There are other GitHub actions to parse different XML coverage results. – emi Jan 19 '22 at 10:25
  • Nice work! Do you have an example project for which it is already used to display coverage status in README? – tribbloid May 13 '22 at 19:22
  • 2
    @tribbloid Sure! Look at the **UPDATE 4: Working examples** section: https://github.com/emibcn/covid and https://github.com/emibcn/Rac1.js – emi May 14 '22 at 08:43
11

You can use codecov seeing as they support every CI provider.

You will need two things:

After you create your account and have access to a token, store the token as a secret in github actions. Call it CODECOV_TOKEN.

In your workflow, create a step that looks something like this and configure as needed:

- name: Upload coverage to Codecov  
  uses: codecov/codecov-action@v1
    with:
      token: ${{ secrets.CODECOV_TOKEN }}

See example workflow

In your README, create the status badge using the format:

[![codecov](https://codecov.io/gh/<your-name>/<project-name>/branch/master/graph/badge.svg)](https://codecov.io/gh/<your-name>/<project-name>)

Sources: Integrating Codecov with a GitHub project

smac89
  • 39,374
  • 15
  • 132
  • 179
  • Now with GitHub Actions seems even easier but I am stuck at an error complaining about a lcov.info file that is missing... How can I generate it? – Pitto Oct 05 '20 at 13:02
  • question is related jacoco which is a java framework. I don't see anything related to that in your answer :( – JRichardsz Dec 13 '21 at 14:21
2

You'll need to publish your coverage stats to a service such as Coveralls as part of your continuous integration build. CI servers such as CircleCI and TravisCI have built in support for Github and Coveralls

Is your project open source? Coveralls, Travis and CircleCI are all free for open source. Once you've got github triggering CI and publishing to coveralls there's an image tag you can embed in your readme.md

lance-java
  • 25,497
  • 4
  • 59
  • 101