Skip to content

feat: add customizable push command to coverage workflow #21

feat: add customizable push command to coverage workflow

feat: add customizable push command to coverage workflow #21

Workflow file for this run

name: Unit Tests
on:
push:
pull_request:
paths-ignore:
- "**.md"
workflow_call:
inputs:
java_version:
description: "Java version"
required: false
default: "17"
type: string
minimum_coverage:
description: "Minimum coverage percentage"
required: false
default: "0"
type: string
coverage_results_path:
description: "Path to the JaCoCo coverage results file"
required: false
default: "food-main/target/jacoco-report/jacoco.xml"
type: string
jacoco_csv_file:
description: "Path to the JaCoCo CSV file"
required: false
default: "food-main/target/jacoco-report/jacoco.csv"
type: string
test_command:
description: "Test command"
required: false
default: "mvn clean verify"
type: string
aws_region:
description: "AWS Region"
required: false
default: "us-east-1"
type: string
push_command:
description: "Push command"
required: false
default: "origin ${{ github.event.pull_request.head.ref }} --set-upstream --force"
type: string
jobs:
coverage:
permissions: write-all
runs-on: ubuntu-latest
env:
AWS_REGION: ${{ inputs.aws_region || 'us-east-1' }}
AWS_DEFAULT_REGION: ${{ inputs.aws_region || 'us-east-1' }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
repository: ${{ github.event.pull_request.head.repo.full_name }}
ref: ${{ github.event.pull_request.head.ref }}
- name: Set up Java JDK
uses: actions/setup-java@v4
with:
distribution: "temurin"
java-version: ${{ inputs.java_version }}
cache: "maven"
# cache-dependency-path: 'sub-project/pom.xml' # optional
- name: Runs test
run: ${{ inputs.test_command }}
env:
AWS_REGION: ${{ inputs.aws_region || 'us-east-1' }}
AWS_DEFAULT_REGION: ${{ inputs.aws_region || 'us-east-1' }}
# Para evitar erros com permissão de escrita
# https://stackoverflow.com/a/75250838/3929980
# generates coverage-report.md and publishes as checkrun
- name: JaCoCo Code Coverage Report
id: jacoco_reporter
uses: PavanMudigonda/jacoco-reporter@v5.0
with:
coverage_results_path: ${{ inputs.coverage_results_path }}
coverage_report_name: Coverage
coverage_report_title: JaCoCo
github_token: ${{ secrets.GITHUB_TOKEN }}
skip_check_run: false
minimum_coverage: ${{ inputs.minimum_coverage }}
fail_below_threshold: false
publish_only_summary: false
# Publish Coverage Job Summary # Optional
- name: Add Jacocoo report to workflow run summary
run: |
echo "| Outcome | Value |" >> $GITHUB_STEP_SUMMARY
echo "| --- | --- |" >> $GITHUB_STEP_SUMMARY
echo "| Code Coverage % | ${{ steps.jacoco_reporter.outputs.coverage_percentage }} |" >> $GITHUB_STEP_SUMMARY
echo "| :heavy_check_mark: Number of Lines Covered | ${{ steps.jacoco_reporter.outputs.covered_lines }} |" >> $GITHUB_STEP_SUMMARY
echo "| :x: Number of Lines Missed | ${{ steps.jacoco_reporter.outputs.missed_lines }} |" >> $GITHUB_STEP_SUMMARY
echo "| Total Number of Lines | ${{ steps.jacoco_reporter.outputs.total_lines }} |" >> $GITHUB_STEP_SUMMARY
# uploads the coverage-report.md artifact # Optional
- name: Upload Code Coverage Artifacts
uses: actions/upload-artifact@v4
with:
name: code-coverage-report-markdown
path: "*/coverage-results.md"
retention-days: 90
- name: Generate JaCoCo Badge
id: jacoco
uses: cicirello/jacoco-badge-generator@v2
with:
jacoco-csv-file: ${{ inputs.jacoco_csv_file }}
generate-branches-badge: true
generate-coverage-badge: true
- name: Check for modified files
id: git-check
run: echo ::set-output name=modified::$(if [ -n "$(git status --porcelain)" ]; then echo "true"; else echo "false"; fi)
- name: Update changes in GitHub repository
if: steps.git-check.outputs.modified == 'true'
uses: EndBug/add-and-commit@v9.1.4
with:
author_name: GitHub Actions
author_email: github-actions[bot]@users.noreply.github.com
committer_name: GitHub Actions
committer_email: github-actions[bot]@users.noreply.github.com
# default_author: github_actions
message: "[GitHub Actions] Autogenerated JaCoCo coverage badge"
fetch: false
tag_push: '--force'
push: ${{ inputs.push_command }}