Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: param for triggers_event, default to pull_request #36

Merged
merged 14 commits into from
Aug 29, 2023
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Unit Tests and Analysis
name: Tests

on:
pull_request:
Expand All @@ -16,17 +16,26 @@ concurrency:

jobs:
tests:
name: Unit Tests
name: Tests
runs-on: ubuntu-22.04
strategy:
matrix:
dir: [backend, frontend]
name: [backend, frontend-merge, frontend-pr]
include:
- dir: backend
- name: backend
dir: backend
token: SONAR_TOKEN_BACKEND
triggers: ('backend/')
- dir: frontend
triggers_event: ('pull_request')
- name: frontend-merge
dir: frontend
triggers: ('.')
triggers_event: ('pull_request')
- name: frontend-pr
dir: frontend
token: SONAR_TOKEN_FRONTEND
triggers: ('.')
triggers_event: ('push')
steps:
- uses: actions/checkout@v3
- uses: ./
Expand All @@ -36,7 +45,7 @@ jobs:
npm run test:cov
dir: ${{ matrix.dir }}
node_version: "20"
repository: bcgov/nr-quickstart-typescript
repository: bcgov/quickstart-openshift
sonar_args: >
-Dsonar.exclusions=**/coverage/**,**/node_modules/**,**/*spec.ts
-Dsonar.javascript.lcov.reportPaths=./coverage/lcov.info
Expand All @@ -46,3 +55,4 @@ jobs:
-Dsonar.tests.inclusions=**/*spec.ts
sonar_token: ${{ secrets[matrix.token] }}
triggers: ${{ matrix.triggers }}
triggers_event: ${{ matrix.triggers_event }}
37 changes: 23 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
[Issues]: https://docs.github.com/en/issues/tracking-your-work-with-issues/creating-an-issue
[Pull Requests]: https://docs.github.com/en/desktop/contributing-and-collaborating-using-github-desktop/working-with-your-remote-repository-on-github-or-github-enterprise/creating-an-issue-or-pull-request

# Unit Testing (nodejs) with SonarCloud and Conditional Triggers
# Test and Analyze with Triggers and SonarCloud

This action runs unit tests and optionally runs analysis, including coverage, using [SonarCloud](https://sonarcloud.io). SonarCloud can be configured to comment on pull requests or stop failing workflows.
This action runs tests, dependent on triggers, optionally sending results and coverage to [SonarCloud](https://sonarcloud.io). 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.

Expand All @@ -30,7 +30,7 @@ Only nodejs (JavaScript, TypeScript) is supported by this action. Please see ou
with:
### Required

# Commands to run unit tests
# Commands to run tests
# Please configure your app to generate coverage (coverage/lcov.info)
commands: |
npm ci
Expand All @@ -54,7 +54,7 @@ Only nodejs (JavaScript, TypeScript) is supported by this action. Please see ou

# Sonar token
# Available from sonarcloud.io or your organization administrator
# BCGov i.e. https://github.com/BCDevOps/devops-requests/issues/new/choose
# 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 }}
Expand All @@ -78,20 +78,25 @@ Only nodejs (JavaScript, TypeScript) is supported by this action. Please see ou
# 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 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 }}.
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/unit-tests.yml`
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.

```yaml
name: Unit Tests and Analysis
name: Test and Analyze

on:
pull_request:
Expand All @@ -109,7 +114,7 @@ concurrency:

jobs:
tests:
name: Run Unit Tests and Analyse
name: Test and Analyze
runs-on: ubuntu-22.04
steps:
- uses: bcgov-nr/action-test-and-analyse@main
Expand All @@ -127,14 +132,14 @@ jobs:
triggers: ('frontend/' 'charts/frontend')
```

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

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

```yaml
jobs:
tests:
name: Run Unit Tests and Analyse
name: Test and Analyze
runs-on: ubuntu-22.04
steps:
- uses: bcgov-nr/action-test-and-analyse@main
Expand All @@ -148,12 +153,12 @@ jobs:

# Example, Matrix / Multiple Directories with Sonar Cloud and Triggers

Unit 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.
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.

```yaml
jobs:
tests:
name: Unit Tests
name: Test and Analyze
runs-on: ubuntu-22.04
strategy:
matrix:
Expand All @@ -180,7 +185,7 @@ jobs:
-Dsonar.projectKey=bcgov-nr_action-test-and-analyse_${{ matrix.dir }}
sonar_token: ${{ secrets[matrix.token] }}
triggers: ${{ matrix.triggers }}
repository: bcgov/nr-quickstart-typescript
repository: bcgov/quickstart-openshift
branch: main
```

Expand All @@ -194,6 +199,10 @@ After sign up, a token should be available from your project on the [SonarCloud]

E.g. https://sonarcloud.io/project/configuration?id={<PROJECT>}&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](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#push), e.g. `("branch_protection_rule" "workflow_dispatch" "push")`.

# Feedback

Please contribute your ideas! [Issues] and [pull requests] are appreciated.
Expand Down
43 changes: 31 additions & 12 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
name: Unit Test and Analysis with SonarCloud
description: Run unit tests (current node only), can send to SonarCloud
name: Test and Analyze with Triggers and SonarCloud
description: Run node tests based on triggers, optional SonarCloud
branding:
icon: check-square
color: blue

inputs:
### Required
commands:
description: Commands to run unit tests, start with '|' for multi-line
description: Commands to run tests, start with '|' for multi-line
required: true

dir:
Expand All @@ -30,21 +30,25 @@ inputs:
description: Sonar token, provide unpopulated token for pre-setup (will skip)

triggers:
description: Paths used to trigger a build; e.g. ('./backend/' './frontend/)
description: Paths (array) used to trigger a build; e.g. ('./backend/' './frontend/)

### Usually a bad idea / not recommended
diff_branch:
description: Branch to diff against
default: ${{ github.event.repository.default_branch }}

repository:
description: Non-default repository to clone
description: Non-default repository to clone (used for testing this action)
default: ${{ github.repository }}

branch:
description: Optionally, specify a different branch to clone
description: Non-default branch to clone (used for testing this action)
default: ""

triggers_event:
description: Events (array) to use with triggers; e.g. ("pull_request" "push" "workflow_dispatch")
default: "('pull_request')"

runs:
using: composite
steps:
Expand Down Expand Up @@ -85,28 +89,43 @@ runs:
- id: vars
shell: bash
run: |
# Expand for inputs and variables
# Triggers and conditions

# Triggers
# Arrays must be stored first
TRIGGERS=${{ inputs.triggers }}
if [ -z "${TRIGGERS}" ]; then
T_EVENTS=${{ inputs.triggers_event }}

# Default to triggered=true
echo "triggered=true" >> $GITHUB_OUTPUT

# Run/trigger conditions
if [[ ! "${T_EVENTS}" =~ "${{ github.event_name }}" ]]
then
# Event doesn't match on type, so fire
echo "Event not matched, so always test"
exit 0
elif [ -z "${TRIGGERS}" ]
then
# Triggers omitted, so fire
echo "Triggers omitted, so always test"
echo "triggered=true" >> $GITHUB_OUTPUT
exit 0
else
# Check triggers against a git diff
echo "Processing triggers"
git fetch origin "${{ inputs.diff_branch }}"
while read -r check; do
for t in "${TRIGGERS[@]}"; do
if [[ "${check}" =~ "${t}" ]]; then
echo -e "Triggered: ${t}\n --> ${check}"
echo "triggered=true" >> $GITHUB_OUTPUT
exit 0
fi
done
done < <(git diff origin/"${{ inputs.diff_branch }}" --name-only)
fi

# Conditions not met, do not fire
echo "Triggers not matched, testing skipped"
echo "triggered=false" >> $GITHUB_OUTPUT

# Setup node and cache dir
- uses: actions/setup-node@v3
Expand Down Expand Up @@ -147,7 +166,7 @@ runs:

### Cleanup

# Fix - Docker takes ownership of files, causing a cleanup fail
# Fix - Docker can take file ownership, causing a cleanup fail
- shell: bash
if: steps.vars.outputs.triggered == 'true'
id: get_uid
Expand Down