Skip to content

Commit

Permalink
Sub-processes + error fixes. (#550)
Browse files Browse the repository at this point in the history
* nohup doesn't work with bash functions - trap HUP instead
* The env in a `trap` handler isn't this one, so can't access global
  variables. Pass it in, in sort of a try..catch pattern.
  • Loading branch information
jerjou authored and lesv committed Mar 7, 2017
1 parent 5a6a25f commit 95d9fd6
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions jenkins.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,24 @@

set -xe
shopt -s globstar
# We spin up some subprocesses. Don't kill them on hangup
trap '' HUP

app_version=""

# shellcheck disable=SC2120
delete_app_version() {
if [ -n "${app_version}" ] || [ $# -gt 0 ]; then
yes | gcloud --project="${GOOGLE_PROJECT_ID}" \
app versions delete "${1-${app_version}}"
fi
yes | gcloud --project="${GOOGLE_PROJECT_ID}" \
app versions delete "${1}"
}
handle_error() {
errcode=$? # Remember the error code so we can exit with it after cleanup

# Clean up
delete_app_version # shellcheck disable=SC2119
delete_app_version "$@"

exit ${errcode}
}
trap handle_error ERR

# First, style-check the shell scripts
shellcheck ./**/*.sh
Expand All @@ -45,16 +44,21 @@ find . -mindepth 2 -maxdepth 5 -name jenkins.sh -type f | while read -r path; do
# Use just the first letter of each subdir in version name
# shellcheck disable=SC2001
app_version="jenkins-$(echo "${dir#./}" | sed 's#\([a-z]\)[^/]*/#\1-#g')"

trap 'handle_error $app_version' ERR
(
# If there's an error, clean up

pushd "${dir}"
# Need different app versions because flex can't deploy over an existing
# version
GOOGLE_VERSION_ID="${app_version}" /bin/bash ./jenkins.sh
echo "Return code: $?"

# Clean up the app version in the background
nohup delete_app_version "${app_version}" &
delete_app_version "${app_version}" &
)
# Clear the trap
trap - ERR
done

wait

0 comments on commit 95d9fd6

Please sign in to comment.