Skip to content

Commit

Permalink
Gha test only modified charts (#16)
Browse files Browse the repository at this point in the history
* feat: test only modified charts with unit tests on pull requests

* run only on charts/** changes

* small improvements replicated from jenkins-infra#598
  • Loading branch information
lemeurherve authored Aug 12, 2023
1 parent f0f0c6e commit 5563183
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
runs-on: ubuntu-latest
outputs:
charts_matrix: ${{ steps.list_modified_charts_with_unit_tests.outputs.modified_charts_with_unit_tests }}
name: Find modified charts with unit tests
steps:
- uses: actions/checkout@v3
- id: list_modified_charts_with_unit_tests
Expand All @@ -25,38 +26,39 @@ jobs:
run: |
if [ "$GITHUB_EVENT_NAME" == "pull_request" ]; then
# Retrieve the list of modified chart names in the pull request
modifiedChartPaths=$(gh pr view ${{ github.event.pull_request.number }} --json files -q '.files[].path' | xargs dirname | cut -d "/" -f1-2 | grep "^charts/" | sort --unique)
modifiedChartPaths=$(gh pr view ${{ github.event.pull_request.number }} --json files -q '.files[].path' | xargs dirname | grep "^charts/" | cut -d "/" -f1-2 | sort --unique)
else
# Retrieve all charts
modifiedChartPaths=$(find . -maxdepth 2 -type d | cut -d "/" -f2-3 | grep "^charts/" | sort --unique)
modifiedChartPaths=$(find . -maxdepth 2 -type d | grep "^charts/" | cut -d "/" -f2-3 | sort --unique)
# TODO: Retrieve only the list of modified chart names in the last commit since the push
fi
# Keep only modified charts with unit tests
modifiedChartsWithUnitTests=()
modifiedChartsWithUnitTestsArray=()
while IFS= read -r chartName; do
if [ -d "${chartName}/tests" ]; then
modifiedChartsWithUnitTests+=("$(echo $chartName | cut -d '/' -f 2)")
modifiedChartsWithUnitTestsArray+=("$(echo $chartName | cut -d '/' -f 2)")
fi
done <<< "$modifiedChartPaths"
modifiedChartsWithUnitTests=$(printf "%s\n" "${modifiedChartsWithUnitTestsArray[@]}")
jsonArray=""
if [ -n "$modifiedChartsWithUnitTests" ]; then
echo "= List of modified charts with unit tests:"
echo "$modifiedChartsWithUnitTests"
# Transform the array into a JSON
jsonArray=$(printf "%s\n" "${modifiedChartsWithUnitTests[@]}" | jq --raw-input --slurp 'split("\n") | map(select(length > 0))' | tr -d '\n')
jsonContent="{\"chart\": ${jsonArray}}"
# Store the JSON as step output
echo "modified_charts_with_unit_tests=$jsonContent" >> "$GITHUB_OUTPUT"
jsonArray=$(echo "$modifiedChartsWithUnitTests" | jq --raw-input --slurp 'split("\n") | map(select(length > 0))' | tr -d '\n')
else
echo "= no modified chart with unit tests found."
# Store the JSON as step output
echo "modified_charts_with_unit_tests=''" >> "$GITHUB_OUTPUT"
fi
# Store the JSON as step output
echo "modified_charts_with_unit_tests={\"chart\": ${jsonArray}}" >> "$GITHUB_OUTPUT"
run-unit-tests:
runs-on: ubuntu-latest
needs: find-modified-charts-with-unit-tests
if: ${{ needs.find-modified-charts-with-unit-tests.outputs.charts_matrix != '' }} # Without it, the strategy parser will fail if the changed_directories is empty.
name: Run unit tests
strategy:
matrix: ${{fromJson(needs.find-modified-charts-with-unit-tests.outputs.charts_matrix)}}
fail-fast: false
Expand Down

0 comments on commit 5563183

Please sign in to comment.