-if [ "$GERRIT_BRANCH" == "master" ]; then
- RTD_BUILD_VERSION=latest
-else
- RTD_BUILD_VERSION="${GERRIT_BRANCH/\//-}"
+# Ensure we fail the job if any steps fail.
+# DO NOT set -u as we depend on unbound variables being passed by Jenkins.
+set -e -o pipefail
+
+# Ensure RTD_BUILD_URL retains the trailing slash as it is needed for the API
+last_char=${RTD_BUILD_URL:length-1:1}
+[[ $last_char != "/" ]] && RTD_BUILD_URL="$RTD_BUILD_URL/"; :
+
+json=$(curl -X POST -d "branches=${GERRIT_BRANCH}" -d "token=$RTD_TOKEN" "$RTD_BUILD_URL")
+build_triggered=$(echo "$json" | jq -r .build_triggered)
+
+if [ "$build_triggered" != "true" ]; then
+ echo "ERROR: Build was not triggered."
+ echo "$json" | jq -r
+ exit 1