Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry asset upload if curl fails due to connection reset by peer #29

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 34 additions & 16 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ fi
release_id="$(jq '.id' < "$TMP/$method.json")"

# Make release ID available to other steps in user's workflow
echo "::set-output name=release_id::$release_id"
echo "release_id=$release_id" >>$GITHUB_OUTPUT
echo "::endgroup::"

#
Expand Down Expand Up @@ -224,21 +224,39 @@ for asset in "$assets"/*; do
fi
fi

# docs ref: https://developer.github.com/v3/repos/releases/#upload-a-release-asset
status_code="$(curl -sS -X POST \
--write-out "%{http_code}" -o "$TMP/$file_name.json" \
-H "Authorization: token $TOKEN" \
-H "Content-Length: $(stat -c %s "$asset")" \
-H "Content-Type: $(file -b --mime-type "$asset")" \
--upload-file "$asset" \
"$upload_url/$release_id/assets?name=$file_name")"

if [ "$status_code" -ne "201" ]; then
>&2 echo "::error::failed to upload asset: $file_name (see log for details)"
>&2 printf "\n\tERR: Failed asset upload: %s\n" "$file_name"
>&2 jq . < "$TMP/$file_name.json"
exit 1
fi
connreset_retry=5
while true; do
# Don't exit if curl returns a non-zero exit code
set +e

# docs ref: https://developer.github.com/v3/repos/releases/#upload-a-release-asset
status_code="$(curl -sS -X POST \
--write-out "%{http_code}" -o "$TMP/$file_name.json" \
-H "Authorization: token $TOKEN" \
-H "Content-Length: $(stat -c %s "$asset")" \
-H "Content-Type: $(file -b --mime-type "$asset")" \
--upload-file "$asset" \
"$upload_url/$release_id/assets?name=$file_name")"

exit_code=$?
set -e

if [ \( "$exit_code" -eq "55" -o "$exit_code" -eq "56" \) -a "$connreset_retry" -gt "0" ]; then
>&2 echo "failed to upload asset: $file_name (send/recv error; $((connreset_retry-=1)) retries remain)"
sleep 5
asset_id="$(gh_release_api "$release_id/assets" | jq -r "map(select(.name == \"$file_name\")) | .[] | .id")"
gh_release_api "assets/$asset_id" DELETE
continue
fi

if [ "$exit_code" -ne "0" -o "$status_code" -ne "201" ]; then
>&2 echo "::error::failed to upload asset: $file_name (see log for details)"
>&2 printf "\n\tERR: Failed asset upload: %s\n" "$file_name"
>&2 jq . < "$TMP/$file_name.json"
exit 1
fi
break
done
done

echo "::endgroup::"
Expand Down