Skip to content

Instantly share code, notes, and snippets.

@faceleg
Last active August 25, 2018 00:42
Show Gist options
  • Save faceleg/e87d03b342310604e131bbf278cca5f4 to your computer and use it in GitHub Desktop.
Save faceleg/e87d03b342310604e131bbf278cca5f4 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
set -x
# Usage:
# ~/repo/ci/git/cancel-build-for-unchanged-directory.sh \
# chromecast \ # CHANGE_DIRECTORY
# https://devices.lightbox.co.nz/google-cast/version.json \ # REMOTE_PRODUCTION_VERSION_URL
# true # SPRINGBOARD_PASSTHROUGH
CHANGE_DIRECTORY="$1" # Required
REMOTE_PRODUCTION_VERSION_URL="$2" # Optional, if omitted comparison will be against origin/master
# Springboard branch pushes should always pass through in some cases
SPRINGBOARD_PASSTHROUGH="$3" # Optional, if present and true then springboard pushes will always build
if [[ "$CIRCLE_BRANCH" == "springboard" && "$SPRINGBOARD_PASSTHROUGH" == "true" ]]; then
echo "Always build $CHANGE_DIRECTORY for springboard, continue job"
exit 0
fi
# Check if there are changes in the $CHANGE_DIRECTORY folder
# If yes, continue build
# If no, cancel build
# shellcheck source=ci/git/cancel-build-for-unchanged-directory.sh
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# shellcheck source=ci/git/cancel-build-for-unchanged-directory.sh
source "$DIR/export-remote-version.sh" "$REMOTE_PRODUCTION_VERSION_URL"
continue_building_for_changes() {
CONTINUE_CHANGE_DIRECTORY="$1"
CURRENT_PROJECT_NAME="$2"
if git --no-pager diff --name-only "$REMOTE_VERSION...$CIRCLE_SHA1" | grep "^$CONTINUE_CHANGE_DIRECTORY" ; then
echo "Changes in $CONTINUE_CHANGE_DIRECTORY, continue building $CURRENT_PROJECT_NAME"
exit 0
fi
}
continue_building_for_changes "$CHANGE_DIRECTORY" "$CHANGE_DIRECTORY"
continue_building_for_changes ".circleci/" "$CHANGE_DIRECTORY"
continue_building_for_changes "ci/" "$CHANGE_DIRECTORY"
echo "No changes to $CHANGE_DIRECTORY, .circleci/ or ci/, cancelling build"
curl --silent -X POST "$CIRCLE_CI_URL/api/v1.1/project/github/lightboxnz/repo/$CIRCLE_BUILD_NUM/cancel?circle-token=$CIRCLE_TOKEN" \
| jq .outcome
sleep 10
... jobs
# chromecast cc build
cc-gatekeeper:
executor: exec-gatekeeper
steps:
- checkout
- run: ~/repo/ci/git/cancel-build-for-unchanged-directory.sh chromecast https://devices.lightbox.co.nz/google-cast/version.json true
cc-test:
executor: exec-defaults
steps:
- chromecast
- run: cd ~/repo/chromecast && yarn run coverage
- run: cd ~/repo && codecov
- store_artifacts:
path: ~/repo/chromecast/coverage/lcov-report
prefix: cc-test-coverage-report
- store_test_results:
path: ~/repo/chromecast/reports
... further jobs
... workflows
# chromecast cc
build-chromecast:
jobs:
- cc-gatekeeper:
filters:
branches:
only:
- /feature\/dev[0-9]\/.*/
- /hotfix\/dev[0-9]\/.*/
- springboard
- cc-test:
requires:
- cc-gatekeeper
- ... further jobs
#!/bin/bash
set -e
set -x
# Check if there are changes in the $1 folder
# If yes, continue build
# If no, cancel build
# If a $1 arg was passed, assume it is a URL to a version.json that has a top level sha1 key
REMOTE_VERSION_URL=$1
REMOTE_VERSION='origin/master'
if [[ ! -z "$REMOTE_VERSION_URL" ]]; then
curl --silent "$REMOTE_VERSION_URL"
curl --silent "$REMOTE_VERSION_URL" | jq --raw-output .sha1
REMOTE_VERSION=$(curl --silent "$REMOTE_VERSION_URL" | jq --raw-output .sha1)
fi
export REMOTE_VERSION="$REMOTE_VERSION"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment