Skip to content

Commit 726777f

Browse files
committed
fix: enhance binary download flow with robust error handling
- Define specific error codes for common failure scenarios - Improve platform and architecture error handling by using error codes - Add check to reuse cached binary if already downloaded - Enhance download process with better error handling and validation of the downloaded file - Add post-download version check with error if binary execution fails Signed-off-by: appleboy <[email protected]>
1 parent c680069 commit 726777f

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

entrypoint.sh

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ GITHUB_ACTION_PATH="${GITHUB_ACTION_PATH%/}"
88
DRONE_SSH_RELEASE_URL="${DRONE_SSH_RELEASE_URL:-https://github.com/appleboy/drone-ssh/releases/download}"
99
DRONE_SSH_VERSION="${DRONE_SSH_VERSION:-1.8.1}"
1010

11+
# Error codes
12+
readonly ERR_UNKNOWN_PLATFORM=2
13+
readonly ERR_UNKNOWN_ARCH=3
14+
readonly ERR_DOWNLOAD_FAILED=4
15+
readonly ERR_INVALID_BINARY=5
16+
readonly ERR_VERSION_CHECK_FAILED=6
17+
1118
function log_error() {
1219
echo "$1" >&2
1320
exit "$2"
@@ -19,31 +26,49 @@ function detect_client_info() {
1926

2027
case "${CLIENT_PLATFORM}" in
2128
darwin | linux | windows) ;;
22-
*) log_error "Unknown or unsupported platform: ${CLIENT_PLATFORM}. Supported platforms are Linux, Darwin, and Windows." 2 ;;
29+
*) log_error "Unknown or unsupported platform: ${CLIENT_PLATFORM}. Supported platforms are Linux, Darwin, and Windows." "${ERR_UNKNOWN_PLATFORM}" ;;
2330
esac
2431

2532
case "${CLIENT_ARCH}" in
2633
x86_64* | i?86_64* | amd64*) CLIENT_ARCH="amd64" ;;
2734
aarch64* | arm64*) CLIENT_ARCH="arm64" ;;
28-
*) log_error "Unknown or unsupported architecture: ${CLIENT_ARCH}. Supported architectures are x86_64, i686, and arm64." 3 ;;
35+
*) log_error "Unknown or unsupported architecture: ${CLIENT_ARCH}. Supported architectures are x86_64, i686, and arm64." "${ERR_UNKNOWN_ARCH}" ;;
2936
esac
3037
}
3138

3239
detect_client_info
3340
DOWNLOAD_URL_PREFIX="${DRONE_SSH_RELEASE_URL}/v${DRONE_SSH_VERSION}"
3441
CLIENT_BINARY="drone-ssh-${DRONE_SSH_VERSION}-${CLIENT_PLATFORM}-${CLIENT_ARCH}"
3542
TARGET="${GITHUB_ACTION_PATH}/${CLIENT_BINARY}"
36-
echo "Downloading ${CLIENT_BINARY} from ${DOWNLOAD_URL_PREFIX}"
37-
INSECURE_OPTION=""
38-
if [[ "${INPUT_CURL_INSECURE}" == 'true' ]]; then
39-
INSECURE_OPTION="--insecure"
40-
fi
4143

42-
curl -fsSL --retry 5 --keepalive-time 2 ${INSECURE_OPTION} "${DOWNLOAD_URL_PREFIX}/${CLIENT_BINARY}" -o "${TARGET}"
43-
chmod +x "${TARGET}"
44+
# Check if binary already exists and is executable (caching)
45+
if [[ -f "${TARGET}" ]] && [[ -x "${TARGET}" ]]; then
46+
echo "Binary ${CLIENT_BINARY} already exists, skipping download"
47+
else
48+
echo "Downloading ${CLIENT_BINARY} from ${DOWNLOAD_URL_PREFIX}"
49+
INSECURE_OPTION=""
50+
if [[ "${INPUT_CURL_INSECURE}" == 'true' ]]; then
51+
INSECURE_OPTION="--insecure"
52+
fi
53+
54+
# Download with better error handling
55+
if ! curl -fsSL --retry 5 --keepalive-time 2 --location ${INSECURE_OPTION} \
56+
"${DOWNLOAD_URL_PREFIX}/${CLIENT_BINARY}" -o "${TARGET}"; then
57+
log_error "Failed to download ${CLIENT_BINARY} from ${DOWNLOAD_URL_PREFIX}. Please check the URL and your network connection." "${ERR_DOWNLOAD_FAILED}"
58+
fi
59+
60+
# Validate downloaded file
61+
if [[ ! -f "${TARGET}" ]] || [[ ! -s "${TARGET}" ]]; then
62+
log_error "Downloaded file is missing or empty: ${TARGET}" "${ERR_INVALID_BINARY}"
63+
fi
64+
65+
chmod +x "${TARGET}"
66+
fi
4467

4568
echo "======= CLI Version Information ======="
46-
"${TARGET}" --version
69+
if ! "${TARGET}" --version; then
70+
log_error "Failed to execute ${TARGET} --version. The binary may be corrupted." "${ERR_VERSION_CHECK_FAILED}"
71+
fi
4772
echo "======================================="
4873
if [[ "${INPUT_CAPTURE_STDOUT}" == 'true' ]]; then
4974
{

0 commit comments

Comments
 (0)