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:
+125
-78
@@ -46,59 +46,99 @@ jobs:
|
|||||||
echo "md5_9=$MD5_SUM_9" >> $GITHUB_OUTPUT
|
echo "md5_9=$MD5_SUM_9" >> $GITHUB_OUTPUT
|
||||||
echo "md5_10=$MD5_SUM_10" >> $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: |
|
run: |
|
||||||
API_URL="${{ github.api_url }}"
|
python3 -c '
|
||||||
REPO="${{ github.repository }}"
|
import os, json, urllib.request, base64, datetime
|
||||||
TOKEN="${{ github.token }}"
|
|
||||||
TAG="${{ github.ref_name }}"
|
|
||||||
|
|
||||||
IS_PRERELEASE="false"
|
|
||||||
if [[ "$TAG" =~ beta|alpha|^v0\. ]]; then
|
|
||||||
IS_PRERELEASE="true"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# 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)
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
# 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
|
|
||||||
|
|
||||||
# Upload Assets
|
api_url = os.environ["API_URL"]
|
||||||
curl -s -X POST "$API_URL/repos/$REPO/releases/$REL_ID/assets" \
|
repo = os.environ["REPO"]
|
||||||
-H "Authorization: token $TOKEN" \
|
token = os.environ["TOKEN"]
|
||||||
-F "attachment=@BlackBarRemover_${TAG}_jf10.11.zip"
|
tag = os.environ["TAG"]
|
||||||
|
md5_9 = os.environ["MD5_9"]
|
||||||
curl -s -X POST "$API_URL/repos/$REPO/releases/$REL_ID/assets" \
|
md5_10 = os.environ["MD5_10"]
|
||||||
-H "Authorization: token $TOKEN" \
|
|
||||||
-F "attachment=@BlackBarRemover_${TAG}_jf12.0.zip"
|
|
||||||
|
|
||||||
- name: Update manifest.json via API
|
headers = {
|
||||||
run: |
|
"Authorization": f"token {token}",
|
||||||
API_URL="${{ github.api_url }}"
|
"Content-Type": "application/json"
|
||||||
REPO="${{ github.repository }}"
|
}
|
||||||
TOKEN="${{ github.token }}"
|
|
||||||
|
# 1. Get or Create Release
|
||||||
VERSION="${{ github.ref_name }}"
|
rel_url = f"{api_url}/repos/{repo}/releases/tags/{tag}"
|
||||||
VERSION="${VERSION#v}"
|
req = urllib.request.Request(rel_url, headers=headers)
|
||||||
CLEAN_VERSION="${VERSION%%-*}"
|
rel_data = None
|
||||||
|
try:
|
||||||
cat > manifest.json <<EOF
|
res = urllib.request.urlopen(req)
|
||||||
[
|
rel_data = json.loads(res.read().decode("utf-8"))
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
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"))
|
||||||
|
|
||||||
|
rel_id = rel_data["id"]
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
|
# 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}")
|
||||||
|
|
||||||
|
# Upload asset
|
||||||
|
with open(filename, "rb") as f:
|
||||||
|
file_bytes = f.read()
|
||||||
|
|
||||||
|
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",
|
"guid": "12345678-1234-1234-1234-1234567890ab",
|
||||||
"name": "Black Bar Remover",
|
"name": "Black Bar Remover",
|
||||||
@@ -109,40 +149,47 @@ jobs:
|
|||||||
"imageUrl": "",
|
"imageUrl": "",
|
||||||
"versions": [
|
"versions": [
|
||||||
{
|
{
|
||||||
"version": "$CLEAN_VERSION",
|
"version": clean_ver,
|
||||||
"changelog": "Automated Release ${{ github.ref_name }} (Jellyfin 12.0)",
|
"changelog": f"Automated Release {tag} (Jellyfin 12.0)",
|
||||||
"targetAbi": "12.0.0.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",
|
"sourceUrl": f"https://gitea.labus.uk/karol/blackBarRemover/releases/download/{tag}/BlackBarRemover_{tag}_jf12.0.zip",
|
||||||
"checksum": "${{ steps.checksum.outputs.md5_10 }}",
|
"checksum": md5_10,
|
||||||
"timestamp": "$(date -u +'%Y-%m-%dT%H:%M:%SZ')"
|
"timestamp": datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"version": "$CLEAN_VERSION",
|
"version": clean_ver,
|
||||||
"changelog": "Automated Release ${{ github.ref_name }} (Jellyfin 10.11)",
|
"changelog": f"Automated Release {tag} (Jellyfin 10.11)",
|
||||||
"targetAbi": "10.11.0.0",
|
"targetAbi": "10.11.0.0",
|
||||||
"sourceUrl": "https://gitea.labus.uk/karol/blackBarRemover/releases/download/${{ github.ref_name }}/BlackBarRemover_${{ github.ref_name }}_jf10.11.zip",
|
"sourceUrl": f"https://gitea.labus.uk/karol/blackBarRemover/releases/download/{tag}/BlackBarRemover_{tag}_jf10.11.zip",
|
||||||
"checksum": "${{ steps.checksum.outputs.md5_9 }}",
|
"checksum": md5_9,
|
||||||
"timestamp": "$(date -u +'%Y-%m-%dT%H:%M:%SZ')"
|
"timestamp": datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
EOF
|
|
||||||
|
b64_manifest = base64.b64encode(json.dumps(manifest_data, indent=4).encode("utf-8")).decode("utf-8")
|
||||||
B64_CONTENT=$(base64 -w0 manifest.json)
|
|
||||||
|
|
||||||
# Update on master branch
|
# Update on master branch
|
||||||
EXISTING_MANIFEST=$(curl -s -H "Authorization: token $TOKEN" "$API_URL/repos/$REPO/contents/manifest.json?ref=master")
|
get_mf_url = f"{api_url}/repos/{repo}/contents/manifest.json?ref=master"
|
||||||
FILE_SHA=$(echo "$EXISTING_MANIFEST" | grep -o '"sha":"[^"]*"' | head -1 | cut -d'"' -f4)
|
req_mf = urllib.request.Request(get_mf_url, headers=headers)
|
||||||
|
file_sha = None
|
||||||
if [ -n "$FILE_SHA" ]; then
|
try:
|
||||||
curl -s -X PUT "$API_URL/repos/$REPO/contents/manifest.json" \
|
res_mf = urllib.request.urlopen(req_mf)
|
||||||
-H "Authorization: token $TOKEN" \
|
file_sha = json.loads(res_mf.read().decode("utf-8")).get("sha")
|
||||||
-H "Content-Type: application/json" \
|
except Exception:
|
||||||
-d "{\"content\":\"$B64_CONTENT\", \"message\":\"Update manifest.json for ${{ github.ref_name }}\", \"sha\":\"$FILE_SHA\", \"branch\":\"master\"}"
|
pass
|
||||||
else
|
|
||||||
curl -s -X PUT "$API_URL/repos/$REPO/contents/manifest.json" \
|
put_payload = {
|
||||||
-H "Authorization: token $TOKEN" \
|
"content": b64_manifest,
|
||||||
-H "Content-Type: application/json" \
|
"message": f"Update manifest.json for {tag}",
|
||||||
-d "{\"content\":\"$B64_CONTENT\", \"message\":\"Create manifest.json for ${{ github.ref_name }}\", \"branch\":\"master\"}"
|
"branch": "master"
|
||||||
fi
|
}
|
||||||
|
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