Last active
February 20, 2019 20:39
-
-
Save sambauers/f096f3fa411a252eae10332a57b17f46 to your computer and use it in GitHub Desktop.
Splits tests across multiple Circle CI nodes manually.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Add this script to your code in the root directory, then add the following to your circle.yml | |
# test: | |
# override: | |
# - ./circleci-parallel.sh: | |
# parallel: true | |
while getopts ":hp:" opt; do | |
case $opt in | |
h) | |
echo "" | |
echo "circleci-parallel.sh" | |
echo "-----------" | |
echo "Runs tests in parallel on Circle CI, or locally… like whatever." | |
echo "" | |
echo "Options:" | |
echo "" | |
echo " -h Prints this help message (duh)." | |
echo "" | |
echo " -p [path] Temporarily add a directory to the end of PATH. Useful" | |
echo " if the script can't find the local binaries." | |
echo "" | |
echo "Example:" | |
echo "" | |
echo " ./circleci-parallel.sh -p ~/src/my-code/vendor-modules/bin" | |
echo "" | |
echo "That is all." | |
echo "" | |
exit 0; | |
;; | |
p) | |
if [ -d "$OPTARG" ]; | |
then | |
PATH="$PATH:$OPTARG"; | |
else | |
echo "Invalid path supplied to -p option." >&2; | |
exit 1; | |
fi | |
;; | |
\?) | |
echo "Invalid option: -$OPTARG" >&2; | |
exit 2; | |
;; | |
:) | |
echo "Option -$OPTARG requires an argument." >&2; | |
exit 3; | |
;; | |
esac | |
done | |
function echo_b() { | |
echo ""; | |
echo ">>> ==============================================="; | |
echo ">>> $1"; | |
echo ">>> ==============================================="; | |
} | |
function reset_db() { | |
echo_b "Reseting database..."; | |
# Do stuff to reset the database | |
} | |
function reseed_db() { | |
echo_b "Re-seeding database..."; | |
# Do stuff to re-seed the database | |
} | |
function run_server() { | |
echo_b "Preparing environment..."; | |
reset_db && | |
reseed_db; | |
echo_b "Building distribution..."; | |
# Build the server if there is a build step | |
echo_b "Running server..."; | |
# Start the server | |
# Add a delay if the server needs some extra time to start or settle | |
for (( i = 10; i > 0; i-- )); do | |
printf "%i… " $i; | |
sleep 1; | |
done | |
printf "Let's Go!"; | |
sleep 1; | |
return 0; | |
} | |
function run_linter() { | |
echo_b "Running linter..."; | |
# Run the linter | |
} | |
function run_test_suite_one() { | |
echo_b "Running test suite one..."; | |
# Run test suite one | |
} | |
function run_test_suite_two() { | |
echo_b "Running test suite two..."; | |
# Run test suite two | |
} | |
function run_test_suite_three() { | |
echo_b "Running test suite three..."; | |
# Run test suite three | |
} | |
function run_test_suite_four() { | |
echo_b "Running test suite four..."; | |
# Run test suite four | |
} | |
# Execute based on number of Circle CI nodes, and which Circle CI node is running | |
case $CIRCLE_NODE_TOTAL in | |
"" | 1 ) | |
# This case is triggered when there is only one Circle CI node, or when run locally | |
echo_b "Will run all tests"; | |
run_linter && | |
run_server && | |
run_test_suite_one && | |
run_test_suite_two && | |
run_test_suite_three && | |
run_test_suite_four; | |
;; | |
2 ) | |
# This case is triggered when there are two Circle CI nodes | |
case $CIRCLE_NODE_INDEX in | |
0 ) | |
# These commands will run on the first Circle CI node | |
echo_b "Will run linter, test suite one, and test suite two"; | |
run_linter && | |
run_server && | |
run_test_suite_one && | |
run_test_suite_two; | |
;; | |
1 ) | |
# These commands will run on the second Circle CI node | |
echo_b "Will run test suite three and test suite four"; | |
run_server && | |
run_test_suite_three && | |
run_test_suite_four; | |
;; | |
esac | |
;; | |
3 ) | |
# This case is triggered when there are three Circle CI nodes | |
case $CIRCLE_NODE_INDEX in | |
0 ) | |
# These commands will run on the first Circle CI node | |
echo_b "Will run linter, test suite one, and test suite two"; | |
run_linter && | |
run_server && | |
run_test_suite_one && | |
run_test_suite_two; | |
;; | |
1 ) | |
# These commands will run on the second Circle CI node | |
echo_b "Will run test suite three"; | |
run_server && | |
run_test_suite_three; | |
;; | |
2 ) | |
# These commands will run on the third Circle CI node | |
echo_b "Will run test suite four"; | |
run_server && | |
run_test_suite_four; | |
;; | |
esac | |
;; | |
* ) | |
# This case is triggered when there are four or more Circle CI nodes | |
case $CIRCLE_NODE_INDEX in | |
0 ) | |
# These commands will run on the first Circle CI node | |
echo_b "Will run linter and test suite one"; | |
run_linter && | |
run_server && | |
run_test_suite_one; | |
;; | |
1 ) | |
# These commands will run on the second Circle CI node | |
echo_b "Will run test suite two"; | |
run_server && | |
run_test_suite_two; | |
;; | |
2 ) | |
# These commands will run on the third Circle CI node | |
echo_b "Will run test suite three"; | |
run_server && | |
run_test_suite_three; | |
;; | |
3 ) | |
# These commands will run on the fourth Circle CI node | |
echo_b "Will run test suite four"; | |
run_server && | |
run_test_suite_four; | |
;; | |
* ) | |
# This warning will display on the fifth or any later Circle CI node | |
echo_b "We only split tests across a maximum of 4 nodes"; | |
exit 0; | |
esac | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment