Skip to content
You're viewing an older version of this GitHub Action. Do you want to see the latest version instead?
check-square

GitHub Action

Java Unit Test and Analysis with SonarCloud

v0.2.0

Java Unit Test and Analysis with SonarCloud

check-square

Java Unit Test and Analysis with SonarCloud

Run Java unit tests, can analyse with SonarCloud

Installation

Copy and paste the following snippet into your .yml file.

              

- name: Java Unit Test and Analysis with SonarCloud

uses: bcgov-nr/[email protected]

Learn more about this action in bcgov-nr/action-test-and-analyse-java

Choose a version

Issues Pull Requests MIT License Lifecycle

Test (Java), Coverage and Analysis with SonarCloud

This action runs tests and optionally runs analysis, including coverage, using SonarCloud. SonarCloud can be configured to comment on pull requests or stop failing workflows. Also this action automatically configures the github package server in a settings file to import dependencies from it during execution.

This Action supports Java. Another for JavaScript/TypeScript is available.

Usage

- uses: bcgov-nr/[email protected]
  with:
    ### Required

    # Commands to run unit tests
    # Please configure your app to generate coverage (coverage/lcov.info)
    commands: |
      ./mvnw test

    # Project/app directory
    dir: backend

    ### Typical / recommended

    # Java package manager cache, defaults to maven
    java-cache: maven

    # Java distribution, defaults to temurin
    java-distribution: temurin

    # Java version, defaults to 17 (LTS)
    java-version: "17"

    # Sonar arguments
    # https://docs.sonarcloud.io/advanced-setup/analysis-parameters/
    sonar_args: |
        -Dsonar.exclusions=**/coverage/**
        -Dsonar.organization=bcgov-sonarcloud
        -Dsonar.projectKey=bcgov_${{ github.repository }}

    # Sonar project token
    # Available from sonarcloud.io or your organization administrator
    # BCGov i.e. https://github.com/BCDevOps/devops-requests/issues/new/choose
    # Provide an unpopulated token for pre-setup, section will be skipped
    sonar_project_token:
      description: ${{ secrets.SONAR_TOKEN }}

    ### Usually a bad idea / not recommended

    # Repository to clone and process
    # Useful for consuming outside reposities, defaults to the current one
    repository: <organization>/<repository>

Example, Single Directory with SonarCloud Analysis

Run unit tests and provide results to SonarCloud. This is a full workflow that runs on pull requests, merge to main and workflow_dispatch. Use a GitHub Action and Dependabot secret for ${{ secrets.SONAR_TOKEN }}.

Create or modify a GitHub workflow, like below. E.g. ./github/workflows/unit-tests.yml

Note: SonarCloud allows pre-setup. Configure without a token to skip steps until one is provided.

name: Unit Tests and Analysis

on:
  pull_request:
  push:
    branches:
      - main
    paths-ignore:
      - ".github/**"
      - "**.md"
  workflow_dispatch:

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  tests:
    name: Run Unit Tests and Analyse
    runs-on: ubuntu-22.04
    steps:
      - uses: bcgov-nr/[email protected]
        with:
          commands: |
            ./mvnw test
          dir: frontend
          java-cache: maven
          java-distribution: temurin
          java-version: "17"
          sonar_args: |
            -Dsonar.exclusions=**/coverage/**
            -Dsonar.organization=bcgov-nr
            -Dsonar.projectKey=bcgov-nr_action-test-and-analyse-java
          sonar_project_token: ${{ secrets.SONAR_TOKEN }}

Example, Single Directory, Only Running Unit Tests (No SonarCloud)

Run unit tests, but not SonarCloud.

jobs:
  tests:
    name: Run Unit Tests and Analyse
    runs-on: ubuntu-22.04
    steps:
      - uses: bcgov-nr/[email protected]
        with:
          commands: |
            ./mvnw test
          dir: frontend
          java-cache: maven
          java-distribution: temurin
          java-version: "17"

Example, Matrix / Multiple Directories with Sonar Cloud

Unit test and analyze projects in multiple directories in parallel using matrices. Please note how matrices required that secrets use [matrix.variable] syntax.

jobs:
  tests:
    name: Unit Tests
    runs-on: ubuntu-22.04
    strategy:
      matrix:
        dir: [backend, frontend]
        include:
          - dir: backend
            token: SONAR_TOKEN_BACKEND
          - dir: frontend
            token: SONAR_TOKEN_FRONTEND
    steps:
      - uses: actions/checkout@v3
      - uses: bcgov-nr/[email protected]
        with:
          commands: |
            ./mvnw test
          dir: ${{ matrix.dir }}
          java-cache: maven
          java-distribution: temurin
          java-version: "17"
          sonar_args: |
            -Dsonar.exclusions=**/coverage/**
            -Dsonar.organization=bcgov-nr
            -Dsonar.projectKey=bcgov-nr_action-test-and-analyse-java_${{ matrix.dir }}
          sonar_project_token: ${{ secrets[matrix.token] }}

Sonar Project Token

SonarCloud project tokens are free, available from SonarCloud or your organization's aministrators.

For BC Government projects, please create an issue for our platform team.

After sign up, a token should be available from your project on the SonarCloud site. Multirepo projects (e.g. backend, frontend) will have multiple projects. Click Administration > Analysis Method > GitHub Actions (tutorial) to find yours.

E.g. https://sonarcloud.io/project/configuration?id={\<PROJECT>}&analysisMode=GitHubActions

Feedback

Please contribute your ideas! Issues and pull requests are appreciated.