Skip to content

Commit

Permalink
Merge pull request #561 from GoogleCloudPlatform/shellcheck
Browse files Browse the repository at this point in the history
Save the output of commands that error.
  • Loading branch information
jerjou authored Mar 14, 2017
2 parents 9cebf6c + 8c46fb0 commit efff667
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
4 changes: 4 additions & 0 deletions appengine/endpoints-frameworks-v2/discovery/jenkins.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,27 @@ function TestEndpoints () {
# Test getGreeting Endpoint (hello world!)
curl -X GET \
"https://${2}-dot-${1}.appspot.com/_ah/api/helloworld/v1/hellogreeting/0" | \
tee "$ERROR_OUTPUT_DIR/response.json" | \
grep "hello version-${2}"

# Test getGreeting Endpoint (goodbye world!)
curl -X GET \
"https://${2}-dot-${1}.appspot.com/_ah/api/helloworld/v1/hellogreeting/1" | \
tee "$ERROR_OUTPUT_DIR/response.json" | \
grep "goodbye world!"

# Test listGreeting Endpoint (hello world! and goodbye world!)
curl -X GET \
"https://${2}-dot-${1}.appspot.com/_ah/api/helloworld/v1/hellogreeting" | \
tee "$ERROR_OUTPUT_DIR/response.json" | \
grep "hello world!\|goodbye world!"

# Test multiply Endpoint (This is a greeting.)
curl -X POST \
-H "Content-Type: application/json" \
--data "{'message':'This is a greeting from instance ${2}'}." \
"https://${2}-dot-${1}.appspot.com/_ah/api/helloworld/v1/hellogreeting/1" | \
tee "$ERROR_OUTPUT_DIR/response.json" | \
grep "This is a greeting from instance ${2}."
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class Greetings {
public static ArrayList<HelloGreeting> greetings = new ArrayList<HelloGreeting>();

static {
greetings.add(new HelloGreeting("hello version-jerjou-test!"));
greetings.add(new HelloGreeting("hello world!"));
greetings.add(new HelloGreeting("goodbye world!"));
}
//[END api_def]
Expand Down
1 change: 1 addition & 0 deletions flexible/sparkjava/jenkins.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ set -xe
function runtests () {
curl -X GET \
"https://${2}-dot-${1}.appspot.com/api/users" | \
tee "$ERROR_OUTPUT_DIR/response.json" | \
grep "^\\["
}

Expand Down
39 changes: 27 additions & 12 deletions jenkins.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,58 @@ shopt -s globstar
# We spin up some subprocesses. Don't kill them on hangup
trap '' HUP

app_version=""
# Temporary directory to store any output to display on error
export ERROR_OUTPUT_DIR
ERROR_OUTPUT_DIR="$(mktemp -d)"
trap 'rm -r "${ERROR_OUTPUT_DIR}"' EXIT

# shellcheck disable=SC2120
delete_app_version() {
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 "$@"
# Clean up remote app version
delete_app_version "${1}" &

# Display any errors
if [ -n "$(find "${2}" -mindepth 1 -print -quit)" ]; then
cat "${2:?}"/* 1>&2
fi

wait

exit ${errcode}
}

cleanup() {
delete_app_version "${GOOGLE_VERSION_ID}" &
rm -r "${ERROR_OUTPUT_DIR:?}/"*
}

# First, style-check the shell scripts
shellcheck ./**/*.sh

# Find all jenkins.sh's and run them.
find . -mindepth 2 -maxdepth 5 -name jenkins.sh -type f | while read -r path; do
dir="${path%/jenkins.sh}"
# Use just the first letter of each subdir in version name
# Need different app versions because flex can't deploy over an existing
# version. Use just the first letter of each subdir in version name
export GOOGLE_VERSION_ID
# shellcheck disable=SC2001
app_version="jenkins-$(echo "${dir#./}" | sed 's#\([a-z]\)[^/]*/#\1-#g')"
GOOGLE_VERSION_ID="jenkins-$(echo "${dir#./}" | sed 's#\([a-z]\)[^/]*/#\1-#g')"

trap 'handle_error $app_version' ERR
trap 'handle_error "${GOOGLE_VERSION_ID}" "${ERROR_OUTPUT_DIR}"' 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
/bin/bash ./jenkins.sh

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

0 comments on commit efff667

Please sign in to comment.