Skip to content

Commit b8756a4

Browse files
committed
Make pre command hook more error prone
1 parent 6bd5efd commit b8756a4

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

.buildkite/hooks/pre-command

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -147,17 +147,28 @@ fi
147147

148148
if [[ "${SMART_RETRIES:-}" == "true" && "${BUILDKITE_RETRY_COUNT:-0}" -gt 0 ]]; then
149149
echo "--- Resolving previously failed tests"
150-
BUILD_JSON=$(curl -H "Authorization: Bearer $BUILDKITE_API_TOKEN" -X GET "https://api.buildkite.com/v2/organizations/elastic/pipelines/${BUILDKITE_PIPELINE_SLUG}/builds/${BUILDKITE_BUILD_NUMBER}")
151-
ORIGIN_JOB_ID=$(printf '%s\n' "$BUILD_JSON" | jq -r --arg jobId "$BUILDKITE_JOB_ID" ' .jobs[] | select(.id == $jobId) | .retry_source.job_id')
152-
BUILD_SCAN_URL=$(printf '%s\n' "$BUILD_JSON" | jq -r --arg job_id "$ORIGIN_JOB_ID" '.meta_data["build-scan-" + $job_id]')
153-
BUILD_SCAN_ID=$(echo "$BUILD_SCAN_URL" | sed 's|.*/s/||')
154-
DEVELOCITY_FAILED_TEST_API_URL="https://gradle-enterprise.elastic.co/api/tests/build/${BUILD_SCAN_ID}?testOutcomes=failed"
155-
156-
curl --request GET \
157-
--url $DEVELOCITY_FAILED_TEST_API_URL \
158-
--header 'accept: application/json' \
159-
--header "authorization: Bearer $DEVELOCITY_API_ACCESS_KEY" \
160-
--header 'content-type: application/json' | gunzip | jq '.' &> failed-tests.json
150+
if BUILD_JSON=$(curl -H "Authorization: Bearer $BUILDKITE_API_TOKEN" -X GET "https://api.buildkite.com/v2/organizations/elastic/pipelines/${BUILDKITE_PIPELINE_SLUG}/builds/${BUILDKITE_BUILD_NUMBER}" 2>/dev/null); then
151+
if ORIGIN_JOB_ID=$(printf '%s\n' "$BUILD_JSON" | jq -r --arg jobId "$BUILDKITE_JOB_ID" ' .jobs[] | select(.id == $jobId) | .retry_source.job_id' 2>/dev/null) && [ "$ORIGIN_JOB_ID" != "null" ] && [ -n "$ORIGIN_JOB_ID" ]; then
152+
if BUILD_SCAN_URL=$(printf '%s\n' "$BUILD_JSON" | jq -r --arg job_id "$ORIGIN_JOB_ID" '.meta_data["build-scan-" + $job_id]' 2>/dev/null) && [ "$BUILD_SCAN_URL" != "null" ] && [ -n "$BUILD_SCAN_URL" ]; then
153+
BUILD_SCAN_ID=$(echo "$BUILD_SCAN_URL" | sed 's|.*/s/||')
154+
DEVELOCITY_FAILED_TEST_API_URL="https://gradle-enterprise.elastic.co/api/tests/build/${BUILD_SCAN_ID}?testOutcomes=failed"
155+
156+
if ! curl --request GET \
157+
--url $DEVELOCITY_FAILED_TEST_API_URL \
158+
--header 'accept: application/json' \
159+
--header "authorization: Bearer $DEVELOCITY_API_ACCESS_KEY" \
160+
--header 'content-type: application/json' 2>/dev/null | gunzip | jq '.' &> failed-tests.json; then
161+
echo "Warning: Failed to fetch failed tests from Develocity API"
162+
fi
163+
else
164+
echo "Warning: Could not extract build scan URL from build metadata"
165+
fi
166+
else
167+
echo "Warning: Could not find origin job ID for retry"
168+
fi
169+
else
170+
echo "Warning: Failed to fetch build information from Buildkite API"
171+
fi
161172
fi
162173

163174
# Amazon Linux 2 has DNS resolution issues with resource-based hostnames in EC2

0 commit comments

Comments
 (0)