Package: jacoco-badge-generator
jacoco-badge-generatorCheck out all of our GitHub Actions:https://actions.cicirello.org/AboutGitHub ActionsCommand-Line UtilityBuild StatusSecuritySource InfoSupportThe jacoco-badge-generator can be used in one of two ways: as a GitHub Action or as a command-line
utility (e.g., such as part of a local build script). The jacoco-badge-generator parses ajacoco.csvfrom a JaCoCo coverage report, computes coverage percentages
fromJaCoCo's Instructions and Branches counters, and
generates badges for one or both of these (user configurable) to provide an easy
to read visual summary of the code coverage of your test cases. The default behavior directly
generates the badges internally with no external calls, but the action also provides an option
to instead generateShields JSON endpoints. It supports
both the basic case of a singlejacoco.csv, as well as multi-module projects in which
case the action can produce coverage badges from the combination of the JaCoCo reports
from all modules, provided that the individual reports are independent. It can also be configured to
generate a simple JSON file containing the coverages as double-precision floating-point values, either
instead of or in addition to generating the badges, which may be useful as input to other tools.When used as a GitHub Action, the jacoco-badge-generator can also optionally be used as part of a pull-request
check. Specifically, you can configure it to fail the workflow run if coverage decreased relative to prior run,
and/or if coverage is below a target threshold. See theInputssection for details of how to configure
it for this purpose.The developers of the jacoco-badge-generator are not affiliated with the developers of JaCoCo, although we are a
fan and user of their excellent test coverage tool.Table of ContentsThe documentation is organized into the following sections:The Coverage Metrics: Explains the JaCoCo
metrics that are supported by the badge generator, such as what
they measure, and why they were chosen for inclusion for
the jacoco-badge-generator.Badge Style and Content: Provides
examples of the appearance of the badges that are generated,
including a description of the color scheme used, and the
formatting of the percentages.GitHub Action Usage: Details on how to use the
jacoco-badge-generator GitHub Action (its primary use-case).Inputs: Detailed descriptions of the action inputs.Outputs: Detailed descriptions of the action inputs.Example Workflows: Example GitHub workflows
demonstrating usage of the jacoco-badge-generator action.Multi-Module Example Workflows: Example
GitHub workflows demonstrating usage of the jacoco-badge-generator
action with multi-module projects.Examples in Other Projects: Info and a link
to a template repository that we have setup to provide live runnable workflows
to get you started; as well as links to a few repositories that are actively
using the action, as well as direct links to the relevant workflow files.Command-Line Usage: Details on how to install and run
the jacoco-badge-generator as a command-line utility outside of GitHub Actions.Installing from PyPI: How to install command-line
utility from PyPI.Running for the First Time: Command-line utility's
help menu.CLI Examples: Several examples of using the command-line utlity.Summary of Input Defaults: A table summarizing all
of the inputs, along with the defaults, for both GitHub Actions usage as well as
CLI usage.Built With: A list of languages, tools, etc used to develop this action.Blog Posts: A selection of blog posts about the GitHub Action.Support the Project: Information on various
ways that you can support the project.The Coverage MetricsThe jacoco-badge-generator currently supports generating badges for
the two primary coverage metrics generated by JaCoCo: Instructions (C0 Coverage), and
Branches (C1 Coverage). Here is a summary of what these compute and why they were chosen
for inclusion by this badge generator.Instructions Coverage (C0 Coverage)The default behavior of the badge generator is to generate only the Instructions Coverage
badge, which is labeled on the badge simply as "coverage". JaCoCo
measuresC0 Coveragefrom the Java bytecode instructions in the compiled.classfiles. One of the advantages
to counting the bytecode instructions executed or missed, rather than lines of source code,
is that it is independent of coding style and formatting. As a simple example, consider
the sequence of Java statements to swap the values in two
variables:int temp = a; a = b; b = temp;.  A line counter will count this as 1 line if
written on a single line, or 3 lines if each statement is written on its own line. However,
JaCoCo's instructions counter treats these two cases as equivalent since they compile
to the same bytecode. Consider a more complex example of calling a method while passing a simple
value, such asfoo(5)versus passing the result of a calculation to the method, such asfoo(2.0 + bar/11.0). Line counting considers both of these as 1 line; while the second case
will factor in more heavily into JaCoCo's instruction counting than will the first case. For
these reasons, although JaCoCo also provides line coverage data, we do not currently support
generating a badge from JaCoCo's line counter data. JaCoCo's use of bytecode instructions
in its definition of C0 Coverage is a more meaningful measure of coverage than is counting
lines of code.Branches Coverage (C1 Coverage)The badge generator also optionally supports generating a badge for Branches Coverage
(or C1 Coverage), with the generated badge labeled as "branches".  See
theinputssection for a description of the action
inputs.  JaCoCo
measuresC1 Coverage or Branches Coveragefrom the Java bytecode in the compiled.classfiles, so the result may be a bit
different than what you might expect from branch coverage. At first, you may even mistakenly
guess that it is counting conditions (C2 coverage) rather than branches, but it is counting
branches (in bytecode rather than in source code). Consider this example to illustrate
the difference:if (a && b) foo(); else bar();. If we count branches in source code,
there are 2 branches, which would require a minimum of two tests for full coverage, one where
bothaandbaretrue, and a second test where at least one of them isfalse.
If we instead count conditions, there are 4 conditions (a==true,a==false,b==true,b==false), which can be covered with as few as two tests (e.g.,a==trueandb==falseas test one, andb==trueanda==falseas test two), without actually covering both
branches. JaCoCo's branches counter is neither of these. JaCoCo's branches counter
counts branches in the Java bytecode. What does that mean for this example? Imagine instead
that the if statement above was written as a pair of nested if
statements:if (a) { if (b) { foo(); } else { bar(); } } else { bar(); }. There are
a total of 4 branches in this case (and is essentially what JaCoCo would count as branches).
To cover all 4 branches would require a minimum of 3 test cases: one whereaandbare bothtrue, one in whichaistrueandbisfalse, and a third whereaisfalseandb's
value doesn't matter. In this way, JaCoCo's branches counter leads to a stronger form
of C1 Coverage than is usually implied by branches coverage.Badge Style and ContentDefault Color SchemeHere are a few samples of what the badges look like if you use
the default colors:Coverage rangeDirect badge generationBadge generation from endpointBright green for 100% coverageGreen for 90% through 99.9% coverageYellow green for 80% through 89.9% coverageYellow for 70% through 79.9% coverageOrange for 60% through 69.9% coverageRed for 0% through 59.9% coverageSample of a branch coverage badgeCustomizing Colors or Coverage IntervalsThe jacoco-badge-generator provides two inputs that can be used to customize
the colors used for the badges. Thecolorsinput enables you to pass a list
of colors to the action. Theintervalsinput enables you to pass a list of
percentages used to determine color choice. If you like the default colors,
but want to start the colors at different percentages, then you can use theintervalsinput to accomplish that. These two inputs can be used either
individually or in combination depending upon what you want to do. See theInputssection for more details.Displayed PercentagesThe coverage displayed in the badge is the result of truncating to one
decimal place.  If that decimal place is 0, then it is displayed as an
integer.  The rationale for truncating to one decimal place, rather than
rounding is to avoid displaying a just failing coverage as passing. For
example, if the user of the action considers 80% to be a passing level,
then we wish to avoid the case of 79.9999% being rounded to 80% (it will
instead be truncated to 79.9%).Direct Badge Generation vs JSON EndpointThe default behavior generates badges that are inspired by the style of the badges
ofShields.io, and generates the badges entirely
within the jacoco-badge-generator, with no external calls. However, it also supports
an optional alternative to instead generateShields JSON endpoints.
Most users will likely prefer the default behavior, for a variety of reasons, such as
simpler insertion of badge into README and probable faster loading. The main reason to
consider generating a JSON endpoint instead is if you are trying to match the style of
the coverage badges to other badges in your README that use one of Shields's alternative
styles. The default internally generated badges match the default Shields style. See
theInputssection for more details on how to generate JSON endpoints instead
of badges.Adding the Badges to your READMEIf you generate the badges (default behavior)....If you use the default badges directory and default badge filenames, then
you can add the coverage badge to your repository's readme with the following
markdown:![Coverage](.github/badges/jacoco.svg)And likewise for the branch coverage badge:![Branches](.github/badges/branches.svg)See theInputssection for how to change the directory and filenames of
the badges. You can of course also link these to the JaCoCo coverage report if you host it
online, or perhaps to the workflow that generated it, such as with (just replace
USERNAME and REPOSITORY with yours):[![Coverage](.github/badges/jacoco.svg)](https://github.com/USERNAME/REPOSITORY/actions/workflows/build.yml)The above assumes that the relevant workflow isbuild.yml(replace as needed). This will
link the badge to the runs of that specific workflow.If you generate JSON endpoints instead....Inserting coverage badges into your README is more complex if you use
the alternate behavior of generating JSON endpoints. It involves
passing the URL of your coverage endpoint to Shields custom badge endpoint.
Assuming that you use the default badge directory, you would then use
the following markdown:![Coverage](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/USERNAME/REPOSITORY/BRANCHNAME/.github/badges/jacoco.json)In the above, replace USERNAME, REPOSITORY, and BRANCHNAME with yours. You can do
something similar for the branches coverage badge, such as:![Branches](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/USERNAME/REPOSITORY/BRANCHNAME/.github/badges/branches.json)And of course, you can also link these to your workflow runs just as before with:[![Coverage](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/USERNAME/REPOSITORY/BRANCHNAME/.github/badges/jacoco.json)](https://github.com/USERNAME/REPOSITORY/actions/workflows/build.yml)If you do have reason to prefer generating endpoints over generating the badges directly,
then you might consider pushing the endpoints to a GitHub Pages site instead, such
as a project site served from a docs directory of your default branch, or from a gh-pages
branch. To do so, in addition to configuring GitHub Pages, you would need to use thebadges-directoryinput to change the directory where the endpoints are stored
(e.g., in "docs" or in a subdirectory of "docs"). Doing so would probably speed up Shields's
access to your JSON endpoint, since you'd gain the benefit of the CDN that backs GitHub
Pages; whereas passing Shields the URL to the JSON file on GitHub's raw server will probably
be slower. Note that the potential benefit is probably small, so if doing so would complicate
your workflow, you can simply pass the URL of the endpoint from GitHub's raw server
(e.g., the examples of generating badges from an endpoint in the rightmost column
of the table in sectionDefault Color Schemewere done that way,
without the use of GitHub Pages).This is not an issue if you use the default behavior of directly generating the badge,
since in that case the image is served directly to the viewer from the repository whose
README is being viewed.GitHub Action UsageThe jacoco-badge-generator's primary use-case is as a GitHub Action. The subsections of this
section documents how to use it as a GitHub Action.InputsAll inputs include default values, and are thus optional provided the
defaults are relevant to your use-case.jacoco-csv-fileThis input is the full path, relative to the root of the repository, to
thejacoco.csvfile, including filename.  It defaults
totarget/site/jacoco/jacoco.csv, which is the default location and filename
assuming you are using the JaCoCo Maven plugin and don't change the default
output location. Note that if you are using Gradle to run your build, you will
definitely need to use this input, because the default location and name of the
jacoco csv report is different than it is in Maven. If you use Gradle's default
output directories, then you will need to set this input with something
like:jacoco-csv-file: build/reports/jacoco/test/jacocoTestReport.csv.If you have a multi-module project, you can pass the paths (including filenames)
to all of thejacoco.csvfiles for all of the sub-projects. Separate these by spaces,
and in particular see theMulti-Module Example Workflowsfor an example of how to do this. Multi-module support is limited to cases where
each module has its own test coverage report, and where those reports don't overlap.You can also use a glob pattern to specify the set of JaCoCo reports for your modules.
For example,jacoco-csv-file: "**/jacoco.csv"will match alljacoco.csvfiles found
across all directories within your project. Or for examplejacoco-csv-file: "**/*.csv"will match allcsvfiles found within your project, but be careful with such a pattern
if your project has othercsvfiles that are not JaCoCo reports. Or as another example,
maybe all of your JaCoCo reports are in the same directory, but with names with numbers.
The patternjacoco-csv-file: "target/site/jacoco/module*.csv"will match allcsvfiles
in the directorytarget/site/jacoco/whose name begins withmodule. Note that in all of
these examples you need the quotes around the glob pattern or else GitHub Actions will
give you an error that your workflow file is invalid.The action assumes that all reports passed via this input are
independent of each other. If you are using matrix testing, such that
each group of tests produces a report, and where the groups overlap in what
they are testing (e.g., one group tests a portion of a class or method,
and another group tests another portion, etc), then the coverage computed by
this action will not be correct. The csv reports don't contain enough information
to properly merge such overlapping reports. If this applies to your use-case, then
you will need to have JaCoCo produce a single JaCoCo report first (for example,
seejacoco:report-aggregate).badges-directoryThis input is the directory for storing badges, relative to the root of the
repository. The default is.github/badges. The action will create the badges
directory if it doesn't already exist, although the action itself does not commit.generate-coverage-badgeThis input controls whether or not to generate the coverage badge (Instructions
Coverage), and defaults totrue.coverage-badge-filenameThis input is the filename for the coverage badge (Instructions or C0
Coverage). The default filename
isjacoco.svg. The file format is ansvg. The badge file will be
created within thebadges-directorydirectory.The action doesn't commit the badge file. You will
need to have additional steps in your workflow to do that.generate-branches-badgeThis input controls whether or not to generate the branches coverage badge, and defaults
tofalse. This defaults tofalseto avoid surprising users who upgrade from earlier
versions with a badge they didn't know would be generated.branches-badge-filenameThis input is the filename for the branches coverage badge (C1
Coverage). The default filename
isbranches.svg. The file format is ansvg. The badge file will be
created within thebadges-directorydirectory.The action doesn't commit the badge file. You will
need to have additional steps in your workflow to do that.generate-coverage-endpointThis input controls whether or not to generate a JSON endpoint
for coverage (Instructions Coverage), and defaults tofalse.coverage-endpoint-filenameThis input is the filename for the coverage endpoint (Instructions or C0
Coverage) if you have opted to generate a JSON endpoint instead of the
badge. The default filename isjacoco.json, and will be
created within thebadges-directorydirectory.The action doesn't commit the JSON file. You will
need to have additional steps in your workflow to do that.generate-branches-endpointThis input controls whether or not to generate a JSON endpoint
for branches coverage, and defaults tofalse.branches-endpoint-filenameThis input is the filename for the branches coverage endpoint (C1
Coverage) if you have opted to generate a JSON endpoint instead of the
badge. The default filename isbranches.json, and will be
created within thebadges-directorydirectory.The action doesn't commit the JSON file. You will
need to have additional steps in your workflow to do that.generate-summaryThis input controls whether or not to generate a simple JSON
summary report of the following form:{"branches":77.77777777777779,"coverage":72.72727272727273}The default isgenerate-summary: false. To enable, usegenerate-summary: true.summary-filenameThis input is the filename for the summary report (see above). The
default issummary-filename: coverage-summary.json, and will be
created within thebadges-directorydirectory.The action doesn't commit the JSON file. You will
need to have additional steps in your workflow to do that.coverage-labelThis input is the text for the label on the left side of the coverage badge, which
defaults tocoverage.branches-labelThis input is the text for the label on the left side of the branches coverage badge, which
defaults tobranches.colorsThis input can be used to change the colors used for the badges.
It defaults tocolors: '#4c1 #97ca00 #a4a61d #dfb317 #fe7d37 #e05d44',
which are the hex color codes for the colors described previously in
sectionDefault Color Scheme.
Because#has special meaning to YAML (it is used for comments), you
must either put quotes around the input value as shown in this example, or
you can escape each#. The list of colors that you pass here can either
be space separated (as shown) or comma separated. The colors in this list
can be specified either with hex (as in the example above), or with any
named colors that are recognized by SVG, or some combination of the two.
Here is an example with named
colors:colors: green yellow orange red purple blue. Notice that you don't need
quotes around the input if none of the colors are specified by hex.
Although the default uses six colors and six coverage intervals, you can have
as many or as few as you want. For example, if you want to usegreenregardless
of percentage, you can set colors like this:colors: green. If you pass more
colors than there are intervals, then the extra colors will be ignored. If you
pass an empty list of colors, then the action will simply use the default colors.The action does not do any validation of the colors that you pass.intervalsThis input enables specifying the coverage intervals for the
different colors. It is a simple list of percentages. The default
isintervals: 100 90 80 70 60 0, which corresponds to what is
described in the sectionDefault Color Schemeearlier.  The action assumes that the percentages in this list are in
decreasing order. You can space separate or
comma separate the percentages. For example,intervals: 100 90 80 70 60 0is equivalent tointervals: 100, 90, 80, 70, 60, 0. A mix of spaces and commas
will also work.If you specify too many intervals, the extras will simply be ignored. If there
are C colors altogether, then only the first (C-1) percentages specified in
this input are used, with the last color designated for coverages that are below
that last cutoff.  For example, if you use the default set of 6 colors, thenintervals: 100 90 80 70 60is equivalent tointervals: 100 90 80 70 60 0.Although these examples have integer percentages, the action
supports floating-point values. For example, you can specify something
likeintervals: 99.5 90 80 70 60.If you only want to use the first three default colors (bright green, green,
and yellow green), then you don't necessarily need to change the value
of thecolorsinput. You can keep the default colors, and
then you can use something likeintervals: 80 60, which will assign
80 and above to bright green, 60 and above to green, and less than 60 to yellow
green.If you like some of the default colors, but want to skip over some of them,
then you can either use a combination of thecolorsinput andintervalsinput to accomplish this, or you can leavecolorsat the default and
exploit the action's assumption of decreasing percentages in theintervalsinput to skip the ones you don't like. For example, if
you want to use bright green for 80 and above,
yellow for 60 and above, and red for less than 60, you might do something like the
following:intervals: 80 80 80 60 60 0. The 0 at the end is optional.on-missing-reportThis input controls what happens if one or morejacoco.csvfiles do not exist.
This input accepts one of three possible values:fail,quiet, orbadges.
The behavior of these is defined as follows:The default ison-missing-report: fail, in which case the action will
return a non-zero exit code (causing the workflow run to fail) if one
or more files listed in thejacoco-csv-fileinput do not exist, or if
an empty list of files is passed to the action. We recommend that you use
this default since missing coverage report files in most cases probably means
that there is either a bug in your workflow (e.g., typo in path to jacoco.csv)
or that something went wrong in an earlier step (e.g., unit tests failed, halting
generation of the coverage report).You can useon-missing-report: quietif you would rather the workflow
itself not fail, in which case the action will instead quietly exit
without producing badges if any JaCoCo reports are missing.Although not recommended, a third option,on-missing-report: badges, will
cause the action to produce badges from the report files that do exist, simply
ignoring missing report files, provided that at least one such report file
exists. We do not recommend this option since such a case is likely due to an
error in your workflow, and any badges produced are likely computed with missing data.Regardless of value passed to this input, the action will log warnings for
any files listed in thejacoco-csv-fileinput that do not exist, for your
inspection in the workflow run.fail-if-coverage-less-thanThis input enables directing the action to fail the workflow run if
the computed coverage is less than a minimum. The default is 0, effectively
disabling the option. You can specify it as either a floating point value
in the interval 0.0 to 1.0, or as a percent (with or without the percent sign).
For example, all of the following are equivalent:fail-if-coverage-less-than: 0.6,fail-if-coverage-less-than: 60, orfail-if-coverage-less-than: "60%".
Note that in the last case, you need the quotes due to the percent sign.
Values greater than 1 are assumed percents.fail-if-branches-less-thanThis input enables directing the action to fail the workflow run if
the computed branches coverage is less than a minimum. The default is 0, effectively
disabling the option. You can specify it as either a floating point value
in the interval 0.0 to 1.0, or as a percent (with or without the percent sign).
For example, all of the following are equivalent:fail-if-branches-less-than: 0.6,fail-if-branches-less-than: 60, orfail-if-branches-less-than: "60%".
Note that in the last case, you need the quotes due to the percent sign.
Values greater than 1 are assumed percents.fail-on-coverage-decreaseThis input enables directing the action to fail the workflow run if
the computed coverage is less than it was on the previous run as recorded in either the
existing coverage badge, the existing coverage Shields endpoint, or the JSON summary
report (see thegenerate-summaryinput), if one of these exists. The default
isfalse.  Usefail-on-coverage-decrease: trueto enable.Additionally, at least one of thegenerate-summary,generate-coverage-badge,
orgenerate-coverage-endpointinputs must also betrue, as the action will otherwise assume
that there is no existing badge or summary report from which to get the prior coverage.
If more than one of these exist, this feature will use the summary report to determine if coverage
decreased since it is more precise than the truncated coverage percentage stored in
the badge or Shields endpoint.Therefore, when using this feature, it is recommended that
you also setgenerate-summary: trueand commit the summary report JSON file to the repository.fail-on-branches-decreaseThis input enables directing the action to fail the workflow run if
the computed branches coverage is less than it was on the previous run as recorded in either the
existing branches coverage badge, the existing branches coverage Shields endpoint, or the JSON summary
report (see thegenerate-summaryinput), if one of these exists. The default isfalse.
Usefail-on-branches-decrease: trueto enable.Additionally, at least one of thegenerate-summary,generate-branches-badge,
orgenerate-branches-endpointinputs must also betrue, as the action will otherwise assume
that there is no existing badge or summary report from which to get the prior coverage.
If more than one of these exist, this feature will use the summary report to determine if branches coverage
decreased since it is more precise than the truncated coverage percentage stored in
the badge or Shields endpoint.Therefore, when using this feature, it is recommended that
you also setgenerate-summary: trueand commit the summary report JSON file to the repository.generate-workflow-summaryThis input controls whether or not to log the coverage percentages to the GitHub Actions
workflow job summary. The default isgenerate-workflow-summary: true. This input is only
relevant when running in GitHub Actions mode, and not when running as a CLI tool.workflow-summary-headingThis input is used to override the heading for the GitHub Actions workflow job summary that
can be found on the Actions tab with detailed workflow logs. It defaults to:workflow-summary-heading: JaCoCo Test Coverage Summary. This default should be fine in most
cases. If you are using this action in a multimodule project, and generating separate badges
for each module, then you might consider using this input so that the GitHub workflow job
summary is clearer as to which output corresponds to which module. See theMulti-Module Example Workflowssection for examples. This
input is only relevant when running in GitHub Actions mode, and not when running as a CLI tool.OutputsThe action also outputs the actual computed coverage percentages as double-precision
floating-point numbers. So you can add a step to your workflow to access these if
desired (these action outputs are values in the interval from 0.0 to 1.0).coverageThis output is the actual computed coverage percentage in the interval
from 0.0 to 1.0.  This is coverage computed from the instructions
coverage data in the JaCoCo csv report.branchesThis output is the actual computed branches coverage percentage
in the interval from 0.0 to 1.0.  This is the percentage of branches
covered, computed from the branches data in the JaCoCo csv report.Example WorkflowsPrerequisite: Running JaCoCoRunning JaCoCo via MavenThe example workflows assume that you are using Maven to build and test
a Java project, and that you have thejacoco-maven-pluginconfigured in yourpom.xmlin the test phase with something
along the lines of the following:<build><plugins><plugin><groupId>org.jacoco</groupId><artifactId>jacoco-maven-plugin</artifactId><version>0.8.10</version><executions><execution><goals><goal>prepare-agent</goal></goals></execution><execution><id>generate-code-coverage-report</id><phase>test</phase><goals><goal>report</goal></goals></execution></executions></plugin></plugins></build>Note that the jacoco-badge-generator action has been tested with
thejacoco.csvfiles generated byjacoco-maven-pluginversions
0.8.6 through 0.8.10, and has not been tested with earlier versions
of JaCoCo.Running JaCoCo via GradleIf you use gradle as your build tool, then you can configure JaCoCo
inbuild.gradle.ktswith:plugins{jacoco}tasks.jacocoTestReport{reports{csv.isEnabled=true}}Or the equivalent inbuild.gradle:plugins {
    id 'jacoco'
}

jacocoTestReport {
    reports {
        csv.enabled true
    }
}Basic Action SyntaxIf you use Maven as your build tool, then you will have steps
in your workflow along the lines of the following (which assumes
that Maven is configured to run JaCoCo during the test phase:- name: Build with Maven
      run: mvn -B test

    - name: Generate JaCoCo Badge
      uses: cicirello/jacoco-badge-generator@v2
      with:
        generate-branches-badge: trueThe equivalent for Gradle is:- name: Run Tests
        run: ./gradlew test

      - name: Run Test Coverage
        run: ./gradlew jacocoTestReport

      - name: Generate JaCoCo Badge
        uses: cicirello/jacoco-badge-generator@v2
        with:
          generate-branches-badge: true
          jacoco-csv-file: build/reports/jacoco/test/jacocoTestReport.csvYou can also use a specific release with:- name: Generate JaCoCo Badge
      uses: cicirello/jacoco-badge-generator@v2.11.0
      with:
        generate-branches-badge: trueAll Possible Action InputsThis shows a workflow step that uses all of the
possible inputs of thejacoco-badge-generatoraction.
It simply shows all of the inputs with their default values.
See theInputssection for complete details of
what these inputs do.- name: Generate JaCoCo Badge
      uses: cicirello/jacoco-badge-generator@v2
      with:
        jacoco-csv-file: target/site/jacoco/jacoco.csv
        badges-directory: .github/badges
        generate-coverage-badge: true
        coverage-badge-filename: jacoco.svg
        generate-branches-badge: false
        branches-badge-filename: branches.svg
        generate-coverage-endpoint: false
        coverage-endpoint-filename: jacoco.json
        generate-branches-endpoint: false
        branches-endpoint-filename: branches.json
        generate-summary: false
        summary-filename: coverage-summary.json
        coverage-label: coverage
        branches-label: branches
        colors: '#4c1 #97ca00 #a4a61d #dfb317 #fe7d37 #e05d44'
        intervals: 100 90 80 70 60 0
        on-missing-report: fail
        fail-if-coverage-less-than: 0
        fail-if-branches-less-than: 0
        fail-on-coverage-decrease: false
        fail-on-branches-decrease: false
        generate-workflow-summary: true
        workflow-summary-heading: JaCoCo Test Coverage SummarySince the above shows all of the default values of the action inputs,
it is equivalent to:- name: Generate JaCoCo Badge
      uses: cicirello/jacoco-badge-generator@v2Example Workflow 1: Generate instructions (or C0) coverage badge only.This sample workflow runs on pushes to the main
branch.  It first sets up Java, and runs the tests with Maven. If you
have JaCoCo configured to run during the test phase, this will also
produce the JaCoCo reports.  The jacoco-badge-generator action is then
run to parse thejacoco.csv, compute the coverage percentage, and
generate the badge.  The coverage percentage is then logged in the
workflow so you can inspect later if necessary. The next step of the workflow
checks if any changes were made to the badge, and if so, does a commit and a push (note
that there are also GitHub Actions that you can use for that step). And
finally, the JaCoCo coverage reports are uploaded as a workflow
artifact using theactions/upload-artifactGitHub Action, so you can inspect them if necessary.name: build

on:
  push:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Set up the Java JDK
      uses: actions/setup-java@v2
      with:
        java-version: '17'
        distribution: 'adopt'

    - name: Build with Maven
      run: mvn -B test

    - name: Generate JaCoCo Badge
      id: jacoco
      uses: cicirello/jacoco-badge-generator@v2

    - name: Log coverage percentage
      run: |
        echo "coverage = ${{ steps.jacoco.outputs.coverage }}"
        echo "branch coverage = ${{ steps.jacoco.outputs.branches }}"

    - name: Commit the badge (if it changed)
      run: |
        if [[ `git status --porcelain` ]]; then
          git config --global user.name 'YOUR NAME HERE'
          git config --global user.email 'YOUR-GITHUB-USERID@users.noreply.github.com'
          git add -A
          git commit -m "Autogenerated JaCoCo coverage badge"
          git push
        fi

    - name: Upload JaCoCo coverage report
      uses: actions/upload-artifact@v2
      with:
        name: jacoco-report
        path: target/site/jacoco/Example Workflow 2: Generate instructions coverage and branches coverage badges.This example workflow is just like the above example, however, it generates
both badges (instructions coverage percentage and branches coverage percentage).
This example also uses
theEndBug/add-and-commitaction to
commit and push the badge, whereas the previous example did this step with
shell commands.name: build

on:
  push:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Set up the Java JDK
      uses: actions/setup-java@v2
      with:
        java-version: '17'
        distribution: 'adopt'

    - name: Build with Maven
      run: mvn -B test

    - name: Generate JaCoCo Badge
      id: jacoco
      uses: cicirello/jacoco-badge-generator@v2
      with:
        generate-branches-badge: true

    - name: Log coverage percentage
      run: |
        echo "coverage = ${{ steps.jacoco.outputs.coverage }}"
        echo "branch coverage = ${{ steps.jacoco.outputs.branches }}"

    - name: Commit and push the badge (if it changed)
      uses: EndBug/add-and-commit@v7
      with:
        default_author: github_actions
        message: 'commit badge'
        add: '*.svg'

    - name: Upload JaCoCo coverage report
      uses: actions/upload-artifact@v2
      with:
        name: jacoco-report
        path: target/site/jacoco/Multi-Module Example WorkflowsExample Workflow 3: Generate Instructions and Coverage Badges for a Multi-Module Project.This example workflow generates both badges (instructions coverage percentage
and branches coverage percentage) for a multi-module project. The badges that are generated
are computed over all modules. To do so, simply pass the paths to all of the JaCoCo reports
that you want to include via thejacoco-csv-fileinput. The>is just Yaml's way of writing
a string across multiple lines. You can also just list all on a single space-separated line,
but your workflow file will be easier for you to read if you put them one per line.
In this example, there are three subprojects:module1,module2, andmodule3.name: build

on:
  push:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Set up the Java JDK
      uses: actions/setup-java@v2
      with:
        java-version: '17'
        distribution: 'adopt'

    - name: Build with Maven
      run: mvn -B test

    - name: Generate JaCoCo Badge
      id: jacoco
      uses: cicirello/jacoco-badge-generator@v2
      with:
        generate-branches-badge: true
        jacoco-csv-file: >
          module1/target/site/jacoco/jacoco.csv
          module2/target/site/jacoco/jacoco.csv
          module3/target/site/jacoco/jacoco.csv

    - name: Log coverage percentage
      run: |
        echo "coverage = ${{ steps.jacoco.outputs.coverage }}"
        echo "branch coverage = ${{ steps.jacoco.outputs.branches }}"

    - name: Commit the badge (if it changed)
      run: |
        if [[ `git status --porcelain` ]]; then
          git config --global user.name 'YOUR NAME HERE'
          git config --global user.email 'YOUR-GITHUB-USERID@users.noreply.github.com'
          git add -A
          git commit -m "Autogenerated JaCoCo coverage badge"
          git push
        fiExample Workflow 4: Glob Pattern to Specify Reports of a Multi-Module Project.This example workflow uses a glob pattern to specify the reports of a multi-module project,
and generates both badges (instructions coverage percentage and branches coverage percentage).
The badges that are generated are computed over all modules. In this example, all of the JaCoCo
reports are namedjacoco.csvbut reside in different directories. You can match all of them
withjacoco-csv-file: "**/jacoco.csv". The quotes around the glob are required to avoid an
invalid workflow error from GitHub Actions.name: build

on:
  push:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Set up the Java JDK
      uses: actions/setup-java@v2
      with:
        java-version: '17'
        distribution: 'adopt'

    - name: Build with Maven
      run: mvn -B test

    - name: Generate JaCoCo Badge
      id: jacoco
      uses: cicirello/jacoco-badge-generator@v2
      with:
        generate-branches-badge: true
        jacoco-csv-file: "**/jacoco.csv"

    - name: Log coverage percentage
      run: |
        echo "coverage = ${{ steps.jacoco.outputs.coverage }}"
        echo "branch coverage = ${{ steps.jacoco.outputs.branches }}"

    - name: Commit the badge (if it changed)
      run: |
        if [[ `git status --porcelain` ]]; then
          git config --global user.name 'YOUR NAME HERE'
          git config --global user.email 'YOUR-GITHUB-USERID@users.noreply.github.com'
          git add -A
          git commit -m "Autogenerated JaCoCo coverage badge"
          git push
        fiExample Workflow 5: Multi-Module Project with Separate Badges for Each Module.If you would prefer to generate separate coverage badges for each of the
modules of a multi-module project, then just include multiple steps of thejacoco-badge-generatorin your workflow, such as in this example. Be sure to use
the inputs to specify names for the badge files, otherwise with the defaults
the subsequent steps will overwrite the previous. This example assumes that there
are two modules. You also will likely want to use thecoverage-labelandbranches-labelinputs to change the text on the left side of the badges if you are displaying badges
for multiple modules in the README of the same repository. This example demonstrates that
as well.name: build

on:
  push:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Set up the Java JDK
      uses: actions/setup-java@v2
      with:
        java-version: '17'
        distribution: 'adopt'

    - name: Build with Maven
      run: mvn -B test

    - name: Generate JaCoCo Badges for Module 1
      id: jacocoMod1
      uses: cicirello/jacoco-badge-generator@v2
      with:
        generate-branches-badge: true
        jacoco-csv-file: module1/target/site/jacoco/jacoco.csv
        coverage-badge-filename: jacoco1.svg
        branches-badge-filename: branches1.svg
        coverage-label: coverage (module 1)
        branches-label: branches (module 1)
        workflow-summary-heading: Module 1 JaCoCo Test Coverage Summary

    - name: Generate JaCoCo Badges for Module 2
      id: jacocoMod2
      uses: cicirello/jacoco-badge-generator@v2
      with:
        generate-branches-badge: true
        jacoco-csv-file: module2/target/site/jacoco/jacoco.csv
        coverage-badge-filename: jacoco2.svg
        branches-badge-filename: branches2.svg
        coverage-label: coverage (module 2)
        branches-label: branches (module 2)
        workflow-summary-heading: Module 2 JaCoCo Test Coverage Summary

    - name: Commit the badge (if it changed)
      run: |
        if [[ `git status --porcelain` ]]; then
          git config --global user.name 'YOUR NAME HERE'
          git config --global user.email 'YOUR-GITHUB-USERID@users.noreply.github.com'
          git add -A
          git commit -m "Autogenerated JaCoCo coverage badge"
          git push
        fiExamples in Other ProjectsTemplate Repository with Runnable Workflow ExamplesWe now have
atemplate repositorywith a simple Maven Java project, using thejacoco-maven-plugin,
along with several live, runnable workflows to demonstrate a variety of use-cases
for thejacoco-badge-generatoraction. That repository,cicirello/examples-jacoco-badge-generator,
is a template so that you can potentially use it as a project starter. You can of course
fork it instead. Its README
explains the contents of that repository, especially the details of the various
workflows it contains, and includes examples inserting the badges into its README.Live Real ExamplesIf you would like to see examples where the action is actively used, here
are a few repositories that are actively using thejacoco-badge-generatoraction.
The table provides a link to repositories using the action, and direct links to the
relevant workflow as well as the relevant build configuration (e.g., Mavenpom.xmlor Gradlebuild.gradle.kts) so you can see how JaCoCo is
configured. Note that in all of the Maven examples, JaCoCo is configured within a
Maven profile within thepom.xml, which is then activated via a command line
option whenmvnis run by the workflow on all push/pull request events. Configuration
can instead be done in the<build>section if you'd rather not use a profile.RepositoryWorkflowBuild ConfigurationChips-n-Salsabuild.ymlpom.xmlJavaPermutationToolsbuild.ymlpom.xmlρμbuild.ymlpom.xmlXpathQSbuild.ymlbuild.gradle.ktsCommand-Line UsageThe jacoco-badge-generator started its life as a GitHub Action, but due to interest can now
be used as a command-line utility outside of GitHub Actions.Installing from PyPIThe jacoco-badge-generator requires Python 3 (and has been tested with 3.8 and above).To install from PyPi (Unix and MacOS):python3-mpipinstalljacoco-badge-generatorTo install from PyPi (Windows):py-mpipinstalljacoco-badge-generatorTo upgrade to the latest version from PyPi (Unix and MacOS):python3-mpipinstall--upgradejacoco-badge-generatorTo upgrade to the latest version from PyPi (Windows):py-mpipinstall--upgradejacoco-badge-generatorRunning for the First TimeAfter installing, we recommend running it once with the-hor--helpflag
to see the details of all of the available command-line options.On Unix or MacOS:python3-mjacoco_badge_generator--helpOn Windows:py-mjacoco_badge_generator--helpCLI ExamplesAll GitHub Action inputs have a counterpart command-line option that can be
used in CLI mode. See theInputssection earlier for details. If the GitHub
Actions input is namedinput-name, then in CLI mode, the corresponding command-line
option is--input-name. All options are optional and provide relevant defaults
for the basic use-case. The defaults are nearly identical to those of the GitHub Action,
with a few exceptions.Here are a few examples. Note that all examples assume Unix (e.g., Python command ispython3). If on Windows, just changepython3topyin all of the examples below.All DefaultsAs an example, from the root of your project (assuming you've already run JaCoCo), execute
the following. In this example, all of the defaults are used, which will generate only the
instructions coverage badge, and will place it in abadgesdirectory (creating it if it
doesn't exist). Note that the default directory for the generated badges is one of the differences
between the defaults in CLI mode vs GitHub Actions mode.python3-mjacoco_badge_generatorGenerating Instructions Coverage and Branches Coverage Badgespython3-mjacoco_badge_generator--generate-branches-badgetrueGenerating Shields JSON Endpoints Instead of BadgesIf you want to generate Shields JSON endpoints instead of badges, you need
to disable generating the coverage badge, and enable the JSON endpoints:python3-mjacoco_badge_generator--generate-coverage-badgefalse--generate-coverage-endpointtrue--generate-branches-endpointtrueChanging Colors and Coverage IntervalsIf you want to change the colors used and the coverage intervals for each color,
you can use the--colorsand--intervalsoptions. In the following example,
green is used if coverage is at least 90 percent, yellow if coverage is less than 90 but
at least 75 percent, orange is used if coverage is less than 75 percent but at least 60
percent, and red is used if coverage is less than 60 percent.python3-mjacoco_badge_generator--colorsgreenyelloworangered--intervals907560Colors can be specified as either SVG named colors as above or as 6-digit or 3-digit hex colors
(see theInputsearlier for more detail).Changing the Badges Directorypython3-mjacoco_badge_generator--badges-directoryput/badges/hereGradle Location of JaCoCo ReportThe utility by default assumes that the JaCoCo report is the Maven default
oftarget/site/jacoco/jacoco.csv. If it is somewhere else, there is an option
to specify its location. Here is an example with Gradle's standard location
and name of the JaCoCo csv report.python3-mjacoco_badge_generator--jacoco-csv-filebuild/reports/jacoco/test/jacocoTestReport.csvMulti-Module ExampleIf you have a multi-module project with multiple coverage reports that you want to combine (provided
they are independent), then you can specify the locations and names of all of the report files with
something like:python3-mjacoco_badge_generator--jacoco-csv-filereports/report1.csvreports/report2.csvIn CLI mode, glob patterns will be handled by your shell. You can accomplish the above with:python3-mjacoco_badge_generator--jacoco-csv-filereports/report*.csvOr perhaps all of your reports are namedjacoco.csvbut in different directories, then you can
match all of them with:python3-mjacoco_badge_generator--jacoco-csv-file**/jacoco.csvSummary of Input DefaultsThe following table summarizes the default values of all inputs for both the GitHub Actions
usage as well as the CLI usage. If your use-case requires the defaults as specified below, then
you do not need to include them.GitHub Actions DefaultCLI Defaultjacoco-csv-file: target/site/jacoco/jacoco.csv--jacoco-csv-file target/site/jacoco/jacoco.csvor-j target/site/jacoco/jacoco.csvbadges-directory: .github/badges--badges-directory badgesor-d badgesgenerate-coverage-badge: true--generate-coverage-badge truecoverage-badge-filename: jacoco.svg--coverage-badge-filename jacoco.svggenerate-branches-badge: false--generate-branches-badge falsebranches-badge-filename: branches.svg--branches-badge-filename branches.svggenerate-coverage-endpoint: false--generate-coverage-endpoint falsecoverage-endpoint-filename: jacoco.json--coverage-endpoint-filename jacoco.jsongenerate-branches-endpoint: false--generate-branches-endpoint falsebranches-endpoint-filename: branches.json--branches-endpoint-filename branches.jsongenerate-summary: false--generate-summary falsesummary-filename: coverage-summary.json--summary-filename coverage-summary.jsoncoverage-label: coverage--coverage-label coveragebranches-label: branches--branches-label branchescolors: '#4c1 #97ca00 #a4a61d #dfb317 #fe7d37 #e05d44'On Windows:--colors #4c1 #97ca00 #a4a61d #dfb317 #fe7d37 #e05d44Bash or anywhere#has special meaning:--colors '#4c1' '#97ca00' '#a4a61d' '#dfb317' '#fe7d37' '#e05d44'intervals: 100 90 80 70 60 0--intervals 100 90 80 70 60 0on-missing-report: fail--on-missing-report failfail-if-coverage-less-than: 0--fail-if-coverage-less-than 0fail-if-branches-less-than: 0--fail-if-branches-less-than 0fail-on-coverage-decrease: false--fail-on-coverage-decrease falsefail-on-branches-decrease: false--fail-on-branches-decrease falsegenerate-workflow-summary: truen/aworkflow-summary-heading: JaCoCo Test Coverage Summaryn/aBuilt WithThejacoco-badge-generatoraction uses the following:Python (CLI mode is 100% Python)GitHub Actions mode additionally utilizes the following:Thecicirello/pyactionDocker container, which
is designed to support GitHub Actions development in the Python language (seepyaction's GitHub repository); andWe started with our template repository for GitHub Actions implemented in
Python:cicirello/python-github-action-template.Blog PostsHere is a selection of blog posts about the jacoco-badge-generator on DEV.to:JaCoCo Coverage Badges for Multi-Module Projects in GitHub Actions, posted on DEV on May 25, 2023.Using GitHub Actions to Build a Java Project With Pull Request Coverage Commenting and Coverage Badges, posted on DEV on November 9, 2022.The jacoco-badge-generator GitHub Action is now also available as a CLI tool from PyPI, posted on DEV on July 8, 2022.JaCoCo coverage badges, PR coverage checks, and PR coverage comments, from GitHub Actions, posted on DEV on November 29, 2021.Support the ProjectYou can support the project in a number of ways:Starring: If you find thejacoco-badge-generatoraction
useful, consider starring the repository.Sharing with Others: Consider sharing it with others who
you feel might find it useful.Reporting Issues: If you find a bug or have a suggestion for
a new feature, please report it via
theIssue tracker.Contributing Code: If there is an open issue that you think
you can help with, submit a pull request.Sponsoring: You can also considerbecoming a sponsor.LicenseThis GitHub action is released under
theMIT License.