Skip to content

Commit 95d9fd6

Browse files
jerjoulesv
authored andcommitted
Sub-processes + error fixes. (#550)
* 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.
1 parent 5a6a25f commit 95d9fd6

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

jenkins.sh

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,24 @@
1616

1717
set -xe
1818
shopt -s globstar
19+
# We spin up some subprocesses. Don't kill them on hangup
20+
trap '' HUP
1921

2022
app_version=""
2123

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

3232
# Clean up
33-
delete_app_version # shellcheck disable=SC2119
33+
delete_app_version "$@"
3434

3535
exit ${errcode}
3636
}
37-
trap handle_error ERR
3837

3938
# First, style-check the shell scripts
4039
shellcheck ./**/*.sh
@@ -45,16 +44,21 @@ find . -mindepth 2 -maxdepth 5 -name jenkins.sh -type f | while read -r path; do
4544
# Use just the first letter of each subdir in version name
4645
# shellcheck disable=SC2001
4746
app_version="jenkins-$(echo "${dir#./}" | sed 's#\([a-z]\)[^/]*/#\1-#g')"
47+
48+
trap 'handle_error $app_version' ERR
4849
(
50+
# If there's an error, clean up
51+
4952
pushd "${dir}"
5053
# Need different app versions because flex can't deploy over an existing
5154
# version
5255
GOOGLE_VERSION_ID="${app_version}" /bin/bash ./jenkins.sh
53-
echo "Return code: $?"
5456

5557
# Clean up the app version in the background
56-
nohup delete_app_version "${app_version}" &
58+
delete_app_version "${app_version}" &
5759
)
60+
# Clear the trap
61+
trap - ERR
5862
done
5963

6064
wait

0 commit comments

Comments
 (0)