Skip to content

Multi-Distro Images Builder #296

Multi-Distro Images Builder

Multi-Distro Images Builder #296

Workflow file for this run

name: Multi-Distro Images Builder
on:
schedule:
- cron: "0 3 * * *"
workflow_dispatch:
jobs:
build-images:
strategy:
fail-fast: false
max-parallel: 6
matrix:
distro:
[
"openeuler",
"debian",
"kali",
"centos",
"almalinux",
"rockylinux",
"fedora", # 编译无效
"centos", # 编译无效
"opensuse",
"alpine",
"archlinux",
"gentoo",
"openwrt", # 编译无效
"oracle",
"ubuntu",
]
arch:
- name: amd64
runner: ubuntu-latest
- name: arm64
runner: ubuntu-24.04-arm
runs-on: ${{ matrix.arch.runner }}
timeout-minutes: 360
steps:
- uses: actions/checkout@v4
- name: Check workspace
run: pwd
- name: Build environment
run: |
sudo apt update -y
sudo apt install -y polkit || sudo apt install -y policykit-1
sudo apt install -y jq gh
- name: Configure Git Identity
run: |
git config --global user.name "daily-update"
git config --global user.email "tg@spiritlhl.top"
- name: Build and Upload Images
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
set +e
DISTRO="${{ matrix.distro }}"
ARCH="${{ matrix.arch.name }}"
echo "Processing $DISTRO for $ARCH architecture"
echo "---zip_name_list---"
output=$(bash build_images.sh $DISTRO false $ARCH | tail -n 1)
zip_name_list=($output)
echo "---zip_name_list---"
for item in "${zip_name_list[@]}"; do
echo "$item"
done
echo "-------"
# 检查或创建release
if ! gh release view "$DISTRO" >/dev/null 2>&1; then
echo "Creating new release for $DISTRO"
gh release create "$DISTRO" --title "$DISTRO Images" --notes "Automated $DISTRO image builds"
fi
echo "Building $DISTRO and packaging zips for $ARCH"
sudo bash build_images.sh $DISTRO true $ARCH || true
echo "------------"
pwd
ls
echo "------------"
# GitHub Release文件大小限制检查
MAX_FILE_SIZE=$((2 * 1024 * 1024 * 1024)) # 2GB
for file in "${zip_name_list[@]}"; do
if [ -f "$file" ]; then
file_size=$(stat -c %s "$file")
echo "Processing $file (size: $(numfmt --to=iec-i --suffix=B $file_size))"
# 跳过过小的文件
if [ $file_size -lt 10485760 ]; then
echo "Skipping $file - size <10MB"
rm -vf "$file"
continue
fi
# 检查文件是否过大
if [ $file_size -gt $MAX_FILE_SIZE ]; then
echo "Warning: $file is too large for GitHub Release ($(numfmt --to=iec-i --suffix=B $file_size))"
echo "Attempting to compress further or split the file..."
# 尝试重新压缩
temp_file="${file%.zip}_compressed.zip"
if command -v 7z >/dev/null 2>&1; then
echo "Attempting 7z compression..."
7z a -mx=9 "$temp_file" "${file%.zip}/" 2>/dev/null || true
if [ -f "$temp_file" ] && [ $(stat -c %s "$temp_file") -lt $MAX_FILE_SIZE ]; then
rm -f "$file"
mv "$temp_file" "$file"
file_size=$(stat -c %s "$file")
echo "Compressed to $(numfmt --to=iec-i --suffix=B $file_size)"
else
rm -f "$temp_file"
echo "Compression failed or file still too large, skipping..."
rm -vf "$file"
continue
fi
else
echo "7z not available, skipping file"
rm -vf "$file"
continue
fi
fi
asset_name=$(basename "$file")
# 删除已存在的asset
echo "Checking for existing asset: $asset_name"
if gh release view "$DISTRO" --json assets --jq ".assets[].name" | grep -q "^$asset_name$"; then
echo "Removing existing asset: $asset_name"
gh release delete-asset "$DISTRO" "$asset_name" --yes || true
sleep 2 # 等待删除完成
fi
# 上传文件
echo "Uploading $asset_name..."
max_retries=3
retry_count=0
upload_success=false
while [ $retry_count -lt $max_retries ] && [ "$upload_success" = false ]; do
if gh release upload "$DISTRO" "$file" --clobber; then
echo "Successfully uploaded $asset_name"
upload_success=true
else
retry_count=$((retry_count + 1))
echo "Upload attempt $retry_count failed for $asset_name"
if [ $retry_count -lt $max_retries ]; then
echo "Retrying in 10 seconds..."
sleep 10
fi
fi
done
# 如果GitHub CLI失败,尝试curl作为备用方案
if [ "$upload_success" = false ]; then
echo "All GitHub CLI attempts failed, trying curl backup method..."
release_id=$(gh release view "$DISTRO" --json id --jq .id 2>/dev/null || echo "")
if [ -n "$release_id" ] && [ "$release_id" != "null" ]; then
upload_url="https://uploads.github.com/repos/oneclickvirt/incus_images/releases/$release_id/assets?name=$asset_name"
if curl -sS -H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Content-Type: application/zip" \
--data-binary @"$file" \
"$upload_url" >/dev/null 2>&1; then
echo "Backup curl upload successful for $asset_name"
upload_success=true
else
echo "Backup curl upload also failed for $asset_name"
fi
else
echo "Could not get release ID for backup upload"
fi
fi
if [ "$upload_success" = false ]; then
echo "All upload methods failed for $asset_name"
fi
# 清理文件
sudo rm -vf "$file"
else
echo "File $file does not exist"
fi
done
continue-on-error: true