-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentrypoint.sh
More file actions
executable file
·144 lines (111 loc) · 5.48 KB
/
Copy pathentrypoint.sh
File metadata and controls
executable file
·144 lines (111 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env bash
set -euo pipefail
# ── Helpers ──────────────────────────────────────────────────────────────────
info() { echo "[autodevice] $*"; }
fail() { echo "::error::$*"; exit 1; }
# ── Mask the API key ────────────────────────────────────────────────────────
if [[ -n "${INPUT_API_KEY:-}" ]]; then
echo "::add-mask::${INPUT_API_KEY}"
fi
# ── Validate inputs ─────────────────────────────────────────────────────────
[[ -n "${INPUT_API_KEY:-}" ]] || fail "api-key is required"
[[ -n "${INPUT_PACKAGE_NAME:-}" ]] || fail "package-name is required"
[[ -n "${INPUT_BUILD_PATH:-}" ]] || fail "build-path is required"
[[ -f "${INPUT_BUILD_PATH}" ]] || fail "File not found: ${INPUT_BUILD_PATH}"
[[ -r "${INPUT_BUILD_PATH}" ]] || fail "File not readable: ${INPUT_BUILD_PATH}"
API_URL="${INPUT_API_URL:-https://app.autodevice.io}"
# ── Install jq if missing ──────────────────────────────────────────────────
if command -v jq &>/dev/null; then
info "jq already installed: $(jq --version)"
else
info "Installing jq…"
OS_NAME="$(uname -s)"
if [ "$OS_NAME" = "Darwin" ]; then
if command -v brew &>/dev/null; then
brew install --quiet jq
else
fail "Homebrew not found on macOS runner; cannot install jq. Please preinstall jq."
fi
elif command -v apt-get &>/dev/null; then
sudo apt-get update -qq && sudo apt-get install -qq -y jq
elif command -v apk &>/dev/null; then
sudo apk add --no-cache jq || apk add --no-cache jq
elif command -v yum &>/dev/null; then
sudo yum install -q -y jq
elif command -v dnf &>/dev/null; then
sudo dnf install -q -y jq
else
fail "Cannot install jq: no supported package manager found. Please preinstall jq."
fi
command -v jq &>/dev/null || fail "Failed to install jq"
info "jq installed: $(jq --version)"
fi
# ── Git metadata ────────────────────────────────────────────────────────────
# For pull_request events, GITHUB_SHA is a merge commit — extract the actual PR head SHA
PR_HEAD_SHA="$(jq -r '.pull_request.head.sha // empty' "$GITHUB_EVENT_PATH" 2>/dev/null || true)"
COMMIT_SHA="${PR_HEAD_SHA:-${GITHUB_SHA:-}}"
BRANCH="${GITHUB_HEAD_REF:-${GITHUB_REF_NAME:-}}"
REPO="${GITHUB_REPOSITORY:-}"
# ── File metadata ───────────────────────────────────────────────────────────
FILE_NAME="$(basename "${INPUT_BUILD_PATH}")"
# Cross-platform file size (GNU stat vs BSD stat)
if stat --version &>/dev/null 2>&1; then
FILE_SIZE="$(stat -c%s "${INPUT_BUILD_PATH}")"
else
FILE_SIZE="$(stat -f%z "${INPUT_BUILD_PATH}")"
fi
info "File: ${FILE_NAME} (${FILE_SIZE} bytes)"
# ── Step 1 – Start upload ──────────────────────────────────────────────────
echo "::group::Step 1 – Get presigned upload URL"
START_PAYLOAD="$(jq -n --arg fn "${FILE_NAME}" '{filename: $fn}')"
START_RESPONSE="$(
curl --fail --silent --show-error \
-X POST \
-H "Authorization: Bearer ${INPUT_API_KEY}" \
-H "Content-Type: application/json" \
-d "${START_PAYLOAD}" \
"${API_URL}/api/v1/apps/start-upload"
)"
UPLOAD_URL="$(echo "${START_RESPONSE}" | jq -r '.upload_url')"
FILE_PATH="$(echo "${START_RESPONSE}" | jq -r '.file_path')"
[[ -n "${UPLOAD_URL}" && "${UPLOAD_URL}" != "null" ]] || fail "Missing upload_url in start-upload response"
[[ -n "${FILE_PATH}" && "${FILE_PATH}" != "null" ]] || fail "Missing file_path in start-upload response"
info "Presigned URL obtained"
echo "::endgroup::"
# ── Step 2 – Upload binary ─────────────────────────────────────────────────
echo "::group::Step 2 – Upload binary"
curl --fail --silent --show-error \
-X PUT \
-H "Content-Type: application/octet-stream" \
--data-binary "@${INPUT_BUILD_PATH}" \
"${UPLOAD_URL}"
info "Upload complete"
echo "::endgroup::"
# ── Step 3 – Confirm upload ────────────────────────────────────────────────
echo "::group::Step 3 – Confirm upload"
CONFIRM_PAYLOAD="$(
jq -n \
--arg fp "${FILE_PATH}" \
--arg fs "${FILE_SIZE}" \
--arg pn "${INPUT_PACKAGE_NAME}" \
--arg sha "${COMMIT_SHA}" \
--arg br "${BRANCH}" \
--arg repo "${REPO}" \
'{
file_path: $fp,
file_size: ($fs | tonumber),
package_name: $pn,
commit_sha: $sha,
branch: $br,
repository: $repo
}'
)"
curl --fail --silent --show-error \
-X POST \
-H "Authorization: Bearer ${INPUT_API_KEY}" \
-H "Content-Type: application/json" \
-d "${CONFIRM_PAYLOAD}" \
"${API_URL}/api/v1/apps/confirm-upload"
info "Upload confirmed"
echo "::endgroup::"
info "Done! Build uploaded to autodevice.io"