1
0
mirror of https://github.com/hanxi/xiaomusic.git synced 2026-04-02 11:05:08 +08:00
Files
xiaomusic/.github/workflows/ci.yml
2026-01-14 07:38:22 +08:00

213 lines
6.8 KiB
YAML

name: CI Workflow
on:
push:
branches:
- "*" # 所有分支触发
tags:
- 'v*'
workflow_dispatch:
env:
VERSION_TAG: ${{ secrets.DOCKERHUB_USERNAME }}/xiaomusic:${{ github.ref_name }}
LATEST_TAG: ${{ secrets.DOCKERHUB_USERNAME }}/xiaomusic:latest
STABLE_TAG: ${{ secrets.DOCKERHUB_USERNAME }}/xiaomusic:stable
permissions:
contents: write
id-token: write
jobs:
# 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:
fetch-depth: 0
submodules: true
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and save Docker image for ${{ matrix.arch.suffix }}
uses: docker/build-push-action@v6
with:
platforms: ${{ matrix.arch.platform }}
context: .
push: false
load: true
tags: ${{ env.VERSION_TAG }}-${{ matrix.arch.suffix }}
# 核心修改:每个架构使用专属的缓存标签
cache-from: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/xiaomusic:buildcache-${{ matrix.arch.suffix }}
cache-to: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/xiaomusic:buildcache-${{ matrix.arch.suffix }},mode=max
- name: Save ${{ matrix.arch.suffix }} image to tar
run: |
docker save ${{ env.VERSION_TAG }}-${{ matrix.arch.suffix }} -o xiaomusic-${{ github.ref_name }}-${{ matrix.arch.suffix }}.tar
- name: Upload ${{ matrix.arch.suffix }} image as artifact
uses: actions/upload-artifact@v4
with:
name: docker-image-${{ matrix.arch.suffix }}
path: xiaomusic-${{ github.ref_name }}-${{ matrix.arch.suffix }}.tar
retention-days: 1
# 汇总所有架构的镜像包
collect-artifacts:
runs-on: ubuntu-latest
needs: build
steps:
- name: Download all architecture artifacts
uses: actions/download-artifact@v4
with:
pattern: docker-image-*
merge-multiple: true
path: ./
- name: Upload combined Docker images as artifacts
uses: actions/upload-artifact@v4
with:
name: docker-images
path: xiaomusic-*.tar
retention-days: 1
# Job 推送 Docker 镜像(推送时也使用按架构缓存)
push-docker:
runs-on: ubuntu-latest
needs: collect-artifacts
if: github.ref_name == 'main' || startsWith(github.ref, 'refs/tags/v')
steps:
- uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Publish to Docker Hub main
if: github.ref_name == 'main'
uses: docker/build-push-action@v6
with:
platforms: linux/amd64,linux/arm64,linux/arm/v7
context: .
push: true
tags: ${{ env.VERSION_TAG }}
# 推送时也复用各架构的缓存
cache-from: |
type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/xiaomusic:buildcache-amd64
type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/xiaomusic:buildcache-arm64
type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/xiaomusic:buildcache-armv7
- name: Publish to Docker Hub latest and stable
if: startsWith(github.ref, 'refs/tags/v')
uses: docker/build-push-action@v6
with:
platforms: linux/amd64,linux/arm64,linux/arm/v7
context: .
push: true
tags: ${{ env.VERSION_TAG }}, ${{ env.LATEST_TAG }}, ${{ env.STABLE_TAG }}
# 推送时也复用各架构的缓存
cache-from: |
type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/xiaomusic:buildcache-amd64
type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/xiaomusic:buildcache-arm64
type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/xiaomusic:buildcache-armv7
# 以下 publish-release、publish-pypi 任务保持不变,省略(和之前的配置一致)
publish-release:
runs-on: ubuntu-latest
needs: collect-artifacts
if: github.ref_name == 'main' || startsWith(github.ref, 'refs/tags/v')
steps:
- uses: actions/checkout@v4
- name: Download artifacts
uses: actions/download-artifact@v4
with:
name: docker-images
- name: Install GitHub CLI
run: |
sudo apt update
sudo apt install -y gh
- name: Create or update Release tag
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
RELEASE_NAME=${{ github.ref_name }}
RELEASE_BODY="This release is automatically updated from the ${RELEASE_NAME} branch."
EXISTING_RELEASE=$(gh release view "${RELEASE_NAME}" --json id --jq .id || echo "")
if [[ -n "${EXISTING_RELEASE}" ]]; then
echo "release exist"
else
gh release create "${RELEASE_NAME}" \
--prerelease=false \
--title "${RELEASE_NAME}" \
--notes "${RELEASE_BODY}"
fi
- name: Upload assets to Release tag
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
RELEASE_NAME=${{ github.ref_name }}
FILES=$(find . -type f -name "xiaomusic-*.tar")
for FILE in ${FILES}; do
echo "type upload ${FILE}"
gh release upload "${RELEASE_NAME}" "${FILE}" --clobber
done
publish-pypi:
runs-on: ubuntu-latest
needs: build
if: startsWith(github.ref, 'refs/tags/v')
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
submodules: true
- uses: actions/setup-node@v4
with:
registry-url: https://registry.npmjs.org/
node-version: lts/*
- name: Generate changelog
run: npx changelogithub
continue-on-error: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- uses: pdm-project/setup-pdm@v3
- name: Publish package distributions to PyPI
run: pdm publish
continue-on-error: true