Skip to content

Commit 4b38a70

Browse files
authored
feat: param for triggers_event, default to pull_request (#36)
1 parent 150d093 commit 4b38a70

File tree

3 files changed

+70
-32
lines changed

3 files changed

+70
-32
lines changed

.github/workflows/unit-tests.yml renamed to .github/workflows/tests.yml

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Unit Tests and Analysis
1+
name: Tests
22

33
on:
44
pull_request:
@@ -16,17 +16,26 @@ concurrency:
1616

1717
jobs:
1818
tests:
19-
name: Unit Tests
19+
name: Tests
2020
runs-on: ubuntu-22.04
2121
strategy:
2222
matrix:
23-
dir: [backend, frontend]
23+
name: [backend, frontend-merge, frontend-pr]
2424
include:
25-
- dir: backend
25+
- name: backend
26+
dir: backend
2627
token: SONAR_TOKEN_BACKEND
2728
triggers: ('backend/')
28-
- dir: frontend
29+
triggers_event: ('pull_request')
30+
- name: frontend-merge
31+
dir: frontend
32+
triggers: ('.')
33+
triggers_event: ('pull_request')
34+
- name: frontend-pr
35+
dir: frontend
2936
token: SONAR_TOKEN_FRONTEND
37+
triggers: ('.')
38+
triggers_event: ('push')
3039
steps:
3140
- uses: actions/checkout@v3
3241
- uses: ./
@@ -36,7 +45,7 @@ jobs:
3645
npm run test:cov
3746
dir: ${{ matrix.dir }}
3847
node_version: "20"
39-
repository: bcgov/nr-quickstart-typescript
48+
repository: bcgov/quickstart-openshift
4049
sonar_args: >
4150
-Dsonar.exclusions=**/coverage/**,**/node_modules/**,**/*spec.ts
4251
-Dsonar.javascript.lcov.reportPaths=./coverage/lcov.info
@@ -46,3 +55,4 @@ jobs:
4655
-Dsonar.tests.inclusions=**/*spec.ts
4756
sonar_token: ${{ secrets[matrix.token] }}
4857
triggers: ${{ matrix.triggers }}
58+
triggers_event: ${{ matrix.triggers_event }}

README.md

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
[Issues]: https://docs.github.com/en/issues/tracking-your-work-with-issues/creating-an-issue
1616
[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
1717

18-
# Unit Testing (nodejs) with SonarCloud and Conditional Triggers
18+
# Test and Analyze with Triggers and SonarCloud
1919

20-
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.
20+
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.
2121

2222
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.
2323

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

33-
# Commands to run unit tests
33+
# Commands to run tests
3434
# Please configure your app to generate coverage (coverage/lcov.info)
3535
commands: |
3636
npm ci
@@ -54,7 +54,7 @@ Only nodejs (JavaScript, TypeScript) is supported by this action. Please see ou
5454
5555
# Sonar token
5656
# Available from sonarcloud.io or your organization administrator
57-
# BCGov i.e. https://github.com/BCDevOps/devops-requests/issues/new/choose
57+
# BCGov uses https://github.com/BCDevOps/devops-requests/issues/new/choose
5858
# Provide an unpopulated token for pre-setup, section will be skipped
5959
sonar_token:
6060
description: ${{ secrets.SONAR_TOKEN }}
@@ -78,20 +78,25 @@ Only nodejs (JavaScript, TypeScript) is supported by this action. Please see ou
7878
# Useful for consuming non-default branches, like in testing
7979
# Defants to empty, cloning the default branch
8080
branch: ""
81+
82+
# Bash array of events for limiting triggers, otherwise trigger automatically
83+
# E.g. ("pull_request" "push" "workflow_dispatch")
84+
# Defaults to only using triggers with pull requests
85+
triggers_event: "('pull_request')"
8186
```
8287
8388
# Example, Single Directory with SonarCloud Analysis
8489
85-
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 }}.
90+
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 }}.
8691
8792
The specified triggers will be used to decide whether this job runs tests and analysis or just exists successfully.
8893
89-
Create or modify a GitHub workflow, like below. E.g. `./github/workflows/unit-tests.yml`
94+
Create or modify a GitHub workflow, like below. E.g. `./github/workflows/tests.yml`
9095

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

9398
```yaml
94-
name: Unit Tests and Analysis
99+
name: Test and Analyze
95100
96101
on:
97102
pull_request:
@@ -109,7 +114,7 @@ concurrency:
109114
110115
jobs:
111116
tests:
112-
name: Run Unit Tests and Analyse
117+
name: Test and Analyze
113118
runs-on: ubuntu-22.04
114119
steps:
115120
- uses: bcgov-nr/action-test-and-analyse@main
@@ -127,14 +132,14 @@ jobs:
127132
triggers: ('frontend/' 'charts/frontend')
128133
```
129134

130-
# Example, Only Running Unit Tests (No SonarCloud), No Triggers
135+
# Example, Only Running Tests (No SonarCloud), No Triggers
131136

132-
No triggers are provided so unit tests will always run. SonarCloud is skipped.
137+
No triggers are provided so tests will always run. SonarCloud is skipped.
133138

134139
```yaml
135140
jobs:
136141
tests:
137-
name: Run Unit Tests and Analyse
142+
name: Test and Analyze
138143
runs-on: ubuntu-22.04
139144
steps:
140145
- uses: bcgov-nr/action-test-and-analyse@main
@@ -148,12 +153,12 @@ jobs:
148153

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

151-
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.
156+
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.
152157

153158
```yaml
154159
jobs:
155160
tests:
156-
name: Unit Tests
161+
name: Test and Analyze
157162
runs-on: ubuntu-22.04
158163
strategy:
159164
matrix:
@@ -180,7 +185,7 @@ jobs:
180185
-Dsonar.projectKey=bcgov-nr_action-test-and-analyse_${{ matrix.dir }}
181186
sonar_token: ${{ secrets[matrix.token] }}
182187
triggers: ${{ matrix.triggers }}
183-
repository: bcgov/nr-quickstart-typescript
188+
repository: bcgov/quickstart-openshift
184189
branch: main
185190
```
186191

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

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

202+
# Triggers and Triggers_Event
203+
204+
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")`.
205+
197206
# Feedback
198207

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

action.yml

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
name: Unit Test and Analysis with SonarCloud
2-
description: Run unit tests (current node only), can send to SonarCloud
1+
name: Test and Analyze with Triggers and SonarCloud
2+
description: Run node tests based on triggers, optional SonarCloud
33
branding:
44
icon: check-square
55
color: blue
66

77
inputs:
88
### Required
99
commands:
10-
description: Commands to run unit tests, start with '|' for multi-line
10+
description: Commands to run tests, start with '|' for multi-line
1111
required: true
1212

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

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

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

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

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

48+
triggers_event:
49+
description: Events (array) to use with triggers; e.g. ("pull_request" "push" "workflow_dispatch")
50+
default: "('pull_request')"
51+
4852
runs:
4953
using: composite
5054
steps:
@@ -85,28 +89,43 @@ runs:
8589
- id: vars
8690
shell: bash
8791
run: |
88-
# Expand for inputs and variables
92+
# Triggers and conditions
8993
90-
# Triggers
94+
# Arrays must be stored first
9195
TRIGGERS=${{ inputs.triggers }}
92-
if [ -z "${TRIGGERS}" ]; then
96+
T_EVENTS=${{ inputs.triggers_event }}
97+
98+
# Default to triggered=true
99+
echo "triggered=true" >> $GITHUB_OUTPUT
100+
101+
# Run/trigger conditions
102+
if [[ ! "${T_EVENTS}" =~ "${{ github.event_name }}" ]]
103+
then
104+
# Event doesn't match on type, so fire
105+
echo "Event not matched, so always test"
106+
exit 0
107+
elif [ -z "${TRIGGERS}" ]
108+
then
109+
# Triggers omitted, so fire
93110
echo "Triggers omitted, so always test"
94-
echo "triggered=true" >> $GITHUB_OUTPUT
95111
exit 0
96112
else
113+
# Check triggers against a git diff
97114
echo "Processing triggers"
98115
git fetch origin "${{ inputs.diff_branch }}"
99116
while read -r check; do
100117
for t in "${TRIGGERS[@]}"; do
101118
if [[ "${check}" =~ "${t}" ]]; then
102119
echo -e "Triggered: ${t}\n --> ${check}"
103-
echo "triggered=true" >> $GITHUB_OUTPUT
104120
exit 0
105121
fi
106122
done
107123
done < <(git diff origin/"${{ inputs.diff_branch }}" --name-only)
108124
fi
125+
126+
# Conditions not met, do not fire
109127
echo "Triggers not matched, testing skipped"
128+
echo "triggered=false" >> $GITHUB_OUTPUT
110129
111130
# Setup node and cache dir
112131
- uses: actions/setup-node@v3
@@ -147,7 +166,7 @@ runs:
147166
148167
### Cleanup
149168

150-
# Fix - Docker takes ownership of files, causing a cleanup fail
169+
# Fix - Docker can take file ownership, causing a cleanup fail
151170
- shell: bash
152171
if: steps.vars.outputs.triggered == 'true'
153172
id: get_uid

0 commit comments

Comments
 (0)