2018-07-18 08:30:52 +03:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# Exit with error if diff with origin/master only contains files mentioned in
|
|
|
|
# .ciignore so that the build can be stopped.
|
|
|
|
#
|
|
|
|
# Adapted from:
|
|
|
|
# https://circleci.com/blog/circleci-hacks-automate-the-decision-to-skip-builds-using-a-git-hook/
|
|
|
|
# https://github.com/dollarshaveclub/harmless-changes/blob/master/index.sh
|
|
|
|
|
|
|
|
set -eo pipefail
|
|
|
|
ROOT="$(readlink -f ${BASH_SOURCE[0]%/*}/../)"
|
|
|
|
|
2018-07-20 23:08:16 +03:00
|
|
|
# always build tagged builds
|
|
|
|
if [[ ! -z "$CIRCLE_TAG" ]]; then
|
|
|
|
echo "Skipping check for tags"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
2018-07-18 14:43:37 +03:00
|
|
|
# always build default branch
|
|
|
|
if [[ "$CIRCLE_BRANCH" == "master" ]]; then
|
|
|
|
echo "Skipping check for master branch"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
2018-07-18 08:30:52 +03:00
|
|
|
if [[ ! -a "$ROOT/.ciignore" ]]; then
|
2018-07-18 14:43:37 +03:00
|
|
|
echo "Skipping check since .ciignore is not found"
|
2018-07-18 08:30:52 +03:00
|
|
|
exit # If .ciignore doesn't exists, just quit this script
|
|
|
|
fi
|
|
|
|
|
2018-07-18 14:43:37 +03:00
|
|
|
# Check CIRCLE_COMPARE_URL first and if its not set, check for diff with master.
|
2018-07-18 08:46:52 +03:00
|
|
|
if [[ ! -z "$CIRCLE_COMPARE_URL" ]]; then
|
|
|
|
# CIRCLE_COMPARE_URL is not empty, use it to get the diff
|
|
|
|
COMMIT_RANGE=$(echo $CIRCLE_COMPARE_URL | sed 's:^.*/compare/::g')
|
|
|
|
echo "Diff: $COMMIT_RANGE"
|
|
|
|
changes="$(git diff $COMMIT_RANGE --name-only)"
|
|
|
|
else
|
|
|
|
# CIRCLE_COMPARE_URL is not set, diff with origin/master
|
|
|
|
echo "Diff: origin/master..HEAD"
|
|
|
|
changes="$(git diff-tree --no-commit-id --name-only -r origin/master..HEAD)"
|
|
|
|
fi
|
2018-07-18 08:30:52 +03:00
|
|
|
|
2018-07-18 08:46:52 +03:00
|
|
|
echo "Changes in this build:"
|
2018-07-18 08:30:52 +03:00
|
|
|
echo $changes
|
|
|
|
echo
|
|
|
|
|
|
|
|
# Load the patterns we want to skip into an array
|
|
|
|
mapfile -t blacklist < "$ROOT/.ciignore"
|
|
|
|
|
|
|
|
for i in "${blacklist[@]}"
|
|
|
|
do
|
|
|
|
# Remove the current pattern from the list of changes
|
|
|
|
changes=( ${changes[@]/$i/} )
|
|
|
|
|
|
|
|
if [[ ${#changes[@]} -eq 0 ]]; then
|
|
|
|
# If we've exhausted the list of changes before we've finished going
|
|
|
|
# through patterns, that's okay, just quit the loop
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ ${#changes[@]} -gt 0 ]]; then
|
|
|
|
# If there's still changes left, then we have stuff to build, leave the commit alone.
|
|
|
|
echo "Files that are not ignored present in commits, need to build, succeed the job"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Only ignored files are present in commits, no need to build, fail the job"
|
|
|
|
exit 1
|