Skip to content

Instantly share code, notes, and snippets.

@bpgould
Created October 18, 2022 02:13
Show Gist options
  • Save bpgould/d9355fa02d39486f3fcdf028611369b2 to your computer and use it in GitHub Desktop.
Save bpgould/d9355fa02d39486f3fcdf028611369b2 to your computer and use it in GitHub Desktop.
bash script to save directories with changed files in Travis CICD
#!/bin/bash
# this script will run in Travis CICD, identify changed files
# and save the directories of the files if a condition is met,
# it will then print the array of matching directories
if [[ "$TRAVIS_EVENT_TYPE" == "push" ]]; then
# collect only changed files from commit
files=($(git diff-tree --no-commit-id --name-only -r "$TRAVIS_COMMIT"))
elif [[ "$TRAVIS_EVENT_TYPE" == "pull_request" ]]; then
# collect all changed files from commit range
files=($(git diff-tree --no-commit-id --name-only -r origin/main -r "$TRAVIS_COMMIT"))
fi
# create list
directories=()
for file in "${files[@]}"; do
parent_dir=$(dirname -- "$file")
# I only want to collect subdirectories under a directory named teams/
# therefore the condition requires that the path has a directory named teams
if [[ $parent_dir != "." ]] && [[ $parent_dir == *"teams"* ]]; then
directories+=("$parent_dir")
fi
done
if [[ ${#directories[@]} -eq 0 ]]; then
echo "no matches"
else
printf "'%s'\n" "${directories[@]}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment