Giter Club home page Giter Club logo

action-test-and-analyse's Introduction

BREAKING CHANGES in v1.0:

  • node_version is now required (previously defaulted to 16)
  • sonar_comment_token has been removed (ignored by SonarCloud)
  • sonar_project_token has been renamed sonar_token

Issues Pull Requests MIT License Lifecycle

Test and Analyze with Triggers and SonarCloud

This action runs tests, dependent on triggers, optionally sending results and coverage to SonarCloud. Test and SonarCloud can be configured to comment on pull requests or stop failing workflows.

Conditional triggers are used to determine whether tests need to be run. If triggers are matched, then the appropriate code has changed and should be tested. Tests always run if no triggers are provided. Untriggered runs do little other than report a success.

Only nodejs (JavaScript, TypeScript) is supported by this action. Please see our Java action or upcoming Python action as required.

Usage

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

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

    # Project/app directory
    dir: frontend

    # Node.js version
    # BREAKING CHANGE: previously defaulted to 16 (LTS)
    node_version: "20"

    ### Typical / recommended

    # 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 token
    # Available from sonarcloud.io or your organization administrator
    # BCGov uses https://github.com/BCDevOps/devops-requests/issues/new/choose
    # Provide an unpopulated token for pre-setup, section will be skipped
    sonar_token:
      description: ${{ secrets.SONAR_TOKEN }}

    # Bash array to diff for build triggering
    # Optional, defaults to nothing, which forces a build
    triggers: ('frontend/')

    ### Usually a bad idea / not recommended

    # Overrides the default branch to diff against
    # Defaults to the default branch, usually `main`
    diff_branch: ${{ github.event.repository.default_branch }}

    # Repository to clone and process
    # Useful for consuming other repos, like in testing
    # Defaults to the current one
    repository: ${{ github.repository }}

    # Branch to clone and process
    # Useful for consuming non-default branches, like in testing
    # Defants to empty, cloning the default branch
    branch: ""

    # Bash array of events for limiting triggers, otherwise trigger automatically
    # E.g. ("pull_request" "push" "workflow_dispatch")
    # Defaults to only using triggers with pull requests
    triggers_event: "('pull_request')"

Example, Single Directory with SonarCloud Analysis

Run 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 }}.

The specified triggers will be used to decide whether this job runs tests and analysis or just exists successfully.

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

Note: Provde an unpopulated SONAR_TOKEN until one is provisioned. SonarCloud will only run once populated, allowing for pre-setup.

name: Test and Analyze

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: Test and Analyze
    runs-on: ubuntu-22.04
    steps:
      - uses: bcgov-nr/action-test-and-analyse@main
        with:
          commands: |
            npm ci
            npm run test:cov
          dir: frontend
          node_version: "20"
          sonar_args: |
            -Dsonar.exclusions=**/coverage/**,**/node_modules/**
            -Dsonar.organization=bcgov-nr
            -Dsonar.projectKey=bcgov-nr_action-test-and-analyse_frontend
          sonar_token: ${{ secrets.SONAR_TOKEN }}
          triggers: ('frontend/' 'charts/frontend')

Example, Only Running Tests (No SonarCloud), No Triggers

No triggers are provided so tests will always run. SonarCloud is skipped.

jobs:
  tests:
    name: Test and Analyze
    runs-on: ubuntu-22.04
    steps:
      - uses: bcgov-nr/action-test-and-analyse@main
        with:
          commands: |
            npm ci
            npm run test:cov
          dir: frontend
          node_version: "20"

Example, Matrix / Multiple Directories with Sonar Cloud and Triggers

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

jobs:
  tests:
    name: Test and Analyze
    runs-on: ubuntu-22.04
    strategy:
      matrix:
        dir: [backend, frontend]
        include:
          - dir: backend
            token: SONAR_TOKEN_BACKEND
            triggers: ('frontend/' 'charts/frontend')
          - dir: frontend
            token: SONAR_TOKEN_FRONTEND
            triggers: ('backend/' 'charts/backend')
    steps:
      - uses: actions/checkout@v3
      - uses: bcgov-nr/action-test-and-analyse@main
        with:
          commands: |
            npm ci
            npm run test:cov
          dir: ${{ matrix.dir }}
          node_version: "20"
          sonar_args: |
            -Dsonar.exclusions=**/coverage/**,**/node_modules/**
            -Dsonar.organization=bcgov-nr
            -Dsonar.projectKey=bcgov-nr_action-test-and-analyse_${{ matrix.dir }}
          sonar_token: ${{ secrets[matrix.token] }}
          triggers: ${{ matrix.triggers }}
          repository: bcgov/quickstart-openshift
          branch: main

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={}&analysisMode=GitHubActions

Triggers and Triggers_Event

Triggers are used to limit test running to only appropriate files are changed. This is generally not desirable outside of pull requests, so triggers_event defaults to ("pull_request"). Override this behaviour by specifying a bash array using any of the many, many event types GitHub provides, e.g. ("branch_protection_rule" "workflow_dispatch" "push").

Feedback

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

action-test-and-analyse's People

Contributors

derekroberts avatar renovate[bot] avatar bcgov-devops avatar craigyu avatar dependabot[bot] avatar

Stargazers

Paulo Gomes da Cruz Junior avatar

Watchers

Guy Lafleur avatar Paulo Gomes da Cruz Junior avatar Michelle Douville avatar

Forkers

renovate-bot

action-test-and-analyse's Issues

Help improving FAM github actions for continuous integration

Open to any suggestions, but some specific issues observed:

  1. The CI job (sonar.yml, which should be renamed) runs separate CI jobs for the front-end, backend, and auth components. I'd prefer for these to only run if there are relevant changes in the corresponding source directories. Right now they each run all the time, each doing a separate SonarCloud scan, which results in SonarCloud spamming results emails.

  2. We have a ci_auth_artifact.yml that should likely be combined with the sonar.yml auth component job.

  3. The ci_infrastructure.yml is working well, I did scope it to the right set of files so it only runs when needed, but I'd appreciate any feedback you have.

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Other Branches

These updates are pending. To force PRs open, click the checkbox below.

  • chore(deps): update actions/checkout action to v4

Detected dependencies

github-actions
.github/workflows/tests.yml
  • actions/checkout v3
  • ubuntu 22.04
action.yml
  • actions/checkout v3
  • actions/setup-node v3
  • actions/cache v3
  • SonarSource/sonarcloud-github-action v2.0.2
  • peter-murray/reset-workspace-ownership-action v1
  • actions/checkout v3

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/tests.yml
  • actions/checkout v4
  • ubuntu 22.04
action.yml
  • bcgov-nr/action-diff-triggers v0.2.0
  • actions/checkout v4
  • actions/setup-node v4
  • SonarSource/sonarcloud-github-action v3.0.0
  • peter-murray/reset-workspace-ownership-action v1
  • actions/checkout v4

  • Check this box to trigger a request for Renovate to run again on this repository

bug: drop unreliable sonar_comment option

Sonar comments are not controlled properly from their action. It's best to make the change from sonarcloud.io. Remove the flag, or at least kick up a warning, and add a breaking change to the release number and readme.

Extra: rename sonar_project_token to just sonar_token.

make node_version a required field

The current version defaults node_version to 16, this issue intends to make a required field for node_version to be specified manually.

feat: base triggers on events

Add a parameter to check for events. This can allow triggers to skip tests on PRs, but run the full set on merge.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.