graphql-engine/.circleci/ciignore.sh
Shahidh K Muhammed 17183caed4
adds a check to see if the jobs should be run or not (close #1161) (#1705)
CircleCI jobs are run for any PR that is submitted to the repo. This PR adds a check to decide whether the job should be run or not.

Figured out that CircleCI has a way to gracefully terminate a job:
```
circleci-agent step halt
```

A `.ciignore` file is ran against all the changes in the PR to decide whether the PR should be built or not. If the answer comes out as `no`, a file is written at `/buid/skip_job.txt`. This is done in the `check_build_worthiness` step.

All further jobs, in the beginning, looks for this file and gracefully terminates the job if this file is present. The directory is passed down to the jobs as the workspace.

```yaml
  skip_job_on_ciignore: &skip_job_on_ciignore
    run: |
      if [ -f /build/skip_job.txt ]; then
        echo "halting job due to /build/skip_job.txt"
        circleci-agent step halt
      fi
```

ref: https://support.circleci.com/hc/en-us/articles/360015562253-Conditionally-end-a-running-job-gracefully

There are some known issues on jobs that are run when PR is merged to master, need to address them after this PR is merged.
2019-03-07 11:28:03 +05:30

78 lines
2.3 KiB
Bash
Executable File

#!/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]%/*}/../)"
# make build directory
mkdir -p /build/ciignore
# always build tagged builds
if [[ ! -z "$CIRCLE_TAG" ]]; then
echo "Skipping check for tags"
exit
fi
# # always build default branch
# if [[ "$CIRCLE_BRANCH" == "master" ]]; then
# echo "Skipping check for master branch"
# exit
# fi
if [[ ! -a "$ROOT/.ciignore" ]]; then
echo "Skipping check since .ciignore is not found"
exit # If .ciignore doesn't exists, just quit this script
fi
# Check CIRCLE_COMPARE_URL first and if its not set, check for diff with master.
if [[ ! -z "$CIRCLE_COMPARE_URL" ]]; then
# CIRCLE_COMPARE_URL is not empty, use it to get the diff
if [[ $CIRCLE_COMPARE_URL = *"commit"* ]]; then
COMMIT_RANGE=$(echo $CIRCLE_COMPARE_URL | sed 's:^.*/commit/::g')~1
else
COMMIT_RANGE=$(echo $CIRCLE_COMPARE_URL | sed 's:^.*/compare/::g')
fi
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
echo "Changes in this build:"
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, build is not required, write the skip_job file"
echo "true" > /build/ciignore/skip_job.txt
exit