Compare commits
4
Commits
6f23f14b76
..
v1.0.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7972834e90 | ||
|
|
c89aa3d1c7 | ||
|
|
d18db76441 | ||
|
|
2462bdf097 |
No files matched your search
+100
-97
@@ -5,140 +5,143 @@ on:
|
|||||||
tags:
|
tags:
|
||||||
- 'v*'
|
- 'v*'
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
inputs:
|
|
||||||
version:
|
|
||||||
description: 'Release version (e.g. 1.0.0)'
|
|
||||||
required: false
|
|
||||||
default: ''
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
test-and-release:
|
release:
|
||||||
|
name: Build Packages and Publish
|
||||||
runs-on: fedora
|
runs-on: fedora
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout Source Code
|
- name: Checkout Source Code
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
with:
|
|
||||||
fetch-depth: 0
|
|
||||||
|
|
||||||
- name: Setup Build Dependencies
|
- name: Verify Build Environment
|
||||||
run: |
|
run: |
|
||||||
echo "[INFO] Verifying build dependencies..."
|
echo "=== Runner System Info ==="
|
||||||
|
uname -a
|
||||||
python3 --version
|
python3 --version
|
||||||
if ! command -v rpmbuild >/dev/null 2>&1; then
|
if ! command -v rpmbuild &>/dev/null; then
|
||||||
echo "[INFO] Installing rpm-build..."
|
echo "Installing rpm-build..."
|
||||||
sudo dnf install -y rpm-build
|
sudo dnf install -y rpm-build
|
||||||
|
else
|
||||||
|
echo "rpmbuild is already available: $(which rpmbuild)"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
- name: Run Quality Gate Test Suite
|
- name: Run Test Suite
|
||||||
run: |
|
run: |
|
||||||
echo "[INFO] Executing comprehensive test suite..."
|
echo "=== Running Unit Tests ==="
|
||||||
python3 -m unittest discover tests -v
|
python3 -m unittest discover tests
|
||||||
|
|
||||||
- name: Build Distribution Packages
|
- name: Build Distribution Packages
|
||||||
id: build_pkgs
|
|
||||||
run: |
|
run: |
|
||||||
echo "[INFO] Building .tar.gz, .deb, and .rpm distribution packages..."
|
echo "=== Building Packages (.rpm, .deb, .tar.gz) ==="
|
||||||
rm -rf dist
|
python3 packaging/build_packages.py --all
|
||||||
python3 packaging/build_packages.py --all --out-dir dist
|
echo "=== Built Artifacts ==="
|
||||||
|
ls -lh dist/*.tar.gz dist/*.deb 2>/dev/null || true
|
||||||
TAR_FILE=$(find dist -maxdepth 1 -name "*.tar.gz" | head -n 1)
|
ls -lh dist/*.rpm dist/rpmbuild/RPMS/*/*.rpm 2>/dev/null || true
|
||||||
DEB_FILE=$(find dist -maxdepth 1 -name "*.deb" | head -n 1)
|
|
||||||
RPM_FILE=$(find dist -maxdepth 1 -name "*.rpm" | head -n 1)
|
|
||||||
|
|
||||||
echo "TAR_FILE=$TAR_FILE" >> $GITHUB_ENV
|
|
||||||
echo "DEB_FILE=$DEB_FILE" >> $GITHUB_ENV
|
|
||||||
echo "RPM_FILE=$RPM_FILE" >> $GITHUB_ENV
|
|
||||||
|
|
||||||
echo "[INFO] Generated packages:"
|
|
||||||
ls -lh dist/
|
|
||||||
|
|
||||||
- name: Publish to Gitea Package Registry
|
- name: Publish to Gitea Package Registry
|
||||||
env:
|
env:
|
||||||
PKG_TOKEN: ${{ secrets.PACKAGES_GENERATION || secrets.GITHUB_TOKEN }}
|
PKG_TOKEN: ${{ secrets.PACKAGES_GENERATION || secrets.GITHUB_TOKEN }}
|
||||||
run: |
|
run: |
|
||||||
echo "[INFO] Uploading packages to Gitea Package Registry..."
|
echo "=== Publishing to Gitea Package Registry ==="
|
||||||
OWNER="${{ github.repository_owner }}"
|
RPM_FILE=$(ls dist/*.rpm 2>/dev/null | head -n 1)
|
||||||
REPO="${{ github.event.repository.name }}"
|
DEB_FILE=$(ls dist/*.deb 2>/dev/null | head -n 1)
|
||||||
SERVER="${{ github.server_url }}"
|
|
||||||
|
|
||||||
# Upload RPM
|
if [ -n "$RPM_FILE" ] && [ -f "$RPM_FILE" ]; then
|
||||||
if [[ -f "$RPM_FILE" ]]; then
|
echo "Uploading RPM: $RPM_FILE"
|
||||||
echo "[INFO] Publishing RPM to registry..."
|
HTTP_CODE=$(curl -k -s -o /dev/null -w "%{http_code}" -X PUT \
|
||||||
curl -k -s -f -X PUT -u "${{ github.actor }}:${PKG_TOKEN}" \
|
-u "${{ github.actor }}:${PKG_TOKEN}" \
|
||||||
--upload-file "$RPM_FILE" \
|
--upload-file "$RPM_FILE" \
|
||||||
"${SERVER}/api/packages/${OWNER}/rpm/upload" || echo "[WARNING] RPM upload returned non-zero (may already exist)"
|
"${{ github.server_url }}/api/packages/${{ github.repository_owner }}/rpm/upload")
|
||||||
|
echo "RPM Registry HTTP status: $HTTP_CODE"
|
||||||
# Link package to repository
|
|
||||||
curl -k -s -X POST -H "Authorization: token ${PKG_TOKEN}" \
|
|
||||||
"${SERVER}/api/v1/packages/${OWNER}/rpm/linux-backuper/-/link/${REPO}" || true
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Upload Debian
|
if [ -n "$DEB_FILE" ] && [ -f "$DEB_FILE" ]; then
|
||||||
if [[ -f "$DEB_FILE" ]]; then
|
echo "Uploading DEB: $DEB_FILE"
|
||||||
echo "[INFO] Publishing Debian to registry..."
|
HTTP_CODE=$(curl -k -s -o /dev/null -w "%{http_code}" -X PUT \
|
||||||
curl -k -s -f -X PUT -u "${{ github.actor }}:${PKG_TOKEN}" \
|
-u "${{ github.actor }}:${PKG_TOKEN}" \
|
||||||
--upload-file "$DEB_FILE" \
|
--upload-file "$DEB_FILE" \
|
||||||
"${SERVER}/api/packages/${OWNER}/debian/pool/stable/main/upload" || echo "[WARNING] DEB upload returned non-zero (may already exist)"
|
"${{ github.server_url }}/api/packages/${{ github.repository_owner }}/debian/pool/stable/main/upload")
|
||||||
|
echo "Debian Registry HTTP status: $HTTP_CODE"
|
||||||
# Link package to repository
|
|
||||||
curl -k -s -X POST -H "Authorization: token ${PKG_TOKEN}" \
|
|
||||||
"${SERVER}/api/v1/packages/${OWNER}/debian/linux-backuper/-/link/${REPO}" || true
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
- name: Publish Gitea Release & Upload Assets
|
# Link packages to this repository in Gitea
|
||||||
if: startsWith(github.ref, 'refs/tags/v')
|
REPO_NAME=$(basename "${{ github.repository }}")
|
||||||
|
echo "Linking RPM package 'linux-backuper' to repository ${REPO_NAME}..."
|
||||||
|
LINK_RPM=$(curl -k -s -w " (HTTP %{http_code})" -X POST \
|
||||||
|
-H "Authorization: token ${PKG_TOKEN}" \
|
||||||
|
"${{ github.server_url }}/api/v1/packages/${{ github.repository_owner }}/rpm/linux-backuper/-/link/${REPO_NAME}")
|
||||||
|
echo "RPM link result: $LINK_RPM"
|
||||||
|
|
||||||
|
echo "Linking Debian package 'linux-backuper' to repository ${REPO_NAME}..."
|
||||||
|
LINK_DEB=$(curl -k -s -w " (HTTP %{http_code})" -X POST \
|
||||||
|
-H "Authorization: token ${PKG_TOKEN}" \
|
||||||
|
"${{ github.server_url }}/api/v1/packages/${{ github.repository_owner }}/debian/linux-backuper/-/link/${REPO_NAME}")
|
||||||
|
echo "Debian link result: $LINK_DEB"
|
||||||
|
|
||||||
|
- name: Attach Artifacts to Gitea Release
|
||||||
env:
|
env:
|
||||||
GITEA_TOKEN: ${{ secrets.PACKAGES_GENERATION || secrets.GITHUB_TOKEN }}
|
GITHUB_TOKEN: ${{ secrets.PACKAGES_GENERATION || secrets.GITHUB_TOKEN }}
|
||||||
|
SERVER_URL: ${{ github.server_url }}
|
||||||
|
REPO: ${{ github.repository }}
|
||||||
|
TAG_REF: ${{ github.ref_name }}
|
||||||
run: |
|
run: |
|
||||||
TAG_NAME="${GITHUB_REF#refs/tags/}"
|
echo "=== Attaching Assets to Release ==="
|
||||||
OWNER="${{ github.repository_owner }}"
|
TAG="${TAG_REF}"
|
||||||
REPO="${{ github.event.repository.name }}"
|
if [[ "$TAG" == "main" || "$TAG" == "" ]]; then
|
||||||
API_URL="${{ github.server_url }}/api/v1/repos/${OWNER}/${REPO}"
|
VER=$(python3 -c "import linux_backuper; print(linux_backuper.__version__)")
|
||||||
|
TAG="v${VER}"
|
||||||
|
fi
|
||||||
|
echo "Target Release Tag: $TAG"
|
||||||
|
|
||||||
echo "[INFO] Ensuring Gitea Release exists for tag ${TAG_NAME}..."
|
# 1. Fetch release ID or create release if missing
|
||||||
REL_INFO=$(curl -k -s -H "Authorization: token ${GITEA_TOKEN}" "${API_URL}/releases/tags/${TAG_NAME}")
|
REL_JSON=$(curl -k -s -H "Authorization: token ${GITHUB_TOKEN}" \
|
||||||
REL_ID=$(echo "$REL_INFO" | grep -o '"id":[0-9]*' | head -n 1 | cut -d: -f2)
|
"${SERVER_URL}/api/v1/repos/${REPO}/releases/tags/${TAG}")
|
||||||
|
REL_ID=$(python3 -c "import sys, json; data=json.loads(sys.argv[1]); print(data.get('id', ''))" "$REL_JSON" 2>/dev/null || true)
|
||||||
|
|
||||||
if [[ -z "$REL_ID" || "$REL_ID" == "null" ]]; then
|
if [ -z "$REL_ID" ]; then
|
||||||
echo "[INFO] Creating release for tag ${TAG_NAME}..."
|
echo "Release $TAG does not exist yet. Creating release..."
|
||||||
CREATE_PAYLOAD=$(cat <<EOF
|
CREATE_DATA=$(python3 -c "import sys, json; print(json.dumps({'tag_name': sys.argv[1], 'name': 'linux-backuper ' + sys.argv[1], 'body': 'Automated build and release for ' + sys.argv[1] + ' (Debian, Red Hat/Fedora, and universal tarball).'}))" "$TAG")
|
||||||
{
|
CREATE_RESP=$(curl -k -s -X POST \
|
||||||
"tag_name": "${TAG_NAME}",
|
-H "Authorization: token ${GITHUB_TOKEN}" \
|
||||||
"name": "linux-backuper ${TAG_NAME}",
|
|
||||||
"body": "Automated release of linux-backuper.\\n\\nIncludes:\\n- Native Debian package (.deb)\\n- Native Red Hat / Fedora package (.rpm)\\n- Universal standalone archive (.tar.gz)",
|
|
||||||
"draft": false,
|
|
||||||
"prerelease": false
|
|
||||||
}
|
|
||||||
EOF
|
|
||||||
)
|
|
||||||
NEW_REL=$(curl -k -s -X POST -H "Authorization: token ${GITEA_TOKEN}" \
|
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-d "$CREATE_PAYLOAD" \
|
-d "$CREATE_DATA" \
|
||||||
"${API_URL}/releases")
|
"${SERVER_URL}/api/v1/repos/${REPO}/releases")
|
||||||
REL_ID=$(echo "$NEW_REL" | grep -o '"id":[0-9]*' | head -n 1 | cut -d: -f2)
|
REL_ID=$(python3 -c "import sys, json; data=json.loads(sys.argv[1]); print(data.get('id', ''))" "$CREATE_RESP" 2>/dev/null || true)
|
||||||
|
echo "Create release response: $CREATE_RESP"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "[INFO] Release ID: ${REL_ID}"
|
echo "Release ID: $REL_ID"
|
||||||
|
|
||||||
# Idempotently upload assets
|
if [ -n "$REL_ID" ]; then
|
||||||
for FPATH in "$TAR_FILE" "$DEB_FILE" "$RPM_FILE"; do
|
# Clean up any existing assets for this release
|
||||||
if [[ -f "$FPATH" ]]; then
|
EXISTING_ASSETS=$(curl -k -s -H "Authorization: token ${GITHUB_TOKEN}" \
|
||||||
FNAME=$(basename "$FPATH")
|
"${SERVER_URL}/api/v1/repos/${REPO}/releases/${REL_ID}/assets" || true)
|
||||||
echo "[INFO] Uploading asset: ${FNAME}..."
|
ASSET_IDS=$(python3 -c "import sys, json; print(' '.join(str(a['id']) for a in json.loads(sys.argv[1])))" "$EXISTING_ASSETS" 2>/dev/null || true)
|
||||||
|
for aid in $ASSET_IDS; do
|
||||||
|
echo "Removing previous asset ID $aid..."
|
||||||
|
curl -k -s -X DELETE -H "Authorization: token ${GITHUB_TOKEN}" \
|
||||||
|
"${SERVER_URL}/api/v1/repos/${REPO}/releases/${REL_ID}/assets/${aid}" || true
|
||||||
|
done
|
||||||
|
|
||||||
# Delete existing asset with same name if present
|
# Target only user-facing binaries in top-level dist/
|
||||||
EXISTING_ASSETS=$(curl -k -s -H "Authorization: token ${GITEA_TOKEN}" "${API_URL}/releases/${REL_ID}/assets")
|
ASSET_FILES=(
|
||||||
ASSET_ID=$(echo "$EXISTING_ASSETS" | grep -B 2 "\"name\":\"${FNAME}\"" | grep -o '"id":[0-9]*' | head -n 1 | cut -d: -f2 || true)
|
$(ls dist/*.tar.gz 2>/dev/null || true)
|
||||||
if [[ -n "$ASSET_ID" ]]; then
|
$(ls dist/*.deb 2>/dev/null || true)
|
||||||
echo "[INFO] Deleting existing asset ${FNAME} (ID: ${ASSET_ID})..."
|
$(ls dist/*.rpm 2>/dev/null || true)
|
||||||
curl -k -s -X DELETE -H "Authorization: token ${GITEA_TOKEN}" "${API_URL}/releases/${REL_ID}/assets/${ASSET_ID}"
|
)
|
||||||
fi
|
|
||||||
|
|
||||||
# Upload asset
|
for f in "${ASSET_FILES[@]}"; do
|
||||||
curl -k -s -X POST -H "Authorization: token ${GITEA_TOKEN}" \
|
if [ -f "$f" ]; then
|
||||||
-H "Content-Type: application/octet-stream" \
|
fname=$(basename "$f")
|
||||||
--data-binary @"$FPATH" \
|
echo "Uploading asset $fname from $f..."
|
||||||
"${API_URL}/releases/${REL_ID}/assets?name=${FNAME}"
|
curl -k -s -X POST \
|
||||||
|
-H "Authorization: token ${GITHUB_TOKEN}" \
|
||||||
|
-F "attachment=@${f}" \
|
||||||
|
"${SERVER_URL}/api/v1/repos/${REPO}/releases/${REL_ID}/assets?name=${fname}" || echo "Asset upload note: $?"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
echo "[SUCCESS] Release publishing complete."
|
echo "Artifacts successfully attached to release $TAG!"
|
||||||
|
else
|
||||||
|
echo "Could not obtain release ID, skipping asset attachment."
|
||||||
|
fi
|
||||||
@@ -511,6 +511,9 @@ def build_rpm_package(repo_root: str, out_dir: str, version: str) -> str:
|
|||||||
if not rpms_found:
|
if not rpms_found:
|
||||||
raise RuntimeError("rpmbuild succeeded but no .rpm file was found in RPMS")
|
raise RuntimeError("rpmbuild succeeded but no .rpm file was found in RPMS")
|
||||||
|
|
||||||
|
# Clean up temporary rpmbuild staging tree so out_dir contains only final artifacts
|
||||||
|
shutil.rmtree(rpmbuild_dir, ignore_errors=True)
|
||||||
|
|
||||||
print(f"[SUCCESS] Built RPM package: {rpms_found[0]} ({os.path.getsize(rpms_found[0])} bytes)")
|
print(f"[SUCCESS] Built RPM package: {rpms_found[0]} ({os.path.getsize(rpms_found[0])} bytes)")
|
||||||
return rpms_found[0]
|
return rpms_found[0]
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,116 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Helper script to create Gitea release and upload package assets.
|
||||||
|
|
||||||
|
Uses only Python standard library (urllib, json, os, sys).
|
||||||
|
"""
|
||||||
|
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import urllib.error
|
||||||
|
import urllib.request
|
||||||
|
|
||||||
|
|
||||||
|
def make_request(url: str, method: str = "GET", data: bytes = None, headers: dict = None):
|
||||||
|
req = urllib.request.Request(url, data=data, headers=headers or {}, method=method)
|
||||||
|
try:
|
||||||
|
with urllib.request.urlopen(req) as resp:
|
||||||
|
content = resp.read()
|
||||||
|
return resp.status, content
|
||||||
|
except urllib.error.HTTPError as e:
|
||||||
|
return e.code, e.read()
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
server_url = os.environ.get("GITHUB_SERVER_URL", "").rstrip("/")
|
||||||
|
owner = os.environ.get("GITHUB_REPOSITORY_OWNER", "")
|
||||||
|
repo = os.environ.get("GITHUB_REPOSITORY", "").split("/")[-1]
|
||||||
|
token = os.environ.get("GITEA_TOKEN", "")
|
||||||
|
ref = os.environ.get("GITHUB_REF", "")
|
||||||
|
|
||||||
|
if not ref.startswith("refs/tags/v"):
|
||||||
|
print(f"[INFO] Ref '{ref}' is not a release tag (refs/tags/v*). Skipping release publishing.")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
tag_name = ref.replace("refs/tags/", "")
|
||||||
|
api_url = f"{server_url}/api/v1/repos/{owner}/{repo}"
|
||||||
|
auth_headers = {"Authorization": f"token {token}"}
|
||||||
|
|
||||||
|
print(f"[INFO] Publishing release for tag '{tag_name}' to {api_url}")
|
||||||
|
|
||||||
|
# 1. Get or create release
|
||||||
|
status, body = make_request(f"{api_url}/releases/tags/{tag_name}", headers=auth_headers)
|
||||||
|
rel_id = None
|
||||||
|
if status == 200:
|
||||||
|
data = json.loads(body.decode("utf-8"))
|
||||||
|
rel_id = data.get("id")
|
||||||
|
print(f"[INFO] Existing release found (ID: {rel_id})")
|
||||||
|
else:
|
||||||
|
print(f"[INFO] Creating new release for tag {tag_name}...")
|
||||||
|
payload = json.dumps({
|
||||||
|
"tag_name": tag_name,
|
||||||
|
"name": f"linux-backuper {tag_name}",
|
||||||
|
"body": f"Automated release of linux-backuper {tag_name}.\n\nArtifacts:\n- Native Debian package (.deb)\n- Native Red Hat / Fedora package (.rpm)\n- Universal standalone archive (.tar.gz)",
|
||||||
|
"draft": False,
|
||||||
|
"prerelease": False,
|
||||||
|
}).encode("utf-8")
|
||||||
|
headers = dict(auth_headers)
|
||||||
|
headers["Content-Type"] = "application/json"
|
||||||
|
status, body = make_request(f"{api_url}/releases", method="POST", data=payload, headers=headers)
|
||||||
|
if status in (200, 201):
|
||||||
|
data = json.loads(body.decode("utf-8"))
|
||||||
|
rel_id = data.get("id")
|
||||||
|
print(f"[SUCCESS] Created release (ID: {rel_id})")
|
||||||
|
else:
|
||||||
|
print(f"[ERROR] Failed to create release: HTTP {status} - {body.decode('utf-8', errors='replace')}", file=sys.stderr)
|
||||||
|
return 1
|
||||||
|
|
||||||
|
# 2. Get existing assets
|
||||||
|
status, body = make_request(f"{api_url}/releases/{rel_id}/assets", headers=auth_headers)
|
||||||
|
existing_assets = {}
|
||||||
|
if status == 200:
|
||||||
|
for a in json.loads(body.decode("utf-8")):
|
||||||
|
existing_assets[a.get("name")] = a.get("id")
|
||||||
|
|
||||||
|
# 3. Upload assets from dist directory
|
||||||
|
dist_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "dist")
|
||||||
|
if not os.path.isdir(dist_dir):
|
||||||
|
print(f"[ERROR] Dist directory not found at {dist_dir}", file=sys.stderr)
|
||||||
|
return 1
|
||||||
|
|
||||||
|
asset_files = [f for f in os.listdir(dist_dir) if f.endswith((".tar.gz", ".deb", ".rpm")) and not f.startswith(".")]
|
||||||
|
for fname in sorted(asset_files):
|
||||||
|
fpath = os.path.join(dist_dir, fname)
|
||||||
|
if not os.path.isfile(fpath):
|
||||||
|
continue
|
||||||
|
|
||||||
|
# If asset exists, delete old one first
|
||||||
|
if fname in existing_assets:
|
||||||
|
asset_id = existing_assets[fname]
|
||||||
|
print(f"[INFO] Deleting existing asset {fname} (ID: {asset_id})...")
|
||||||
|
make_request(f"{api_url}/releases/{rel_id}/assets/{asset_id}", method="DELETE", headers=auth_headers)
|
||||||
|
|
||||||
|
print(f"[INFO] Uploading asset: {fname} ({os.path.getsize(fpath)} bytes)...")
|
||||||
|
with open(fpath, "rb") as f:
|
||||||
|
data = f.read()
|
||||||
|
|
||||||
|
headers = dict(auth_headers)
|
||||||
|
headers["Content-Type"] = "application/octet-stream"
|
||||||
|
status, body = make_request(
|
||||||
|
f"{api_url}/releases/{rel_id}/assets?name={fname}",
|
||||||
|
method="POST",
|
||||||
|
data=data,
|
||||||
|
headers=headers
|
||||||
|
)
|
||||||
|
if status in (200, 201):
|
||||||
|
print(f"[SUCCESS] Uploaded {fname}")
|
||||||
|
else:
|
||||||
|
print(f"[ERROR] Failed to upload {fname}: HTTP {status} - {body.decode('utf-8', errors='replace')}", file=sys.stderr)
|
||||||
|
return 1
|
||||||
|
|
||||||
|
print("[SUCCESS] All release assets uploaded successfully.")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
sys.exit(main())
|
||||||
Reference in new issue
Block a user