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: bcgov-nr/diff-action-triggers #49

Merged
merged 2 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ jobs:
- name: frontend-pr
dir: frontend
token: SONAR_TOKEN_FRONTEND
triggers: ('.')
triggers_event: ('push')
steps:
- uses: actions/checkout@v4
Expand Down
74 changes: 19 additions & 55 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,6 @@ inputs:
runs:
using: composite
steps:
# Shallow clone is faster, but SonarCloud requires a full clone
- uses: actions/checkout@v4
with:
fetch-depth: 0
repository: ${{ inputs.repository }}
ref: ${{ inputs.branch }}

- name: Warnings for breaking changes
shell: bash
run: |
Expand All @@ -85,67 +78,39 @@ runs:
exit 1
fi

# Process variables and inputs
- id: vars
shell: bash
run: |
# Triggers and conditions

# Arrays must be stored first
TRIGGERS=${{ inputs.triggers }}
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"
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}"
exit 0
fi
done
done < <(git diff origin/"${{ inputs.diff_branch }}" --name-only)
fi
# Send triggers to diff action
- id: diff
uses: bcgov-nr/[email protected]
with:
triggers: ${{ inputs.triggers }}
diff_branch: ${{ inputs.diff_branch }}

# Conditions not met, do not fire
echo "Triggers not matched, testing skipped"
echo "triggered=false" >> $GITHUB_OUTPUT
# Shallow clone is faster, but SonarCloud requires a full clone
- uses: actions/checkout@v4
with:
fetch-depth: 0
repository: ${{ inputs.repository }}
ref: ${{ inputs.branch }}

# Setup node and cache dir
- uses: actions/setup-node@v4
if: steps.vars.outputs.triggered == 'true'
if: steps.diff.outputs.triggered == 'true'
with:
node-version: ${{ inputs.node_version }}
- id: npm-cache-dir
if: steps.vars.outputs.triggered == 'true'
if: steps.diff.outputs.triggered == 'true'
shell: bash
run: echo "dir=$(npm config get cache)" >> $GITHUB_OUTPUT
- uses: actions/cache@v4
if: steps.vars.outputs.triggered == 'true'
if: steps.diff.outputs.triggered == 'true'
with:
path: ${{ steps.npm-cache-dir.outputs.dir }}
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: ${{ runner.os }}-node-

# Run tests, hopefully generating coverage for SonarCloud
- name: Run Tests
if: steps.vars.outputs.triggered == 'true'
if: steps.diff.outputs.triggered == 'true'
shell: bash
run: |
cd ${{ inputs.dir }}
Expand All @@ -154,8 +119,7 @@ runs:
### Optional SonarCloud

# If sonar_token
- name: SonarCloud Scan
if: inputs.sonar_token && steps.vars.outputs.triggered == 'true'
- if: inputs.sonar_token && steps.diff.outputs.triggered == 'true'
uses: SonarSource/[email protected]
env:
SONAR_TOKEN: ${{ inputs.sonar_token }}
Expand All @@ -168,13 +132,13 @@ runs:

# Fix - Docker can take file ownership, causing a cleanup fail
- shell: bash
if: steps.vars.outputs.triggered == 'true'
if: steps.diff.outputs.triggered == 'true'
id: get_uid
run: |
# User for workstation ownership reset/fix
echo "uid=$(id -u ${USER})" >> $GITHUB_OUTPUT
- uses: peter-murray/reset-workspace-ownership-action@v1
if: steps.vars.outputs.triggered == 'true'
if: steps.diff.outputs.triggered == 'true'
with:
user_id: ${{ steps.get_uid.outputs.uid }}

Expand Down