From 31b1ac05686f40adf6bb2d7084eedb51f66f8a2c Mon Sep 17 00:00:00 2001 From: hc-github-team-consul-core Date: Wed, 23 Aug 2023 14:39:22 -0500 Subject: [PATCH] Backport of NET-5382 & PLAT-1159: Do not trigger workflow if only doc files are in commit history into release/1.16.x (#18570) backport of commit c833fcc3650e7cd4e9198d1202aafb17ed2827d2 Co-authored-by: NiniOak --- .../scripts/filter_changed_files_go_test.sh | 33 +++++++++++++++++++ .github/workflows/go-tests.yml | 18 +++++++++- .github/workflows/test-integrations.yml | 18 +++++++++- 3 files changed, 67 insertions(+), 2 deletions(-) create mode 100755 .github/scripts/filter_changed_files_go_test.sh diff --git a/.github/scripts/filter_changed_files_go_test.sh b/.github/scripts/filter_changed_files_go_test.sh new file mode 100755 index 000000000..d83e4a094 --- /dev/null +++ b/.github/scripts/filter_changed_files_go_test.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +# Get the list of changed files +files_to_check=$(git diff --name-only origin/$GITHUB_BASE_REF) + +# Define the directories to check +skipped_directories=("docs/" "ui/" "website/" "grafana/") + +# Initialize a variable to track directories outside the skipped ones +other_directories="" +trigger_ci=false + +# Loop through the changed files and find directories/files outside the skipped ones +for file_to_check in $files_to_check; do + file_is_skipped=false + for dir in "${skipped_directories[@]}"; do + if [[ "$file_to_check" == "$dir"* ]] || [[ "$file_to_check" == *.md && "$dir" == *"/" ]]; then + file_is_skipped=true + break + fi + done + if [ "$file_is_skipped" = "false" ]; then + other_directories+="$(dirname "$file_to_check")\n" + trigger_ci=true + echo "Non doc file(s) changed - triggered ci: $trigger_ci" + echo -e $other_directories + echo "trigger-ci=$trigger_ci" >>"$GITHUB_OUTPUT" + exit 0 ## if file is outside of the skipped_directory exit script + fi +done + +echo "Only doc file(s) changed - triggered ci: $trigger_ci" +echo "trigger-ci=$trigger_ci" >>"$GITHUB_OUTPUT" diff --git a/.github/workflows/go-tests.yml b/.github/workflows/go-tests.yml index ed0f5dd1d..ce2381930 100644 --- a/.github/workflows/go-tests.yml +++ b/.github/workflows/go-tests.yml @@ -24,8 +24,23 @@ env: GOPRIVATE: github.com/hashicorp # Required for enterprise deps jobs: + conditional-skip: + runs-on: ubuntu-latest + name: Get files changed and conditionally skip CI + outputs: + trigger-ci: ${{ steps.read-files.outputs.trigger-ci }} + steps: + - uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3 + with: + fetch-depth: 0 + - name: Get changed files + id: read-files + run: ./.github/scripts/filter_changed_files_go_test.sh + setup: + needs: [conditional-skip] name: Setup + if: needs.conditional-skip.outputs.trigger-ci == 'true' runs-on: ubuntu-latest outputs: compute-small: ${{ steps.setup-outputs.outputs.compute-small }} @@ -463,6 +478,7 @@ jobs: go-tests-success: needs: + - conditional-skip - setup - check-generated-deep-copy - check-generated-protobuf @@ -485,7 +501,7 @@ jobs: - go-test-32bit # - go-test-s390x runs-on: ${{ fromJSON(needs.setup.outputs.compute-small) }} - if: ${{ always() }} + if: always() && needs.conditional-skip.outputs.trigger-ci == 'true' steps: - name: evaluate upstream job results run: | diff --git a/.github/workflows/test-integrations.yml b/.github/workflows/test-integrations.yml index 98f91d142..510eaf2be 100644 --- a/.github/workflows/test-integrations.yml +++ b/.github/workflows/test-integrations.yml @@ -26,9 +26,24 @@ env: GOPRIVATE: github.com/hashicorp # Required for enterprise deps jobs: + conditional-skip: + runs-on: ubuntu-latest + name: Get files changed and conditionally skip CI + outputs: + trigger-ci: ${{ steps.read-files.outputs.trigger-ci }} + steps: + - uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3 + with: + fetch-depth: 0 + - name: Get changed files + id: read-files + run: ./.github/scripts/filter_changed_files_go_test.sh + setup: + needs: [conditional-skip] runs-on: ubuntu-latest name: Setup + if: needs.conditional-skip.outputs.trigger-ci == 'true' outputs: compute-small: ${{ steps.runners.outputs.compute-small }} compute-medium: ${{ steps.runners.outputs.compute-medium }} @@ -462,6 +477,7 @@ jobs: test-integrations-success: needs: + - conditional-skip - setup - dev-build - nomad-integration-test @@ -470,7 +486,7 @@ jobs: - envoy-integration-test - compatibility-integration-test runs-on: ${{ fromJSON(needs.setup.outputs.compute-small) }} - if: ${{ always() }} + if: always() && needs.conditional-skip.outputs.trigger-ci == 'true' steps: - name: evaluate upstream job results run: |