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

Test and Analyze with Triggers and SonarCloud

v0.0.1 Pre-release

Test and Analyze with Triggers and SonarCloud

check-square

Test and Analyze with Triggers and SonarCloud

Run node tests based on triggers, optional SonarCloud

Installation

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

              

- name: Test and Analyze with Triggers and SonarCloud

uses: bcgov-nr/action-test-and-analyse@v0.0.1

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

Choose a version

Issues Pull Requests MIT License Lifecycle

Unit Test (nodejs), Coverage and Analysis with SonarCloud

This action runs unit tests and optionally runs analysis, including coverage, using SonarCloud. SonarCloud can be configured to comment on pull requests or stop failing workflows.

Only nodejs (JavaScript, TypeScript) is currently support, with plans for Java next.

Usage

- uses: bcgov-nr/action-test-and-analyse@main
  with:
    ### Required

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

    # Project/app directory
    dir: frontend

    ### Typical / recommended

    # Node.js version
    # Defaults to 16 (LTS)
    node_version: "16"

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

    # Sonar comment token
    # Providing this will enable SonarCloud to comment on Pull Requests
    # $${{ secrets.GITHUB }} or a personal access token can be used
    sonar_comment_token: ${{ secrets.GITHUB_TOKEN }}

    # Sonar project token
    # Available from sonarcloud.io or your organization administrator
    # BCGov i.e. https://github.com/BCDevOps/devops-requests/issues/new/choose
    sonar_project_token:
      description: ${{ secrets.SONAR_TOKEN }}

    ### Usually a bad idea / not recommended

    # Repository to clone and process
    # Useful for consuming other repos, like in testing
    # Defaults to the current one
    repository: ${{ github.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 secret to provide ${{ secrets.SONAR_TOKEN }}.

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

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-latest
    steps:
      - uses: bcgov-nr/action-test-and-analyse@main
        with:
          commands: |
            npm ci
            npm run test:cov
          dir: frontend
          sonar_args: |
            -Dsonar.exclusions=**/coverage/**,**/node_modules/**
            -Dsonar.organization=bcgov-nr
            -Dsonar.projectKey=bcgov-nr_action-test-and-analyse_frontend
          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-latest
    steps:
      - uses: bcgov-nr/action-test-and-analyse@main
        with:
          commands: |
            npm ci
            npm run test:cov
          dir: frontend

Example, Matrix / Multiple Directories with Sonar Cloud

Unit test and analyze projects in multiple directories in parallel. This time repository and sonar_comment_token are provided. Please note how secrets must be passed in to composite Actions using the secrets[matrix.variable] syntax.

jobs:
  tests:
    name: Unit Tests
    runs-on: ubuntu-latest
    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/action-test-and-analyse@main
        with:
          commands: |
            npm ci
            npm run test:cov
          dir: ${{ matrix.dir }}
          repository: bcgov/nr-quickstart-typescript
          sonar_args: |
            -Dsonar.exclusions=**/coverage/**,**/node_modules/**
            -Dsonar.organization=bcgov-nr
            -Dsonar.projectKey=bcgov-nr_action-test-and-analyse_${{ matrix.dir }}
          sonar_comment_token: ${{ secrets.GITHUB_TOKEN }}
          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 #, 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={}&analysisMode=GitHubActions

Acknowledgements

This Action is provided courtesty of the Forestry Suite of Applications, part of the Government of British Columbia.