Files
DetectionLab/.circleci/config.yml
2018-12-22 15:32:43 -08:00

106 lines
4.6 KiB
YAML

version: 2
jobs:
hold:
machine: true
steps:
- run: echo "This build requires approval to continue..."
build:
machine: true
working_directory: ~/repo
steps:
- checkout
- run:
name: Choose which test suite to run based on which files were modified
command: |
## As far as I'm aware, there are three possibile cases to check for here. The CIRCLE_COMPARE_URL environment variable will either contain:
## - A SHA1 hash for the commit (If there is only a single commit on the branch)
## - Two SHA1 hashes in a "xxxxxxxx..yyyyyyyy" (comparing the two most recent commits
## - Nothing
## There is certainly a better way to accomplish all of this, but my limited knowledge of git has lead me to write this awful spaghetti code :-/
## We'll handle all 3 of these cases below:
## Checking commits for changes to Packer files
COMMIT_SHA1=""
PACKER_MODIFIED=0
VAGRANT_MODIFIED=0
## Check for empty CIRCLE_COMPARE_URL. If it is, set the SHA1 hash to the CIRCLE_SHA1 environment variable
if [ "$(echo -n $CIRCLE_COMPARE_URL | wc -c)" -eq 0 ]; then
export COMMIT_SHA1=$CIRCLE_SHA1
## Check for two short-hashes in the CIRCLE_COMPARE_URL by searching for '..'. If it exists, use the second short-hash
elif echo "$CIRCLE_COMPARE_URL" | grep '\.\.'; then
export COMMIT_SHA1="$(echo $CIRCLE_COMPARE_URL | cut -d '.' -f 5)"
## Check for a single short hash in the CIRCLE_COMPARE_URL and use it if it exists
## TODO: This check may not be needed.
else
export COMMIT_SHA1="$(echo $CIRCLE_COMPARE_URL | cut -d '/' -f 7)"
fi
## Display the files that were modified in this branch
echo "Files modified since origin/Master:"
git diff-tree --no-commit-id --name-only -r $(git rev-parse origin/HEAD) "$COMMIT_SHA1"
## Check to see if Packer files were modified
if [ "$(git diff-tree --no-commit-id --name-only -r $(git rev-parse origin/HEAD) "$COMMIT_SHA1" | grep -c ^Packer/)" -gt 0 ]; then
export PACKER_MODIFIED=1
fi
if [ "$(git diff-tree --no-commit-id --name-only -r $(git rev-parse origin/HEAD) "$COMMIT_SHA1" | grep -c ^Vagrant/)" -gt 0 ]; then
export VAGRANT_MODIFIED=1
fi
echo "Displaying the values of the modifier environment variables:"
echo "VAGRANT_MODIFIED=$VAGRANT_MODIFIED"
echo "PACKER_MODIFIED=$PACKER_MODIFIED"
## Choosing which test suite to run based on the files that were changed
if [[ "$PACKER_MODIFIED" -eq 1 ]] && [[ "$VAGRANT_MODIFIED" -eq 1 ]]; then
echo "Running the test suite for Packer and Vagrant changes"
chmod +x ci/circle_workflows/packer_and_vagrant_changes.sh
ci/circle_workflows/packer_and_vagrant_changes.sh
exit 0
fi
if [[ "$PACKER_MODIFIED" -eq 0 ]] && [[ "$VAGRANT_MODIFIED" -eq 0 ]]; then
echo "Running the default test suite (Vagrant-only)"
chmod +x ci/circle_workflows/vagrant_changes.sh
ci/circle_workflows/vagrant_changes.sh
exit 0
fi
if [ "$PACKER_MODIFIED" -eq 1 ]; then
echo "Running the test suite for Packer-only changes"
chmod +x ci/circle_workflows/packer_changes.sh
ci/circle_workflows/packer_changes.sh
exit 0
fi
if [ "$VAGRANT_MODIFIED" -eq 1 ]; then
echo "Running the test suite for Vagrant-only changes"
chmod +x ci/circle_workflows/vagrant_changes.sh
ci/circle_workflows/vagrant_changes.sh
exit 0
fi
- store_artifacts:
path: /tmp/artifacts
workflows:
version: 2
workflow:
jobs:
- hold:
type: approval
filters:
branches:
# Don't rebuild the PR after it has been merged into master
# Users cannot push directly to master as it is a protected branch
ignore: master
- build:
requires:
- hold
filters:
branches:
# Don't rebuild the PR after it has been merged into master
ignore: master
weekly-build:
jobs:
- build
triggers:
- schedule:
cron: "0 0 * * 0"
filters:
branches:
# Automatically re-build the contents of master once per week
only: master