Created
July 3, 2018 04:46
-
-
Save markcerqueira/ff08867b3ad75e0c61d06a6ee28ef641 to your computer and use it in GitHub Desktop.
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
# Helper script that all Jenkins scripts can source in to get step/next/try error checking | |
# Source: https://stackoverflow.com/a/5196220 | |
# Use step(), try(), and next() to perform a series of commands and print | |
# [ OK ] or [FAILED] at the end. The step as a whole fails if any individual | |
# command fails. | |
# | |
# Example: | |
# step "Remounting / and /boot as read-write:" | |
# try mount -o remount,rw / | |
# try mount -o remount,rw /boot | |
# next | |
step() { | |
echo "$@" | |
STEP_OK=0 | |
[[ -w /tmp ]] && echo $STEP_OK > /tmp/step.$$ | |
} | |
try() { | |
# Check for `-b' argument to run command in the background. | |
local BG= | |
[[ $1 == -b ]] && { BG=1; shift; } | |
[[ $1 == -- ]] && { shift; } | |
# Run the command. | |
if [[ -z $BG ]]; then | |
"$@" | |
else | |
"$@" & | |
fi | |
# Check if command failed and update $STEP_OK if so. | |
local EXIT_CODE=$? | |
if [[ $EXIT_CODE -ne 0 ]]; then | |
STEP_OK=$EXIT_CODE | |
[[ -w /tmp ]] && echo $STEP_OK > /tmp/step.$$ | |
if [[ -n $LOG_STEPS ]]; then | |
local FILE=$(readlink -m "${BASH_SOURCE[1]}") | |
local LINE=${BASH_LINENO[0]} | |
echo "$FILE: line $LINE: Command \`$*' failed with exit code $EXIT_CODE." >> "$LOG_STEPS" | |
fi | |
fi | |
return $EXIT_CODE | |
} | |
next() { | |
[[ -f /tmp/step.$$ ]] && { STEP_OK=$(< /tmp/step.$$); rm -f /tmp/step.$$; } | |
[[ $STEP_OK -eq 0 ]] && echo_success || echo_failure | |
echo | |
return $STEP_OK | |
} | |
# Add echo_success and echo_failure commands from init.d | |
# Adapted from https://bash.cyberciti.biz/guide//etc/init.d/functions | |
echo_success() { | |
return 0 | |
} | |
echo_failure() { | |
return 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment