1
0
mirror of https://github.com/hanxi/xiaomusic.git synced 2026-03-15 08:13:16 +08:00
Files
xiaomusic/.github/workflows/ci.yml
2026-01-16 00:59:55 +08:00

203 lines
5.7 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:
# 构建多架构 Docker 镜像并在所有平台上运行基本测试
build:
runs-on: ubuntu-latest
strategy:
matrix:
platform: [amd64, arm64, armv7]
include:
- platform: amd64
arch: amd64
- platform: arm64
arch: arm64
- platform: armv7
arch: arm/v7
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 Docker image for ${{ matrix.platform }}
uses: docker/build-push-action@v6
with:
platforms: linux/${{ matrix.arch }}
context: .
push: false
load: true
tags: ${{ env.VERSION_TAG }}
cache-from: type=gha,scope=${{ matrix.platform }}
cache-to: type=gha,mode=max,scope=${{ matrix.platform }}
- name: Test Docker image for ${{ matrix.platform }}
run: |
docker run --rm ${{ env.VERSION_TAG }} /app/.venv/bin/python3 /app/xiaomusic.py -h
- name: Save ${{ matrix.platform }} image to tar
run: |
docker save ${{ env.VERSION_TAG }} -o xiaomusic-${{ github.ref_name }}-${{ matrix.platform }}.tar
- name: Upload Docker images as artifacts
uses: actions/upload-artifact@v4
with:
name: docker-images-${{ matrix.platform }}
path: xiaomusic-${{ github.ref_name }}-${{ matrix.platform }}.tar
retention-days: 1
# 推送多架构 Docker 镜像到 Docker Hub
push-docker:
runs-on: ubuntu-latest
needs: build
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
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=gha,scope=amd64
type=gha,scope=arm64
type=gha,scope=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=gha,scope=amd64
type=gha,scope=arm64
type=gha,scope=armv7
# 推送镜像到 GitHub Release
publish-release:
runs-on: ubuntu-latest
needs: build
if: github.ref_name == 'main' || startsWith(github.ref, 'refs/tags/v')
steps:
- uses: actions/checkout@v4
- name: Download all platform artifacts
uses: actions/download-artifact@v4
with:
pattern: docker-images-*
merge-multiple: true
- 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
# 推送 PyPI 包
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