Skip to content

Commit cab66a0

Browse files
committed
Adding POST method, adding retry to create build post
1 parent fe16b35 commit cab66a0

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

entrypoint.sh

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ function curl_with_retry() {
122122
local AUTH_TOKEN="$2"
123123
local MAX_ATTEMPTS="${3:-5}"
124124
local BASE_DELAY="${4:-2}"
125+
local METHOD="${5:-GET}"
126+
local DATA="${6:-}"
125127
local ATTEMPT=1
126128
local HTTP_CODE
127129
local RESPONSE
@@ -131,13 +133,25 @@ function curl_with_retry() {
131133

132134
while [ "$ATTEMPT" -le "$MAX_ATTEMPTS" ]; do
133135

134-
HTTP_CODE=$(curl \
135-
--silent \
136-
--show-error \
137-
--write-out '%{http_code}' \
138-
--output "$TEMP_FILE" \
139-
-H "Authorization: Bearer ${AUTH_TOKEN}" \
140-
"$URL" 2>&1 | tail -n1)
136+
if [ "$METHOD" = "POST" ] && [ -n "$DATA" ]; then
137+
HTTP_CODE=$(curl \
138+
--silent \
139+
--show-error \
140+
--write-out '%{http_code}' \
141+
--output "$TEMP_FILE" \
142+
-X POST \
143+
-H "Authorization: Bearer ${AUTH_TOKEN}" \
144+
-d "$DATA" \
145+
"$URL" 2>&1 | tail -n1)
146+
else
147+
HTTP_CODE=$(curl \
148+
--silent \
149+
--show-error \
150+
--write-out '%{http_code}' \
151+
--output "$TEMP_FILE" \
152+
-H "Authorization: Bearer ${AUTH_TOKEN}" \
153+
"$URL" 2>&1 | tail -n1)
154+
fi
141155

142156
RESPONSE=$(cat "$TEMP_FILE")
143157

@@ -369,26 +383,21 @@ else
369383
FINAL_JSON=$JSON
370384
fi
371385

372-
CODE=0
373-
RESPONSE=$(
374-
curl \
375-
--fail-with-body \
376-
--silent \
377-
--show-error \
378-
-X POST \
379-
-H "Authorization: Bearer ${INPUT_BUILDKITE_API_ACCESS_TOKEN}" \
380-
"https://api.buildkite.com/v2/organizations/${ORG_SLUG}/pipelines/${PIPELINE_SLUG}/builds" \
381-
-d "$FINAL_JSON" | tr -d '\n'
382-
) || CODE=$?
383-
384-
if [ $CODE -ne 0 ]; then
385-
MESSAGE=$(echo "$RESPONSE" | jq .message 2>/dev/null || true)
386-
if [[ -n "$MESSAGE" ]] && [[ "$MESSAGE" != 'null' ]]; then
387-
echo -n "Buildkite API call failed: $MESSAGE"
388-
fi
389-
exit $CODE
386+
RESPONSE=$(curl_with_retry \
387+
"https://api.buildkite.com/v2/organizations/${ORG_SLUG}/pipelines/${PIPELINE_SLUG}/builds" \
388+
"${INPUT_BUILDKITE_API_ACCESS_TOKEN}" \
389+
"${INPUT_RETRY_MAX_ATTEMPTS:-5}" \
390+
"${INPUT_RETRY_BASE_DELAY:-2}" \
391+
"POST" \
392+
"$FINAL_JSON")
393+
394+
if [ $? -ne 0 ]; then
395+
echo "Failed to create build after retries"
396+
exit 1
390397
fi
391398

399+
RESPONSE=$(echo "$RESPONSE" | tr -d '\n')
400+
392401
echo ""
393402
echo "Build created:"
394403
URL=$(echo "$RESPONSE" | jq --raw-output ".web_url")

0 commit comments

Comments
 (0)