|
| 1 | +#!/bin/bash |
| 2 | +# Copyright (c) HashiCorp, Inc. |
| 3 | +# SPDX-License-Identifier: MPL-2.0 |
| 4 | + |
| 5 | +set -euo pipefail |
| 6 | + |
| 7 | +# Get the list of changed files |
| 8 | +# Using `git merge-base` ensures that we're always comparing against the correct branch point. |
| 9 | +#For example, given the commits: |
| 10 | +# |
| 11 | +# A---B---C---D---W---X---Y---Z # origin/main |
| 12 | +# \---E---F # feature/branch |
| 13 | +# |
| 14 | +# ... `git merge-base origin/$SKIP_CHECK_BRANCH HEAD` would return commit `D` |
| 15 | +# `...HEAD` specifies from the common ancestor to the latest commit on the current branch (HEAD).. |
| 16 | +files_to_check=$(git diff --name-only "$(git merge-base origin/$SKIP_CHECK_BRANCH HEAD~)"...HEAD) |
| 17 | + |
| 18 | +# Define the directories to check |
| 19 | +skipped_directories=("assets" ".changelog/") |
| 20 | + |
| 21 | +# Loop through the changed files and find directories/files outside the skipped ones |
| 22 | +files_to_check_array=($files_to_check) |
| 23 | +for file_to_check in "${files_to_check_array[@]}"; do |
| 24 | + file_is_skipped=false |
| 25 | + echo "checking file: $file_to_check" |
| 26 | + |
| 27 | + # Allow changes to: |
| 28 | + # - This script |
| 29 | + # - Files in the skipped directories |
| 30 | + # - Markdown files |
| 31 | + for dir in "${skipped_directories[@]}"; do |
| 32 | + if [[ "$file_to_check" == */check_skip_ci.sh ]] || |
| 33 | + [[ "$file_to_check" == "$dir"* ]] || |
| 34 | + [[ "$file_to_check" == *.md ]]; then |
| 35 | + file_is_skipped=true |
| 36 | + break |
| 37 | + fi |
| 38 | + done |
| 39 | + |
| 40 | + if [ "$file_is_skipped" != "true" ]; then |
| 41 | + echo -e "non-skippable file changed: $file_to_check" |
| 42 | + SKIP_CI=false |
| 43 | + echo "Changes detected in non-documentation files - will not skip tests and build" |
| 44 | + echo "skip-ci=false" >> "$GITHUB_OUTPUT" |
| 45 | + exit 0 ## if file is outside of the skipped_directory exit script |
| 46 | + fi |
| 47 | +done |
| 48 | + |
| 49 | +echo "Changes detected in only documentation files - skipping tests and build" |
| 50 | +echo "skip-ci=true" >> "$GITHUB_OUTPUT" |
0 commit comments