How create a badge with test coverage (jacoco) on github (actions) - gradle

In spring project, I use jacoco plugin to measure test coverage.
I see the html report like below:
Now I would like to add a badge to github project with this percentage, something like this:
Any idea how can I combine jacoco with github actions?

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):
https://github.com/emibcn/covid/blob/master/.github/workflows/node.js.yml
https://github.com/emibcn/Rac1.js/blob/master/.github/workflows/node.js.yml

You can use codecov seeing as they support every CI provider.
You will need two things:
An account from codecov and a token.
The codecov gh-action.
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

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

Related

How to store previous report to gh-pages branch

I have following workflow in github action. I am using newman to run my postman collection.
Here what I want to do
Run newman and then store into gh-pages branch. but at the same time I want to store previous report too.
For now it's storing only one report with the name index.html.
name: User workflow
on:
schedule:
- cron: '0 3 * * *'
workflow_dispatch:
push:
branches:
- master
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout#v1
- name: Get Previous Report history
uses: actions/checkout#v2
if: always()
continue-on-error: true
with:
ref: gh-pages
path: gh-pages
- name: Install HTML extra reporter
run: |
npm install -g newman
npm install -g newman-reporter-htmlextra
- name: Run Pets Profile API automated test on Dev or Stage
run: |
newman run users/user_pet_postman_collection.json \
-e users/user_env_postman_collection.json \
-g users/user_postman_globals.json \
-r cli,htmlextra
- name: Archive test report
uses: actions/upload-artifact#v3
with:
name: Newman-Report-${{ github.run_id }}-${{ github.run_attempt }}
path: newman/**/*.html
retention-days: 30
- name: Rename report
if: always()
run: |
ls
pwd
cd newman
mv *.html index.html
- name: Host Report on GH pages
if: always()
uses: crazy-max/ghaction-github-pages#v3
with:
target_branch: gh-pages
build_dir: newman
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Deploy report to Github Pages newman directory
if: always()
uses: peaceiris/actions-gh-pages#v2
env:
PERSONAL_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PUBLISH_BRANCH: gh-pages
PUBLISH_DIR: newman

Helm chart releaser action failed to upload release asset .tgz to GHE using ubuntu self-runner

The chart is packaged but fails at releasing the charts.
Error: error creating GitHub release kafka-cluster-0.1.0: failed to upload release asset: /home/sid/actions-runner/_work/<repo>/<repo>/.cr-release-packages/kafka-cluster-0.1.0.tgz
: POST https://ghe.com/api/uploads/repos/<org>/<repo>/releases/78254/assets?name=kafka-cluster-0.1.0.tgz: 502 Error uploading to https://media.<org ghe>.com/releases/78254/files: 404 []
The helm chart action I'm using
name: Release Chart
on:
push:
branches:
- main
jobs:
release:
runs-on: self-hosted
steps:
- name: Checkout
uses: actions/checkout#v3
with:
fetch-depth: 0
- name: Configure Git
run: |
git config user.name "$GITHUB_ACTOR"
git config user.email "$GITHUB_ACTOR#users.noreply.github.com"
- name: Run chart-releaser
uses: <org>/helm-releaser-action#v1.4.0
with:
config: cr.yaml
env:
CR_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
My github token has these permissions
my cr.yaml :
owner: <org>
git-repo: <repo>
token: ${{ secrets.GITHUB_TOKEN }}
git-base-url: https://ghe/api/v3/
git-upload-url: https://ghe/api/uploads/

How to bump version with GitHub Actions

I want to bump the version by running my shell script. I am manually triggering the GitHub Actions with inputs. I am using GitHub API to get the latest version and I want to run my shell script with the version taken from GitHub API.
I am able to achieve everything. It's just that I am not able to get the version output.
This is my GitHub Workflow:
#############################################################################
# GitHub Action to bump release version
#
#############################################################################
name: "Bump version and Update Milestone"
on:
workflow_dispatch:
inputs:
version:
description: 'New Version'
required: true
default: 'warning'
permissions:
contents: write
pull-requests: write
jobs:
printInputs:
runs-on: ubuntu-latest
steps:
- run: |
echo "Latest Version: ${{ github.event.inputs.version }}"
bump:
name: Bump version
runs-on: ubuntu-latest
steps:
- name: Checkout the latest code
uses: actions/checkout#v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Get the version
id: get_version
run: |
VERSION=$(curl -s https://api.github.com/repos/checkstyle/checkstyle/releases/latest \
| jq ".tag_name")
echo VERSION="$VERSION"
- name: Modify File
run: |
./.ci/bump-version.sh ${{ steps.get_version.VERSION }}
- name: Push commit
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]#users.noreply.github.com'
git commit -am "minor: Bump release version to ${{ steps.get_version.VERSION }}"
git push
- name: GitHub Milestone
run: |
.ci/update-github-milestone.sh
This is my shell script:
#!/bin/bash
set -e
VERSION=$1
echo VERSION="$VERSION"
echo "bump version in pom.xml"
mvn versions:set -DnewVersion="$VERSION" && mvn versions:commit
And these is my tested actions where I want help:
Action link: https://github.com/Rahulkhinchi03/checkstyle/runs/7785606554?check_suite_focus=true
To transfer an output from one step to another, you need to use a workflow command:
echo "::set-output name=VERSION::$VERSION"
and then reference it like this in a later step via the outputs object:
run: |
./.ci/bump-version.sh ${{ steps.get_version.outputs.VERSION }}

Issues creating a CD based release on Github (WPF .NET 5.0)

I'm trying to put together a CI / CD pipeline for GitHub and strugglign with attaching my build artifact to the release. Here's my ci.yml
name: CI
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
workflow_dispatch:
jobs:
Build:
runs-on: windows-latest
env:
BuildPath: ${{ github.workspace }}\BuildTesting\bin\Release\net5.0-windows
steps:
- uses: actions/checkout#v2
- name: Setup .NET SDK
uses: actions/setup-dotnet#v1.8.1
with:
dotnet-version: 5.0.x
- name: Install dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Upload a Build Artifact
uses: actions/upload-artifact#v2.2.4
with:
name: thingy
path: ${{ github.workspace }}\BuildTesting\bin\Release\net5.0-windows
retention-days: 1
That runs well and I get a release artifact:
I was under the impression I'd be able to download that existing artifact but I couldn't get my head around why actions/download-artifact isn't downloading anything. So I found another article and in their cd step they were re-building, so I figured that in doing that at least I'd have a fresh build in the cd workflow to pull from. So I create a release triggered on tag push events. I can't use most zip utilities becuase they don't run on windows. I have to user windows-latest as the target framework for WPF desktop applications has to be net5.0-windows and using ubuntu-latest it fails. I tried papeloto/action-zip#v1 and in one case I managed to get a zip file which then attached to the release successfully but was only 22 bytes, so empty once I downloaded it. Here's my cd.yml:
name: CD
on:
push:
tags:
- '*'
jobs:
Release:
runs-on: windows-latest
env:
BuildPath: ${{ github.workspace }}\BuildTesting\bin\Release\net5.0-windows
ZipName: TheThing.zip
steps:
# Build the solution
- uses: actions/checkout#v2
- name: Setup .NET SDK
uses: actions/setup-dotnet#v1.8.1
with:
dotnet-version: 5.0.x
- name: Install dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Zip the release
uses: << What should I use to zip ${{ env.BuildPath }} ? >>
# Create a Release on the GitHub project
- name: Create release
id: create_release
uses: actions/create-release#v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
with:
tag_name: ${{ github.ref }}
release_name: ${{ github.ref }}
draft: false
prerelease: false
# Upload the Build Artifact to the Release
- name: Update release asset
id: upload-release-asset
uses: actions/upload-release-asset#v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }} # This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps
asset_path: .\${{ env.ZipName }}
asset_name: ${{ env.ZipName }}
asset_content_type: application/zip
Can anyone please recommend a GitHub action for zipping a folder that will work for windows-latest ... or another approach?
Long term I want to go with semantic versioning but GitVersion overwhelmed me last time I tried, granted I'd never working with yml builds before. I'd like to keep this as simple as possible as I'm starting to grok the basics of what's going on and once I get past this I'll start looking into GitVersion.
I'm using a test repository located here so you can see the whole thing.
You may use PowerShell to zip your artifacts using 7Zip. Please check my repository in GitHub. That compile a WPF app, compress the artifacts, create a prerelease and upload files under that release.

How to use Go Release Binary GitHub Action

Anyone able to get Go Release Binary GitHub Action working? which is supposed to
Automate publishing Go build artifacts for GitHub releases through GitHub Actions
The readme looks rather simple, but I've tried all my imaginations to get it working but not avail. Similar questions has been asked in its issue tracks but got no answer.
Somebody help please.
BTW, while searching for the answer, I came upon this commit logs, which is quite interesting/amusing to read. I.e., it seems to be quite a battle to get it working, but the author gave up eventually (no any releases from his/her latest commits/tags)
Conclusion:
Turns out that my project does not have go mod and there were issues in Go Release which stops it from working. It was then fixed by this and this.
I actually wrote my own release workflow for generating Go binaries.
The only non-obvious steps from my point of view are:
I have a release note generation step where I include a list of non-merge commits since the last release tag.
I use a matrix build for GOOS and GOARCH pairs and do some Bash string manipulation in the "Get OS and arch info" step.
The nice thing about softprops/action-gh-release is that you can keep adding artifacts to the same release as long as the workflow run is triggered by a push to the same tag.
on:
push:
# Sequence of patterns matched against refs/tags
tags:
- 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10
name: Latest Release
defaults:
run:
shell: bash
jobs:
lint:
name: Lint files
runs-on: 'ubuntu-latest'
steps:
- uses: actions/checkout#v2
- uses: actions/setup-go#v2
with:
go-version: '1.16.3'
- name: golangci-lint
uses: golangci/golangci-lint-action#v2.5.2
with:
version: latest
test:
name: Run tests
runs-on: 'ubuntu-latest'
needs: lint
steps:
- uses: actions/checkout#v2
- uses: actions/setup-go#v2
with:
go-version: '1.16.3'
- run: go test -v -cover
release:
name: Create Release
runs-on: 'ubuntu-latest'
needs: test
strategy:
matrix:
# List of GOOS and GOARCH pairs from `go tool dist list`
goosarch:
- 'aix/ppc64'
# etc
steps:
- name: Checkout code
uses: actions/checkout#v2
with:
fetch-depth: 0
- uses: actions/setup-go#v2
with:
go-version: '1.16.3'
- name: Get OS and arch info
run: |
GOOSARCH=${{matrix.goosarch}}
GOOS=${GOOSARCH%/*}
GOARCH=${GOOSARCH#*/}
BINARY_NAME=${{github.repository}}-$GOOS-$GOARCH
echo "BINARY_NAME=$BINARY_NAME" >> $GITHUB_ENV
echo "GOOS=$GOOS" >> $GITHUB_ENV
echo "GOARCH=$GOARCH" >> $GITHUB_ENV
- name: Build
run: |
go build -o "$BINARY_NAME" -v
- name: Release Notes
run:
git log $(git describe HEAD~ --tags --abbrev=0)..HEAD --pretty='format:* %h %s%n * %an <%ae>' --no-merges >> ".github/RELEASE-TEMPLATE.md"
- name: Release with Notes
uses: softprops/action-gh-release#v1
with:
body_path: ".github/RELEASE-TEMPLATE.md"
draft: true
files: ${{env.BINARY_NAME}}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Resources