Issue
I'm new to Github workflows. I wanted to do a POC/demo showing code coverage and gating. I'm trying to fix two issues:
- Get past the code coverage issue (the previous run worked)
- Make a low coverage un-mergable into the master branch. I don't know how to raise some sort of blocking Github status. I request enlightenment on this.
Details:
If I do
mvn test
I get a build that works and has 100% code coverage (it says anyway).
When I check this into Github, the PR has an error, but no explanation.
Run madrapps/[email protected]
with:
paths: /home/runner/work/java-testing-demo/java-testing-demo/target/site/jacoco/jacoco.xml
token: ***
min-coverage-overall: 40
min-coverage-changed-files: 85
debug-mode: false
env:
JAVA_HOME: /opt/hostedtoolcache/Java_Adopt_jdk/17.0.2-8/x64
Event is push
base sha: 736b95e6068cb7a507790dfc01c6b44d9b663e67
head sha: 0a796a06c68a47511db88838b9d7794df9777535
build.yml:
name: Build
on:
push:
branches:
- "*"
pull_request:
branches:
- master
jobs:
build:
environment: DEV
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'adopt'
- name: Cache SonarCloud packages
uses: actions/cache@v1
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Cache Maven packages
uses: actions/cache@v1
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
- name: Build and analyze
run: |
mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar \
-Dsonar.projectKey=bpracht_java-testing-demo \
-Dsonar.organization=bpracht \
-Dsonar.host.url=https://sonarcloud.io \
-Dsonar.login="${{secrets.SONAR_TOKEN}}"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
- name: Code coverage metric
id: jacoco
uses: madrapps/[email protected]
with:
paths: ${{ github.workspace }}/target/site/jacoco/jacoco.xml
token: ${{ secrets.GITHUB_TOKEN }}
min-coverage-overall: 40
min-coverage-changed-files: 85
- name: Get the Coverage information
run: |
echo "Total coverage ${{ steps.jacoco.outputs.coverage-overall }}"
echo "Changed Files coverage ${{ steps.jacoco.outputs.coverage-changed-files }}"
Solution
The default mvn wrapper scipts run the java program with an "exec" command. If anything fails, you do not get any detailed messaging at all and it skips any further steps in your actions list so you cannot even get access to the artifact files if you collect them.
To figure out what is going on you can remove the "exec" that prefixes the java command in the script and add "exit 0" as the last line in the maven script so that Github actions thinks everything was fine and you can then upload artifact files to help identifying the issue. You can also pipe all Maven output to a file then upload the log as an artifact. Your script would look something like this:
name: Java CI
on: [pull_request]
jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
java_version: [8, 11, 13, 17]
os: [windows-latest, ubuntu-latest]
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set up JDK ${{ matrix.java_version }}
uses: actions/setup-java@v2
with:
java-version: ${{ matrix.java_version }}
distribution: 'adopt'
- name: Make Maven Wrapper and Java finder executable so command works for windows and nix
if: contains(matrix.os, 'win') == false
run: |
chmod +x ./mvnw
- name: Build with Maven
run: |
./mvnw clean test -e --debug --file pom.xml --log-file maven.log
- name: Archive logs
uses: actions/upload-artifact@v3
with:
name: execution-logs-${{ matrix.java_version }}-${{ matrix.os }}
path: maven.log
retention-days: 1
You can then download the maven.log file from the artifact store for your project in Github
Answered By - Christopher Broderick
Answer Checked By - David Goodson (JavaFixing Volunteer)