Skip to content

Commit

Permalink
feat(ci): use actions-diff-triggers (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
DerekRoberts authored Apr 10, 2024
1 parent 9a3a8ba commit a0bfbaf
Showing 1 changed file with 24 additions and 73 deletions.
97 changes: 24 additions & 73 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,6 @@ inputs:
runs:
using: composite
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
repository: ${{ inputs.repository }}
ref: ${{ inputs.branch }}

- name: Warnings for breaking changes
shell: bash
run: |
Expand All @@ -78,56 +72,18 @@ runs:
exit 1
fi
# Process variables and inputs
- id: vars
shell: bash
run: |
# Triggers and conditions
if [ "${{ inputs.java-cache }}" == "maven" ]; then
echo sonarCmd="mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar" >> $GITHUB_OUTPUT
elif [ "${{ inputs.java-cache }}" == "gradle" ]; then
echo sonarCmd="./gradlew build sonarqube --info" >> $GITHUB_OUTPUT
else
echo "ERROR: inputs.java-cache = ${{ inputs.java-cache }}"
exit 1
fi
# 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
- uses: actions/checkout@v4
with:
fetch-depth: 0
repository: ${{ inputs.repository }}
ref: ${{ inputs.branch }}

# Setup Java and cache dir
- uses: actions/setup-java@v4
Expand All @@ -139,38 +95,33 @@ runs:

# Run tests, hopefully generating coverage for SonarCloud
- name: Run Tests
if: steps.vars.outputs.triggered == 'true'
if: steps.diff.outputs.triggered == 'true'
shell: bash
working-directory: ${{ inputs.dir }}
run: |
# Run tests
cd ${{ inputs.dir }}
${{ inputs.commands }}
### Optional SonarCloud

- if: inputs.sonar_token && steps.vars.outputs.triggered == 'true'
- if: inputs.sonar_token && steps.diff.outputs.triggered == 'true'
env:
SONAR_TOKEN: ${{ inputs.sonar_token }}
shell: bash
working-directory: ${{ inputs.dir }}
run: |
# Run SonarCloud for ${{ inputs.java-cache }}
cd ${{ inputs.dir }}
${{ steps.vars.outputs.sonarCmd }} \
-Dsonar.host.url=https://sonarcloud.io ${{ inputs.sonar_args }}
### Cleanup
if [ "${{ inputs.java-cache }}" == "maven" ]; then
mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.host.url=https://sonarcloud.io ${{ inputs.sonar_args }}
elif [ "${{ inputs.java-cache }}" == "gradle" ]; then
gradlew build sonarqube --info -Dsonar.host.url=https://sonarcloud.io ${{ inputs.sonar_args }}
else
echo "ERROR: inputs.java-cache = ${{ inputs.java-cache }}"
exit 1
fi
# Fix - Docker can take file ownership, causing a cleanup fail
- shell: bash
if: steps.vars.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'
with:
user_id: ${{ steps.get_uid.outputs.uid }}
### Cleanup

# Fix - Clone for action.yml and other verifications
- name: Checkout Action repo to pass tests
Expand Down

0 comments on commit a0bfbaf

Please sign in to comment.