Harden release publishing workflow
This commit is contained in:
1 parent
940a7514b1
commit
524a0b57dd
2 files changed
+162
-28
No files matched your search
+142
-21
@@ -100,32 +100,153 @@ jobs:
|
||||
fi
|
||||
|
||||
if [ -z "$RELEASE_ID" ]; then
|
||||
echo "::warning::Failed to create or find Gitea release ID. Proceeding anyway."
|
||||
exit 0
|
||||
echo "::error::Failed to create or find the Gitea release."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "==> Uploading assets to Gitea release ID ${RELEASE_ID}..."
|
||||
upload_asset() {
|
||||
local file="$1"
|
||||
local filename
|
||||
filename=$(basename "$file")
|
||||
echo " [start] Uploading ${filename} to Gitea..."
|
||||
RESP=$(curl -s -m 120 -w "\nHTTP_STATUS:%{http_code}" -X POST "${GITEA_HOST}/api/v1/repos/${GITEA_REPO}/releases/${RELEASE_ID}/assets?name=${filename}" \
|
||||
priority_files=(
|
||||
"dist/bin/seal-home-darwin-arm64.tar.gz"
|
||||
"dist/bin/seal-home-darwin-arm64"
|
||||
"dist/bin/seal-home-darwin-x64.tar.gz"
|
||||
"dist/bin/seal-home-darwin-x64"
|
||||
)
|
||||
remaining_files=(
|
||||
"dist/bin/seal-home-linux-arm64.tar.gz"
|
||||
"dist/bin/seal-home-linux-arm64"
|
||||
"dist/bin/seal-home-linux-x64.tar.gz"
|
||||
"dist/bin/seal-home-linux-x64"
|
||||
"dist/bin/seal-home-windows-x64.zip"
|
||||
"dist/bin/seal-home-windows-x64.exe"
|
||||
"dist/bin/checksums.txt"
|
||||
)
|
||||
expected_files=("${priority_files[@]}" "${remaining_files[@]}")
|
||||
|
||||
for file in "${expected_files[@]}"; do
|
||||
if [ ! -f "$file" ]; then
|
||||
echo "::error::Expected release asset is missing locally: ${file}"
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
|
||||
release_json() {
|
||||
curl -fsS \
|
||||
-H "Authorization: token ${GITEA_TOKEN}" \
|
||||
-F "attachment=@${file}")
|
||||
STATUS=$(echo "$RESP" | tail -n 1)
|
||||
echo " [done] ${filename} -> ${STATUS}"
|
||||
"${GITEA_HOST}/api/v1/repos/${GITEA_REPO}/releases/${RELEASE_ID}"
|
||||
}
|
||||
|
||||
pids=()
|
||||
for file in dist/bin/seal-home-* dist/bin/checksums.txt; do
|
||||
[ -f "$file" ] || continue
|
||||
upload_asset "$file" &
|
||||
pids+=($!)
|
||||
delete_asset() {
|
||||
local asset_id="$1"
|
||||
curl -fsS -X DELETE \
|
||||
-H "Authorization: token ${GITEA_TOKEN}" \
|
||||
"${GITEA_HOST}/api/v1/repos/${GITEA_REPO}/releases/assets/${asset_id}" \
|
||||
>/dev/null
|
||||
}
|
||||
|
||||
upload_asset() {
|
||||
local file="$1"
|
||||
local filename file_size local_hash assets matching_id asset_id asset_url comparison_file remote_hash response_file http_code
|
||||
filename=$(basename "$file")
|
||||
file_size=$(wc -c < "$file" | tr -d ' ')
|
||||
local_hash=$(sha256sum "$file" | awk '{print $1}')
|
||||
assets=$(release_json)
|
||||
|
||||
matching_id=""
|
||||
while IFS=$'\t' read -r asset_id asset_url; do
|
||||
[ -n "$asset_id" ] || continue
|
||||
comparison_file=$(mktemp)
|
||||
if curl -fsSL --connect-timeout 10 --max-time 900 --retry 2 \
|
||||
-H "Authorization: token ${GITEA_TOKEN}" \
|
||||
"$asset_url" -o "$comparison_file"; then
|
||||
remote_hash=$(sha256sum "$comparison_file" | awk '{print $1}')
|
||||
if [ "$remote_hash" = "$local_hash" ]; then
|
||||
matching_id="$asset_id"
|
||||
rm -f "$comparison_file"
|
||||
break
|
||||
fi
|
||||
fi
|
||||
rm -f "$comparison_file"
|
||||
done < <(echo "$assets" | jq -r --arg name "$filename" \
|
||||
'.assets[] | select(.name == $name) | [.id, .browser_download_url] | @tsv')
|
||||
|
||||
if [ -n "$matching_id" ]; then
|
||||
echo " [keep] ${filename} already exists with the expected SHA-256"
|
||||
while IFS= read -r asset_id; do
|
||||
[ -n "$asset_id" ] || continue
|
||||
if [ "$asset_id" != "$matching_id" ]; then
|
||||
echo " [dedupe] removing duplicate ${filename} asset ${asset_id}"
|
||||
delete_asset "$asset_id"
|
||||
fi
|
||||
done < <(echo "$assets" | jq -r --arg name "$filename" '.assets[] | select(.name == $name) | .id')
|
||||
return
|
||||
fi
|
||||
|
||||
while IFS= read -r asset_id; do
|
||||
[ -n "$asset_id" ] || continue
|
||||
echo " [replace] removing stale ${filename} asset ${asset_id}"
|
||||
delete_asset "$asset_id"
|
||||
done < <(echo "$assets" | jq -r --arg name "$filename" '.assets[] | select(.name == $name) | .id')
|
||||
|
||||
response_file=$(mktemp)
|
||||
echo " [start] uploading ${filename} (${file_size} bytes)"
|
||||
if ! http_code=$(curl -sS --fail-with-body \
|
||||
--connect-timeout 10 \
|
||||
--max-time 900 \
|
||||
--retry 3 \
|
||||
--retry-delay 5 \
|
||||
--retry-all-errors \
|
||||
--retry-max-time 2700 \
|
||||
-o "$response_file" \
|
||||
-w '%{http_code}' \
|
||||
-X POST "${GITEA_HOST}/api/v1/repos/${GITEA_REPO}/releases/${RELEASE_ID}/assets?name=${filename}" \
|
||||
-H "Authorization: token ${GITEA_TOKEN}" \
|
||||
-F "attachment=@${file}"); then
|
||||
echo "::error::Failed to upload ${filename} to Gitea."
|
||||
sed -n '1,20p' "$response_file"
|
||||
rm -f "$response_file"
|
||||
return 1
|
||||
fi
|
||||
rm -f "$response_file"
|
||||
|
||||
if [ "$http_code" != "201" ]; then
|
||||
echo "::error::Unexpected Gitea upload status for ${filename}: HTTP ${http_code}"
|
||||
return 1
|
||||
fi
|
||||
echo " [done] ${filename} -> HTTP ${http_code}"
|
||||
}
|
||||
|
||||
echo "==> Uploading macOS assets first (Apple Silicon archive has highest priority)..."
|
||||
for file in "${priority_files[@]}"; do
|
||||
upload_asset "$file"
|
||||
done
|
||||
|
||||
# Wait for all parallel uploads to complete
|
||||
for pid in "${pids[@]}"; do
|
||||
wait "$pid" || true
|
||||
echo "==> Uploading remaining assets with at most 2 concurrent transfers..."
|
||||
for ((index=0; index<${#remaining_files[@]}; index+=2)); do
|
||||
pids=()
|
||||
for ((offset=0; offset<2 && index+offset<${#remaining_files[@]}; offset++)); do
|
||||
upload_asset "${remaining_files[index+offset]}" &
|
||||
pids+=("$!")
|
||||
done
|
||||
for pid in "${pids[@]}"; do
|
||||
wait "$pid"
|
||||
done
|
||||
done
|
||||
echo "✓ Successfully published release assets to Gitea!"
|
||||
|
||||
echo "==> Verifying the Gitea release asset manifest..."
|
||||
final_release=$(release_json)
|
||||
verification_failed=false
|
||||
for file in "${expected_files[@]}"; do
|
||||
filename=$(basename "$file")
|
||||
file_size=$(wc -c < "$file" | tr -d ' ')
|
||||
matching_count=$(echo "$final_release" | jq -r --arg name "$filename" --argjson size "$file_size" \
|
||||
'[.assets[] | select(.name == $name and .size == $size)] | length')
|
||||
total_count=$(echo "$final_release" | jq -r --arg name "$filename" \
|
||||
'[.assets[] | select(.name == $name)] | length')
|
||||
if [ "$matching_count" != "1" ] || [ "$total_count" != "1" ]; then
|
||||
echo "::error::Gitea asset verification failed for ${filename}: matching=${matching_count}, total=${total_count}"
|
||||
verification_failed=true
|
||||
fi
|
||||
done
|
||||
if [ "$verification_failed" = "true" ]; then
|
||||
exit 1
|
||||
fi
|
||||
echo "✓ Successfully published and verified all Gitea release assets!"
|
||||
Reference in new issue
Block a user