Skip to content

feat(cicd): Add UT to PR's via SonarCloud #149

feat(cicd): Add UT to PR's via SonarCloud

feat(cicd): Add UT to PR's via SonarCloud #149

Workflow file for this run

name: Analysis
on:
push:
branches: [dev, main]
pull_request:
types: [opened, reopened, synchronize, ready_for_review, converted_to_draft]
schedule:
- cron: "0 11 * * 0" # 3 AM PST = 12 PM UDT, runs sundays
workflow_dispatch:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
#TODO add the Unit TEST with sonar, sample https://github.com/bcgov/quickstart-openshift/blob/main/.github/workflows/analysis.yml#L17-L58
tests:
name: Unit Tests
# TODO - Re-enable below before submitting PR, jsut have it disabled while I work via draft.
# if: ${{ ! github.event.pull_request.draft }}
runs-on: ubuntu-24.04
timeout-minutes: 5
services:
postgres:
image: postgres
env:
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
strategy:
matrix:
dir: [backend/sites, frontend/site-registry]
include:
- dir: backend/sites
name: backend
token: SONAR_TOKEN_BACKEND_SITES
- dir: frontend/site-registry
name: frontend
token: SONAR_TOKEN_FRONTEND_SITE_REGISTRY
steps:
- uses: bcgov-nr/action-test-and-analyse@v1.2.1
env:
LOG_LEVEL: info
LOG_FILE_LOCATION: /tmp/sites-%DATE%.log
with:
commands: |
npm ci
npm run test:cov
dir: ${{ matrix.dir }}
node_version: "22"
sonar_args: >
-Dsonar.exclusions=**/coverage/**,**/node_modules/**,**/*spec.ts,src/app/migrations/
-Dsonar.organization=bcgov-sonarcloud
-Dsonar.projectKey=bcgov-sonarcloud_nr-site-registry-${{ matrix.name }}
-Dsonar.sources=src
-Dsonar.tests.inclusions=**/*spec.ts
-Dsonar.javascript.lcov.reportPaths=./coverage/lcov.info
-Dsonar.cpd.exclusions=**/migrations/**
-Dsonar.projectBaseDir=${{ matrix.dir }}/
sonar_token: ${{ secrets[matrix.token] }}
triggers: ('${{ matrix.dir }}/')
# https://github.com/marketplace/actions/aqua-security-trivy
trivy:
name: Trivy Security Scan
if: ${{ ! github.event.pull_request.draft }}
runs-on: ubuntu-24.04
timeout-minutes: 1
steps:
- uses: actions/checkout@v4
- name: Run Trivy vulnerability scanner in repo mode
uses: aquasecurity/trivy-action@0.26.0
with:
format: "sarif"
output: "trivy-results.sarif"
ignore-unfixed: true
scan-type: "fs"
scanners: "vuln,secret,config"
severity: "CRITICAL,HIGH"
- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: "trivy-results.sarif"
results:
name: Analysis Results
needs: [tests, trivy]
if: always()
runs-on: ubuntu-24.04
steps:
- if: contains(needs.*.result, 'failure')
run: echo "At least one job has failed." && exit 1
- run: echo "Success!"