refactor: 重構釋放工作流程,分開構建 AMD64 和 ARM64 映像以優化磁碟空間使用

This commit is contained in:
Your Name 2026-01-25 16:22:03 +08:00
parent 7f74d18089
commit 1eaf5e4c2c

View file

@ -19,9 +19,9 @@ env:
jobs: jobs:
# ============================================================================== # ==============================================================================
# 🐳 Build and Push Docker Image關鍵流程 # 🐳 Build AMD64 Image分開構建解決空間不足問題
# ============================================================================== # ==============================================================================
build-and-push: build-amd64:
runs-on: ubuntu-latest runs-on: ubuntu-latest
permissions: permissions:
contents: read contents: read
@ -31,12 +31,6 @@ jobs:
- name: Checkout repository - name: Checkout repository
uses: actions/checkout@v4 uses: actions/checkout@v4
# ========================================
# 清理磁碟空間(解決 no space left on device
# ========================================
# 🔥 GitHub Actions Runner 只有約 14GB 可用空間
# 大型模型 + Multi-Arch build 需要清理更多預裝軟體
# ========================================
# ======================================== # ========================================
# 🔥 極限磁碟清理(釋放 50-60GB 空間) # 🔥 極限磁碟清理(釋放 50-60GB 空間)
# ======================================== # ========================================
@ -54,72 +48,81 @@ jobs:
- name: Additional disk cleanup - name: Additional disk cleanup
run: | run: |
echo "🧹 額外磁碟清理..." echo "🧹 額外磁碟清理..."
echo "========================================"
echo "📊 初始磁碟空間:"
df -h / df -h /
echo "========================================" sudo rm -rf /usr/share/swift /var/lib/mysql /var/lib/postgresql || true
sudo rm -rf /usr/share/doc /usr/share/man /usr/share/locale || true
# ======================================== sudo rm -rf /usr/local/go ~/go /usr/local/julia* || true
# 移除額外大型軟體 sudo apt-get clean && sudo rm -rf /var/lib/apt/lists/* || true
# ======================================== docker system prune -af --volumes || true
echo "🗑️ 移除額外預裝軟體..." df -h /
# Swift約 1.5GB - name: Get tag name
sudo rm -rf /usr/share/swift || true id: tag
run: |
# 移除預裝資料庫 if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
sudo rm -rf /var/lib/mysql || true echo "version=${{ github.event.inputs.tag }}" >> $GITHUB_OUTPUT
sudo rm -rf /var/lib/postgresql || true else
echo "version=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
# 移除預裝瀏覽器 fi
sudo rm -rf /usr/local/share/chromium || true
sudo rm -rf /usr/local/share/chrome || true - name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
# 移除文件和 man pages
sudo rm -rf /usr/share/doc || true - name: Login to Docker Hub
sudo rm -rf /usr/share/man || true uses: docker/login-action@v3
sudo rm -rf /usr/share/locale || true with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
# 移除 Rust 工具鏈 password: ${{ secrets.DOCKERHUB_TOKEN }}
sudo rm -rf /usr/share/rust || true
sudo rm -rf ~/.rustup || true - name: Build and push AMD64 image
sudo rm -rf ~/.cargo || true uses: docker/build-push-action@v6
with:
# 移除 Go context: .
sudo rm -rf /usr/local/go || true file: Dockerfile
sudo rm -rf ~/go || true push: true
tags: ${{ env.DOCKER_IMAGE }}:${{ steps.tag.outputs.version }}-amd64
# 移除 Julia platforms: linux/amd64
sudo rm -rf /usr/local/julia* || true cache-from: type=registry,ref=${{ env.DOCKER_IMAGE }}:buildcache-amd64
cache-to: type=registry,ref=${{ env.DOCKER_IMAGE }}:buildcache-amd64,mode=max,compression=zstd
# 移除 R provenance: false
sudo rm -rf /usr/share/R || true sbom: false
sudo rm -rf /usr/lib/R || true build-args: |
BUILDKIT_INLINE_CACHE=1
# 清理 apt
sudo apt-get clean || true # ==============================================================================
sudo apt-get autoremove -y || true # 🐳 Build ARM64 Image分開構建解決空間不足問題
sudo rm -rf /var/lib/apt/lists/* || true # ==============================================================================
sudo rm -rf /var/cache/apt/archives || true build-arm64:
runs-on: ubuntu-latest
# 清理 npm/yarn permissions:
sudo rm -rf /usr/local/share/.cache || true contents: read
sudo rm -rf ~/.npm || true packages: write
# 清理 pip cache steps:
sudo rm -rf ~/.cache/pip || true - name: Checkout repository
uses: actions/checkout@v4
# 清理 Docker確保乾淨
- name: Free disk space (maximum)
uses: jlumbroso/free-disk-space@main
with:
tool-cache: true
android: true
dotnet: true
haskell: true
large-packages: true
docker-images: true
swap-storage: true
- name: Additional disk cleanup
run: |
echo "🧹 額外磁碟清理..."
df -h /
sudo rm -rf /usr/share/swift /var/lib/mysql /var/lib/postgresql || true
sudo rm -rf /usr/share/doc /usr/share/man /usr/share/locale || true
sudo rm -rf /usr/local/go ~/go /usr/local/julia* || true
sudo apt-get clean && sudo rm -rf /var/lib/apt/lists/* || true
docker system prune -af --volumes || true docker system prune -af --volumes || true
sudo rm -rf /var/lib/docker/buildkit || true
# 清理 tmp
sudo rm -rf /tmp/* || true
echo "========================================"
echo "📊 清理後磁碟空間:"
df -h / df -h /
echo "========================================"
- name: Get tag name - name: Get tag name
id: tag id: tag
@ -133,16 +136,8 @@ jobs:
- name: Set up QEMU - name: Set up QEMU
uses: docker/setup-qemu-action@v3 uses: docker/setup-qemu-action@v3
# ========================================
# 設定 Docker Buildx針對大型模型優化
# ========================================
- name: Set up Docker Buildx - name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3 uses: docker/setup-buildx-action@v3
with:
# 🔥 使用 docker-container driver 以獲得更好的 cache 控制
driver-opts: |
image=moby/buildkit:latest
network=host
- name: Login to Docker Hub - name: Login to Docker Hub
uses: docker/login-action@v3 uses: docker/login-action@v3
@ -150,44 +145,70 @@ jobs:
username: ${{ secrets.DOCKERHUB_USERNAME }} username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }} password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Login to GitHub Container Registry - name: Build and push ARM64 image
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# ========================================
# Build and Push針對大型模型優化
# ========================================
# 🔥 注意事項:
# - 使用 registry cache 而非 GHA cache避免 10GB 限制)
# - 啟用 zstd compression 減少傳輸量和儲存空間
# - Multi-arch build 需要更多磁碟空間,已在上方清理
# - 明確指定 file: Dockerfile 避免與 Lite 版混淆
# - 使用 CACHE_BUST 強制重新下載模型
# - 🔥 禁用 provenance 減少磁碟使用
# ========================================
- name: Build and push Docker image
uses: docker/build-push-action@v6 uses: docker/build-push-action@v6
with: with:
context: . context: .
file: Dockerfile file: Dockerfile
push: true push: true
tags: | tags: ${{ env.DOCKER_IMAGE }}:${{ steps.tag.outputs.version }}-arm64
${{ env.DOCKER_IMAGE }}:${{ steps.tag.outputs.version }} platforms: linux/arm64
${{ env.DOCKER_IMAGE }}:latest cache-from: type=registry,ref=${{ env.DOCKER_IMAGE }}:buildcache-arm64
platforms: linux/amd64,linux/arm64 cache-to: type=registry,ref=${{ env.DOCKER_IMAGE }}:buildcache-arm64,mode=max,compression=zstd
# 使用 registry cache已手動刪除 Docker Hub 上的 buildcache tag
cache-from: type=registry,ref=${{ env.DOCKER_IMAGE }}:buildcache-full
cache-to: type=registry,ref=${{ env.DOCKER_IMAGE }}:buildcache-full,mode=max,compression=zstd
# 🔥 禁用 provenance 和 sbom 減少磁碟和記憶體使用
provenance: false provenance: false
sbom: false sbom: false
# 設定 build 參數(使用時間戳強制重新下載模型層)
build-args: | build-args: |
BUILDKIT_INLINE_CACHE=1 BUILDKIT_INLINE_CACHE=1
CACHE_BUST=${{ github.run_id }}
# ==============================================================================
# 🐳 Create Multi-Arch Manifest合併兩個架構
# ==============================================================================
create-manifest:
runs-on: ubuntu-latest
needs: [build-amd64, build-arm64]
permissions:
contents: read
packages: write
steps:
- name: Get tag name
id: tag
run: |
if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
echo "version=${{ github.event.inputs.tag }}" >> $GITHUB_OUTPUT
else
echo "version=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
fi
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Create and push multi-arch manifest
run: |
VERSION="${{ steps.tag.outputs.version }}"
IMAGE="${{ env.DOCKER_IMAGE }}"
echo "🔄 創建 multi-arch manifest..."
# 創建版本 tag 的 manifest
docker buildx imagetools create -t ${IMAGE}:${VERSION} \
${IMAGE}:${VERSION}-amd64 \
${IMAGE}:${VERSION}-arm64
# 創建 latest tag 的 manifest
docker buildx imagetools create -t ${IMAGE}:latest \
${IMAGE}:${VERSION}-amd64 \
${IMAGE}:${VERSION}-arm64
echo "✅ Multi-arch manifest 創建完成"
# 驗證 manifest
echo "🔍 驗證 manifest..."
docker buildx imagetools inspect ${IMAGE}:${VERSION}
docker buildx imagetools inspect ${IMAGE}:latest
- name: Docker Build Summary - name: Docker Build Summary
run: | run: |
@ -207,7 +228,7 @@ jobs:
verify-image: verify-image:
name: Verify Docker Image name: Verify Docker Image
runs-on: ubuntu-latest runs-on: ubuntu-latest
needs: build-and-push needs: create-manifest
timeout-minutes: 45 timeout-minutes: 45
steps: steps:
@ -534,7 +555,7 @@ jobs:
# ============================================================================== # ==============================================================================
create-release: create-release:
runs-on: ubuntu-latest runs-on: ubuntu-latest
needs: [build-and-push, verify-image] needs: [create-manifest, verify-image]
permissions: permissions:
contents: write contents: write