fix: refine python release workflow script with dynamic timestamp
Build and Release Plugin / release (push) Failing after 51s
Build and Release Plugin / release (push) Failing after 51s
This commit is contained in:
+115
-68
@@ -46,59 +46,99 @@ jobs:
|
||||
echo "md5_9=$MD5_SUM_9" >> $GITHUB_OUTPUT
|
||||
echo "md5_10=$MD5_SUM_10" >> $GITHUB_OUTPUT
|
||||
|
||||
- name: Create Gitea Release
|
||||
- name: Publish Release and Update Manifest
|
||||
env:
|
||||
API_URL: ${{ github.api_url }}
|
||||
REPO: ${{ github.repository }}
|
||||
TOKEN: ${{ github.token }}
|
||||
TAG: ${{ github.ref_name }}
|
||||
MD5_9: ${{ steps.checksum.outputs.md5_9 }}
|
||||
MD5_10: ${{ steps.checksum.outputs.md5_10 }}
|
||||
run: |
|
||||
API_URL="${{ github.api_url }}"
|
||||
REPO="${{ github.repository }}"
|
||||
TOKEN="${{ github.token }}"
|
||||
TAG="${{ github.ref_name }}"
|
||||
python3 -c '
|
||||
import os, json, urllib.request, base64, datetime
|
||||
|
||||
IS_PRERELEASE="false"
|
||||
if [[ "$TAG" =~ beta|alpha|^v0\. ]]; then
|
||||
IS_PRERELEASE="true"
|
||||
fi
|
||||
api_url = os.environ["API_URL"]
|
||||
repo = os.environ["REPO"]
|
||||
token = os.environ["TOKEN"]
|
||||
tag = os.environ["TAG"]
|
||||
md5_9 = os.environ["MD5_9"]
|
||||
md5_10 = os.environ["MD5_10"]
|
||||
|
||||
# Check if release exists
|
||||
EXISTING_REL=$(curl -s -H "Authorization: token $TOKEN" "$API_URL/repos/$REPO/releases/tags/$TAG")
|
||||
REL_ID=$(echo "$EXISTING_REL" | grep -o '"id":[0-9]*' | head -1 | cut -d':' -f2)
|
||||
headers = {
|
||||
"Authorization": f"token {token}",
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
|
||||
if [ -z "$REL_ID" ] || [ "$REL_ID" == "null" ]; then
|
||||
RELEASE_RES=$(curl -s -X POST "$API_URL/repos/$REPO/releases" \
|
||||
-H "Authorization: token $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{\"tag_name\":\"$TAG\", \"name\":\"Release $TAG\", \"body\":\"Automated release\", \"draft\":false, \"prerelease\":$IS_PRERELEASE}")
|
||||
REL_ID=$(echo "$RELEASE_RES" | grep -o '"id":[0-9]*' | head -1 | cut -d':' -f2)
|
||||
fi
|
||||
# 1. Get or Create Release
|
||||
rel_url = f"{api_url}/repos/{repo}/releases/tags/{tag}"
|
||||
req = urllib.request.Request(rel_url, headers=headers)
|
||||
rel_data = None
|
||||
try:
|
||||
res = urllib.request.urlopen(req)
|
||||
rel_data = json.loads(res.read().decode("utf-8"))
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# Delete existing assets for this release if present
|
||||
ASSETS=$(curl -s -H "Authorization: token $TOKEN" "$API_URL/repos/$REPO/releases/$REL_ID/assets")
|
||||
echo "$ASSETS" | grep -o '"id":[0-9]*' | cut -d':' -f2 | while read -r asset_id; do
|
||||
if [ -n "$asset_id" ]; then
|
||||
curl -s -X DELETE -H "Authorization: token $TOKEN" "$API_URL/repos/$REPO/releases/$REL_ID/assets/$asset_id"
|
||||
fi
|
||||
done
|
||||
if not rel_data or "id" not in rel_data:
|
||||
is_pre = "beta" in tag or "alpha" in tag or tag.startswith("v0.")
|
||||
create_url = f"{api_url}/repos/{repo}/releases"
|
||||
body = json.dumps({
|
||||
"tag_name": tag,
|
||||
"name": f"Release {tag}",
|
||||
"body": "Automated release",
|
||||
"draft": False,
|
||||
"prerelease": is_pre
|
||||
}).encode("utf-8")
|
||||
create_req = urllib.request.Request(create_url, data=body, headers=headers)
|
||||
res = urllib.request.urlopen(create_req)
|
||||
rel_data = json.loads(res.read().decode("utf-8"))
|
||||
|
||||
# Upload Assets
|
||||
curl -s -X POST "$API_URL/repos/$REPO/releases/$REL_ID/assets" \
|
||||
-H "Authorization: token $TOKEN" \
|
||||
-F "attachment=@BlackBarRemover_${TAG}_jf10.11.zip"
|
||||
rel_id = rel_data["id"]
|
||||
|
||||
curl -s -X POST "$API_URL/repos/$REPO/releases/$REL_ID/assets" \
|
||||
-H "Authorization: token $TOKEN" \
|
||||
-F "attachment=@BlackBarRemover_${TAG}_jf12.0.zip"
|
||||
# 2. Upload Assets
|
||||
for filename in [f"BlackBarRemover_{tag}_jf10.11.zip", f"BlackBarRemover_{tag}_jf12.0.zip"]:
|
||||
if not os.path.exists(filename):
|
||||
print(f"File {filename} not found, skipping.")
|
||||
continue
|
||||
|
||||
- name: Update manifest.json via API
|
||||
run: |
|
||||
API_URL="${{ github.api_url }}"
|
||||
REPO="${{ github.repository }}"
|
||||
TOKEN="${{ github.token }}"
|
||||
# Delete existing asset if any
|
||||
for existing_asset in rel_data.get("assets", []) or []:
|
||||
if existing_asset.get("name") == filename:
|
||||
del_url = f"{api_url}/repos/{repo}/releases/{rel_id}/assets/{existing_asset['id']}"
|
||||
del_req = urllib.request.Request(del_url, headers=headers, method="DELETE")
|
||||
try:
|
||||
urllib.request.urlopen(del_req)
|
||||
print(f"Deleted old asset {existing_asset['id']}")
|
||||
except Exception as e:
|
||||
print(f"Delete asset notice: {e}")
|
||||
|
||||
VERSION="${{ github.ref_name }}"
|
||||
VERSION="${VERSION#v}"
|
||||
CLEAN_VERSION="${VERSION%%-*}"
|
||||
# Upload asset
|
||||
with open(filename, "rb") as f:
|
||||
file_bytes = f.read()
|
||||
|
||||
cat > manifest.json <<EOF
|
||||
[
|
||||
boundary = "----WebKitFormBoundary7MA4YWxkTrZu0gW"
|
||||
body_data = (
|
||||
f"--{boundary}\r\n"
|
||||
f"Content-Disposition: form-data; name=\"attachment\"; filename=\"{filename}\"\r\n"
|
||||
f"Content-Type: application/zip\r\n\r\n"
|
||||
).encode("utf-8") + file_bytes + f"\r\n--{boundary}--\r\n".encode("utf-8")
|
||||
|
||||
upload_headers = {
|
||||
"Authorization": f"token {token}",
|
||||
"Content-Type": f"multipart/form-data; boundary={boundary}"
|
||||
}
|
||||
upload_url = f"{api_url}/repos/{repo}/releases/{rel_id}/assets"
|
||||
upload_req = urllib.request.Request(upload_url, data=body_data, headers=upload_headers)
|
||||
try:
|
||||
urllib.request.urlopen(upload_req)
|
||||
print(f"Uploaded {filename} successfully.")
|
||||
except Exception as e:
|
||||
print(f"Asset upload error: {e}")
|
||||
|
||||
# 3. Update manifest.json on master branch
|
||||
clean_ver = tag.lstrip("v").split("-")[0]
|
||||
manifest_data = [
|
||||
{
|
||||
"guid": "12345678-1234-1234-1234-1234567890ab",
|
||||
"name": "Black Bar Remover",
|
||||
@@ -109,40 +149,47 @@ jobs:
|
||||
"imageUrl": "",
|
||||
"versions": [
|
||||
{
|
||||
"version": "$CLEAN_VERSION",
|
||||
"changelog": "Automated Release ${{ github.ref_name }} (Jellyfin 12.0)",
|
||||
"version": clean_ver,
|
||||
"changelog": f"Automated Release {tag} (Jellyfin 12.0)",
|
||||
"targetAbi": "12.0.0.0",
|
||||
"sourceUrl": "https://gitea.labus.uk/karol/blackBarRemover/releases/download/${{ github.ref_name }}/BlackBarRemover_${{ github.ref_name }}_jf12.0.zip",
|
||||
"checksum": "${{ steps.checksum.outputs.md5_10 }}",
|
||||
"timestamp": "$(date -u +'%Y-%m-%dT%H:%M:%SZ')"
|
||||
"sourceUrl": f"https://gitea.labus.uk/karol/blackBarRemover/releases/download/{tag}/BlackBarRemover_{tag}_jf12.0.zip",
|
||||
"checksum": md5_10,
|
||||
"timestamp": datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')
|
||||
},
|
||||
{
|
||||
"version": "$CLEAN_VERSION",
|
||||
"changelog": "Automated Release ${{ github.ref_name }} (Jellyfin 10.11)",
|
||||
"version": clean_ver,
|
||||
"changelog": f"Automated Release {tag} (Jellyfin 10.11)",
|
||||
"targetAbi": "10.11.0.0",
|
||||
"sourceUrl": "https://gitea.labus.uk/karol/blackBarRemover/releases/download/${{ github.ref_name }}/BlackBarRemover_${{ github.ref_name }}_jf10.11.zip",
|
||||
"checksum": "${{ steps.checksum.outputs.md5_9 }}",
|
||||
"timestamp": "$(date -u +'%Y-%m-%dT%H:%M:%SZ')"
|
||||
"sourceUrl": f"https://gitea.labus.uk/karol/blackBarRemover/releases/download/{tag}/BlackBarRemover_{tag}_jf10.11.zip",
|
||||
"checksum": md5_9,
|
||||
"timestamp": datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
EOF
|
||||
|
||||
B64_CONTENT=$(base64 -w0 manifest.json)
|
||||
b64_manifest = base64.b64encode(json.dumps(manifest_data, indent=4).encode("utf-8")).decode("utf-8")
|
||||
|
||||
# Update on master branch
|
||||
EXISTING_MANIFEST=$(curl -s -H "Authorization: token $TOKEN" "$API_URL/repos/$REPO/contents/manifest.json?ref=master")
|
||||
FILE_SHA=$(echo "$EXISTING_MANIFEST" | grep -o '"sha":"[^"]*"' | head -1 | cut -d'"' -f4)
|
||||
get_mf_url = f"{api_url}/repos/{repo}/contents/manifest.json?ref=master"
|
||||
req_mf = urllib.request.Request(get_mf_url, headers=headers)
|
||||
file_sha = None
|
||||
try:
|
||||
res_mf = urllib.request.urlopen(req_mf)
|
||||
file_sha = json.loads(res_mf.read().decode("utf-8")).get("sha")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if [ -n "$FILE_SHA" ]; then
|
||||
curl -s -X PUT "$API_URL/repos/$REPO/contents/manifest.json" \
|
||||
-H "Authorization: token $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{\"content\":\"$B64_CONTENT\", \"message\":\"Update manifest.json for ${{ github.ref_name }}\", \"sha\":\"$FILE_SHA\", \"branch\":\"master\"}"
|
||||
else
|
||||
curl -s -X PUT "$API_URL/repos/$REPO/contents/manifest.json" \
|
||||
-H "Authorization: token $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{\"content\":\"$B64_CONTENT\", \"message\":\"Create manifest.json for ${{ github.ref_name }}\", \"branch\":\"master\"}"
|
||||
fi
|
||||
put_payload = {
|
||||
"content": b64_manifest,
|
||||
"message": f"Update manifest.json for {tag}",
|
||||
"branch": "master"
|
||||
}
|
||||
if file_sha:
|
||||
put_payload["sha"] = file_sha
|
||||
|
||||
put_url = f"{api_url}/repos/{repo}/contents/manifest.json"
|
||||
put_req = urllib.request.Request(put_url, data=json.dumps(put_payload).encode("utf-8"), headers=headers, method="PUT")
|
||||
urllib.request.urlopen(put_req)
|
||||
print("manifest.json updated successfully via API.")
|
||||
'
|
||||
|
||||
Reference in New Issue
Block a user