1
0
mirror of https://github.com/hanxi/xiaomusic.git synced 2026-05-18 11:06:47 +08:00

docker: 优化构建速度

This commit is contained in:
涵曦
2026-01-14 07:33:25 +08:00
parent 5740384309
commit 5ebb9f9e92

View File

@@ -18,9 +18,16 @@ permissions:
id-token: write
jobs:
# Job 构建和测试镜像
# Job 并行构建不同架构的镜像
build:
runs-on: ubuntu-latest
strategy:
fail-fast: false # 一个架构构建失败不影响其他架构
matrix:
arch:
- { platform: linux/amd64, suffix: amd64 }
- { platform: linux/arm64, suffix: arm64 }
- { platform: linux/arm/v7, suffix: armv7 }
steps:
- uses: actions/checkout@v4
with:
@@ -39,50 +46,42 @@ jobs:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and save Docker image for amd64
- name: Build and save Docker image for ${{ matrix.arch.suffix }}
uses: docker/build-push-action@v6
with:
platforms: linux/amd64
platforms: ${{ matrix.arch.platform }}
context: .
push: false
load: true
tags: ${{ env.VERSION_TAG }}-amd64
tags: ${{ env.VERSION_TAG }}-${{ matrix.arch.suffix }}
# 只有amd64架构写入缓存其他架构只读缓存避免缓存冲突
cache-from: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/xiaomusic:buildcache
cache-to: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/xiaomusic:buildcache,mode=max
${{ matrix.arch.suffix == 'amd64' && 'cache-to: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/xiaomusic:buildcache,mode=max' || '' }}
- name: Save amd64 image to tar
- name: Save ${{ matrix.arch.suffix }} image to tar
run: |
docker save ${{ env.VERSION_TAG }}-amd64 -o xiaomusic-${{ github.ref_name }}-amd64.tar
docker save ${{ env.VERSION_TAG }}-${{ matrix.arch.suffix }} -o xiaomusic-${{ github.ref_name }}-${{ matrix.arch.suffix }}.tar
- name: Build and save Docker image for arm64
uses: docker/build-push-action@v6
- name: Upload ${{ matrix.arch.suffix }} image as artifact
uses: actions/upload-artifact@v4
with:
platforms: linux/arm64
context: .
push: false
load: true
tags: ${{ env.VERSION_TAG }}-arm64
cache-from: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/xiaomusic:buildcache
name: docker-image-${{ matrix.arch.suffix }}
path: xiaomusic-${{ github.ref_name }}-${{ matrix.arch.suffix }}.tar
retention-days: 1
- name: Save arm64 image to tar
run: |
docker save ${{ env.VERSION_TAG }}-arm64 -o xiaomusic-${{ github.ref_name }}-arm64.tar
- name: Build and save Docker image for armv7
uses: docker/build-push-action@v6
# 汇总所有架构的镜像包
collect-artifacts:
runs-on: ubuntu-latest
needs: build
steps:
- name: Download all architecture artifacts
uses: actions/download-artifact@v4
with:
platforms: linux/arm/v7
context: .
push: false
load: true
tags: ${{ env.VERSION_TAG }}-armv7
cache-from: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/xiaomusic:buildcache
pattern: docker-image-*
merge-multiple: true
path: ./
- name: Save armv7 image to tar
run: |
docker save ${{ env.VERSION_TAG }}-armv7 -o xiaomusic-${{ github.ref_name }}-armv7.tar
- name: Upload Docker images as artifacts
- name: Upload combined Docker images as artifacts
uses: actions/upload-artifact@v4
with:
name: docker-images
@@ -92,7 +91,7 @@ jobs:
# Job 推送 Docker 镜像
push-docker:
runs-on: ubuntu-latest
needs: build
needs: collect-artifacts # 依赖汇总任务完成
if: github.ref_name == 'main' || startsWith(github.ref, 'refs/tags/v')
steps:
- uses: actions/checkout@v4
@@ -132,7 +131,7 @@ jobs:
# Job 发布 Release
publish-release:
runs-on: ubuntu-latest
needs: build
needs: collect-artifacts # 依赖汇总任务完成
if: github.ref_name == 'main' || startsWith(github.ref, 'refs/tags/v')
steps:
- uses: actions/checkout@v4
@@ -180,7 +179,7 @@ jobs:
# Job 发布 PyPI 版本
publish-pypi:
runs-on: ubuntu-latest
needs: build
needs: build # 只需要build完成即可不需要等汇总
if: startsWith(github.ref, 'refs/tags/v')
steps:
- uses: actions/checkout@v4