Skip to content

Instantly share code, notes, and snippets.

@yu-iskw
Last active October 2, 2020 06:39
Show Gist options
  • Save yu-iskw/b52ff86b013a00b8daec6da94be0c161 to your computer and use it in GitHub Desktop.
Save yu-iskw/b52ff86b013a00b8daec6da94be0c161 to your computer and use it in GitHub Desktop.
Reusable CircleCI command to halt a job if target files are not changed
# .circleci/config.yml
commands:
check-diff-files-to-halt:
description: "halt CircleCI step if there is any files matched with a pattern"
parameters:
pattern:
description: "regular expression to filter changed files"
type: string
steps:
- run: |
# `|| :` is used to avoid exit when there is no changed file which matches the pattern.
changed_files=$(bash ./ci/get_changed_files.sh "<< parameters.pattern >>" || :)
num_changed_files=$(echo "$changed_files" | grep -v -e '^\s*$' | wc -l || :)
if [[ $num_changed_files -eq 0 ]] ; then
echo "Halt due to no changed file"
circleci step halt
else
echo "Changed files match the pattern << parameters.pattern >>"
echo $changed_files
fi
jobs:
test-basics:
docker:
- image: python3.8
steps:
- checkout
# Halt the job, if any file in `./src/` is not changed
- check-diff-files-to-halt:
pattern: "^src/"
- run:
name: Run unit tests
command: pytest -v -s ./src/tests/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment