Merge pull request #1405 from jenkins-infra/updatecli_main_b84fe2bd14… #4120
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Test Charts | |
on: | |
push: | |
branches: | |
- main | |
pull_request: null | |
workflow_dispatch: null | |
env: | |
UNITTEST_VERSION: v0.6.3 | |
jobs: | |
run_unit_tests_on_modified_charts: | |
runs-on: ubuntu-latest | |
name: Run tests on modified charts with unit tests | |
steps: | |
- uses: actions/checkout@v4 | |
- id: list_modified_charts_with_unit_tests | |
name: List modified charts with unit tests | |
env: | |
GH_TOKEN: ${{ github.token }} | |
GH_PR_NUMBER: ${{ github.event.pull_request.number }} | |
run: | | |
# Retrieve all charts by default | |
allChartsPaths=$(find . -maxdepth 2 -type d | grep "charts/" | cut -d "/" -f2-3 | sort --unique) | |
modifiedChartPaths=$allChartsPaths | |
if [ "$GITHUB_EVENT_NAME" == "pull_request" ]; then | |
# Retrieve the list of modified paths | |
modifiedPaths=$(gh pr view $GH_PR_NUMBER --json files -q '.files[].path') | |
echo "= list of modified path(s) from the pull request: $modifiedPaths" | |
# Retrieve chart names from modified files | |
modifiedChartPaths=$(echo ${modifiedPaths} | xargs dirname | grep "^charts/" | cut -d "/" -f1-2 | sort --unique) | |
# Test all charts if this workflow has been modified | |
modifiedWorkflow=$(echo ${modifiedPaths} | grep ".github/workflows/test.yml" || echo "") | |
if [ -n "${modifiedWorkflow}" ]; then | |
modifiedChartPaths=$allChartsPaths | |
echo "= workflow modified, including all charts" | |
fi | |
echo "= list of modified chart(s) from the pull request: $modifiedChartPaths" | |
fi | |
# Keep only modified charts with unit tests | |
modifiedChartsWithUnitTests="" | |
while IFS= read -r chartPath; do | |
if [ -d "${chartPath}/tests" ]; then | |
modifiedChartsWithUnitTests+=",$(echo $chartPath | cut -d '/' -f 2)" | |
fi | |
done <<< "$modifiedChartPaths" | |
# Remove first comma | |
modifiedChartsWithUnitTests=${modifiedChartsWithUnitTests#,} | |
# Return early if there is no modified chart with unit tests | |
if [ -z "$modifiedChartsWithUnitTests" ]; then | |
echo "= no modified chart with unit tests found." | |
else | |
echo "= modified chart(s) with unit tests: ${modifiedChartsWithUnitTests}" | |
fi | |
# Store the result | |
echo "modified_charts_with_unit_tests=${modifiedChartsWithUnitTests}" >> "$GITHUB_OUTPUT" | |
- id: tools_versions | |
name: Get Tools Versions | |
if: steps.list_modified_charts_with_unit_tests.outputs.modified_charts_with_unit_tests != '' | |
run: | | |
current_helm_version="$(curl --silent --location https://raw.githubusercontent.com/jenkins-infra/docker-helmfile/main/Dockerfile | grep 'ARG' | grep 'HELM_VERSION=' | sort -u | cut -d'=' -f2)" | |
echo "CURRENT_HELM_VERSION=${current_helm_version}" >> $GITHUB_OUTPUT | |
- id: setup_helm | |
name: Set up Helm | |
if: steps.list_modified_charts_with_unit_tests.outputs.modified_charts_with_unit_tests != '' | |
uses: azure/setup-helm@fe7b79cd5ee1e45176fcad797de68ecaf3ca4814 # v4.2.0 | |
with: | |
version: "${{ steps.tools_versions.outputs.CURRENT_HELM_VERSION }}" | |
- id: install_helm_unittests | |
name: install helm unittests | |
if: steps.list_modified_charts_with_unit_tests.outputs.modified_charts_with_unit_tests != '' | |
run: | | |
helm env | |
# Repeat 2 times to fight against network errors in GHA runners | |
# Always return true | |
helm plugin install https://github.com/helm-unittest/helm-unittest --version ${UNITTEST_VERSION} \ | |
|| helm plugin install https://github.com/helm-unittest/helm-unittest --version ${UNITTEST_VERSION} \ | |
|| true | |
# Fail if not installed | |
helm plugin list | grep unittest | |
- id: run_helm_unittests | |
name: run unit tests | |
if: steps.list_modified_charts_with_unit_tests.outputs.modified_charts_with_unit_tests != '' | |
working-directory: ./charts/ | |
run: | | |
# Set IFS to a comma to split the string into an array | |
IFS=',' read -ra chartNames <<< "${{ steps.list_modified_charts_with_unit_tests.outputs.modified_charts_with_unit_tests }}" | |
for chartName in "${chartNames[@]}"; do | |
helm unittest "$chartName" | |
done |