|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Build script to run continuous integration on [Circle CI][1]. |
| 4 | +# |
| 5 | +# [1]: https://circleci.com |
| 6 | + |
| 7 | + |
| 8 | +# VARIABLES # |
| 9 | + |
| 10 | +# Define a username: |
| 11 | +username="${CIRCLE_USERNAME}" |
| 12 | + |
| 13 | +# Get the total number of VMs (parallelism): |
| 14 | +total_nodes="${CIRCLE_NODE_TOTAL}" |
| 15 | + |
| 16 | +# Get the VM index: |
| 17 | +index="${CIRCLE_NODE_INDEX}" |
| 18 | + |
| 19 | +# Define an output file to store log output: |
| 20 | +log_file='/var/log/circle-ci.log' |
| 21 | + |
| 22 | +# Define a heartbeat interval to periodically print messages in order to prevent Circle CI from prematurely ending a build due to long running commands: |
| 23 | +heartbeat_interval='30s' |
| 24 | + |
| 25 | +# Declare a variable for storing the heartbeat process id: |
| 26 | +heartbeat_pid="" |
| 27 | + |
| 28 | +# Define the number of lines of logged output to print upon completion: |
| 29 | +tail_num_lines=4000 |
| 30 | + |
| 31 | + |
| 32 | +# FUNCTIONS # |
| 33 | + |
| 34 | +# Defines an error handler. |
| 35 | +# |
| 36 | +# $1 - error status |
| 37 | +on_error() { |
| 38 | + echo 'ERROR: An error was encountered during execution.' >&2 |
| 39 | + cleanup |
| 40 | + exit "$1" |
| 41 | +} |
| 42 | + |
| 43 | +# Runs clean-up tasks. |
| 44 | +cleanup() { |
| 45 | + tail_output "${log_file}" "${tail_num_lines}" |
| 46 | + stop_heartbeat |
| 47 | +} |
| 48 | + |
| 49 | +# Starts a heartbeat. |
| 50 | +# |
| 51 | +# $1 - heartbeat interval |
| 52 | +start_heartbeat() { |
| 53 | + echo 'Starting heartbeat...' >&2 |
| 54 | + |
| 55 | + # Create a heartbeat and send to background: |
| 56 | + heartbeat "$1" & |
| 57 | + |
| 58 | + # Capture the heartbeat pid: |
| 59 | + heartbeat_pid=$! |
| 60 | + echo "Heartbeat pid: ${heartbeat_pid}" >&2 |
| 61 | +} |
| 62 | + |
| 63 | +# Runs an infinite print loop. |
| 64 | +# |
| 65 | +# $1 - heartbeat interval |
| 66 | +heartbeat() { |
| 67 | + while true; do |
| 68 | + echo "$(date) - heartbeat..." >&2; |
| 69 | + sleep "$1"; |
| 70 | + done |
| 71 | +} |
| 72 | + |
| 73 | +# Stops the heartbeat print loop. |
| 74 | +stop_heartbeat() { |
| 75 | + echo 'Stopping heartbeat...' >&2 |
| 76 | + kill "${heartbeat_pid}" |
| 77 | +} |
| 78 | + |
| 79 | +# Tails a log file. |
| 80 | +# |
| 81 | +# $1 - log file to tail |
| 82 | +# $2 - number of lines to tail |
| 83 | +tail_output() { |
| 84 | + echo "Printing the last $2 lines of log output..." >&2 |
| 85 | + tail -"$2" "$1" |
| 86 | +} |
| 87 | + |
| 88 | +# Creates an output log file. |
| 89 | +# |
| 90 | +# $1 - log file path |
| 91 | +create_log_file() { |
| 92 | + echo "Creating an output log file: $1." >&2 |
| 93 | + sudo touch "$1" |
| 94 | + sudo chown "${username}" "$1" |
| 95 | +} |
| 96 | + |
| 97 | +# Prints a success message. |
| 98 | +print_success() { |
| 99 | + echo 'Success!' >&2 |
| 100 | +} |
| 101 | + |
| 102 | +# Sets the Node.js version. |
| 103 | +# |
| 104 | +# $1 - version |
| 105 | +node_version() { |
| 106 | + echo "Switching to Node.js version: $1..." >&2 |
| 107 | + nvm use "$1" |
| 108 | +} |
| 109 | + |
| 110 | +# Remove node modules. |
| 111 | +# |
| 112 | +# $1 - log file |
| 113 | +clean_node() { |
| 114 | + echo 'Removing node module dependencies...' >&2 |
| 115 | + make clean-node >> "$1" 2>&1 |
| 116 | + if [[ "$?" -ne 0 ]]; then |
| 117 | + echo 'Error when attempting to remove dependencies.' >&2 |
| 118 | + return 1 |
| 119 | + fi |
| 120 | + echo 'Dependencies successfully removed.' >&2 |
| 121 | + return 0 |
| 122 | +} |
| 123 | + |
| 124 | +# Installs dependencies. |
| 125 | +# |
| 126 | +# $1 - log file |
| 127 | +install() { |
| 128 | + echo 'Installing...' >&2 |
| 129 | + make install >> "$1" 2>&1 |
| 130 | + if [[ "$?" -ne 0 ]]; then |
| 131 | + echo 'Error occurred during install.' >&2 |
| 132 | + return 1 |
| 133 | + fi |
| 134 | + echo 'Install successful.' >&2 |
| 135 | + return 0 |
| 136 | +} |
| 137 | + |
| 138 | +# Initializes the Node.js environment. |
| 139 | +# |
| 140 | +# $1 - Node.js version |
| 141 | +# $2 - log file |
| 142 | +init() { |
| 143 | + node_version "$1" |
| 144 | + clean_node "$2" |
| 145 | + if [[ "$?" -ne 0 ]]; then |
| 146 | + return 1 |
| 147 | + fi |
| 148 | + install "$2" |
| 149 | + if [[ "$?" -ne 0 ]]; then |
| 150 | + return 1 |
| 151 | + fi |
| 152 | + return 0 |
| 153 | +} |
| 154 | + |
| 155 | +# Checks dependencies. |
| 156 | +# |
| 157 | +# $1 - log file |
| 158 | +check_deps() { |
| 159 | + echo 'Checking dependencies...' >&2 |
| 160 | + make check-deps >> "$1" 2>&1 |
| 161 | + if [[ "$?" -ne 0 ]]; then |
| 162 | + echo 'Dependencies are out-of-date.' >&2 |
| 163 | + return 1 |
| 164 | + fi |
| 165 | + echo 'Dependencies are up-to-date.' >&2 |
| 166 | + return 0 |
| 167 | +} |
| 168 | + |
| 169 | +# Performs lint tasks. |
| 170 | +# |
| 171 | +# $1 - log file |
| 172 | +lint() { |
| 173 | + echo 'Linting filenames...' >&2 |
| 174 | + make lint-filenames >> "$1" 2>&1 |
| 175 | + if [[ "$?" -ne 0 ]]; then |
| 176 | + echo 'Linting failed.' >&2 |
| 177 | + return 1 |
| 178 | + fi |
| 179 | + echo 'Linting passed.' >&2 |
| 180 | + return 0 |
| 181 | +} |
| 182 | + |
| 183 | +# Main execution sequence. |
| 184 | +main() { |
| 185 | + create_log_file "${log_file}" |
| 186 | + start_heartbeat "${heartbeat_interval}" |
| 187 | + |
| 188 | + # Initialize the Node.js environment: |
| 189 | + init stable "${log_file}" |
| 190 | + if [[ "$?" -ne 0 ]]; then |
| 191 | + on_error 1 |
| 192 | + fi |
| 193 | + |
| 194 | + # Assign nodes (VMs) initial tasks: |
| 195 | + if [[ "${index}" = "0" ]]; then |
| 196 | + check_deps "${log_file}" |
| 197 | + if [[ "$?" -ne 0 ]]; then |
| 198 | + on_error 1 |
| 199 | + fi |
| 200 | + elif [[ "${index}" = "1" ]]; then |
| 201 | + lint "${log_file}" |
| 202 | + if [[ "$?" -ne 0 ]]; then |
| 203 | + on_error 1 |
| 204 | + fi |
| 205 | + fi |
| 206 | + cleanup |
| 207 | + print_success |
| 208 | + exit 0 |
| 209 | +} |
| 210 | + |
| 211 | +# Run main: |
| 212 | +main |
0 commit comments