1
0
mirror of https://github.com/hanxi/xiaomusic.git synced 2025-12-08 15:08:12 +08:00
Files
xiaomusic/.github/workflows/ci.yml
2024-12-15 01:55:30 +08:00

218 lines
7.8 KiB
YAML

name: ci
on:
push:
branches:
- "main"
workflow_dispatch:
env:
TEST_TAG: ${{ secrets.DOCKERHUB_USERNAME }}/xiaomusic:${{ github.ref_name }}
RELEASE_NAME: main # 固定的 Release 名称
jobs:
build-test-publish:
runs-on: ubuntu-latest
if: github.event_name != 'pull_request'
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: Build Docker image (linux/amd64)
uses: docker/build-push-action@v6
with:
platforms: linux/amd64
context: .
push: false
load: true
tags: ${{ env.TEST_TAG }}-linux-amd64
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new
- name: Build Docker image (linux/arm64)
uses: docker/build-push-action@v6
with:
platforms: linux/arm64
context: .
push: false
load: true
tags: ${{ env.TEST_TAG }}-linux-arm64
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new
- name: Build Docker image (linux/arm/v7)
uses: docker/build-push-action@v6
with:
platforms: linux/arm/v7
context: .
push: false
load: true
tags: ${{ env.TEST_TAG }}-linux-arm-v7
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new
# We test all the images on amd64 host here. This uses QEMU to emulate
# the other platforms.
- run: docker run --rm ${TEST_TAG}-linux-amd64 /app/.venv/bin/python3 /app/xiaomusic.py -h
- run: docker run --rm ${TEST_TAG}-linux-arm64 /app/.venv/bin/python3 /app/xiaomusic.py -h
- run: docker run --rm ${TEST_TAG}-linux-arm-v7 /app/.venv/bin/python3 /app/xiaomusic.py -h
# 打包完整版本和精简版本 tar.gz 文件
- name: Package /app for amd64
run: |
docker run --rm -v $PWD:/workspace ${TEST_TAG}-linux-amd64 tar czf /workspace/app-amd64.tar.gz -C / app
- name: Package /app (lite) for amd64
run: |
docker run --rm -v $PWD:/workspace ${TEST_TAG}-linux-amd64 bash -c \
"cd /app && tar --exclude='ffmpeg' -czf /workspace/app-amd64-lite.tar.gz *"
- name: Package /app for arm64
run: |
docker run --rm -v $PWD:/workspace ${TEST_TAG}-linux-arm64 tar czf /workspace/app-arm64.tar.gz -C / app
- name: Package /app (lite) for arm64
run: |
docker run --rm -v $PWD:/workspace ${TEST_TAG}-linux-arm64 bash -c \
"cd /app && tar --exclude='ffmpeg' -czf /workspace/app-arm64-lite.tar.gz *"
- name: Package /app for arm/v7
run: |
docker run --rm -v $PWD:/workspace ${TEST_TAG}-linux-arm-v7 tar czf /workspace/app-arm-v7.tar.gz -C / app
- name: Package /app (lite) for arm/v7
run: |
docker run --rm -v $PWD:/workspace ${TEST_TAG}-linux-arm-v7 bash -c \
"cd /app && tar --exclude='ffmpeg' -czf /workspace/app-arm-v7-lite.tar.gz *"
# This will only push the previously built images.
- name: Publish to Docker Hub
uses: docker/build-push-action@v6
with:
platforms: linux/amd64,linux/arm64,linux/arm/v7
context: .
push: true
tags: ${{ env.TEST_TAG }}
cache-from: type=local,src=/tmp/.buildx-cache-new
cache-to: type=local,dest=/tmp/.buildx-cache-new
- name: Docker Hub Description
uses: peter-evans/dockerhub-description@v4
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
repository: hanxi/xiaomusic
- name: Move cache to limit growth
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
update-release:
runs-on: ubuntu-latest
needs: build-test-publish
steps:
- uses: actions/checkout@v4
- name: Check if Release exists
id: check_release
uses: actions/github-script@v6
with:
script: |
try {
const { data: releases } = await github.repos.listReleases({
owner: context.repo.owner,
repo: context.repo.repo
});
const release = releases.find(r => r.tag_name === process.env.RELEASE_NAME);
if (release) {
core.setOutput('exists', true);
core.setOutput('id', release.id);
core.setOutput('upload_url', release.upload_url);
} else {
core.setOutput('exists', false);
}
} catch (error) {
core.setOutput('exists', false);
}
- name: Create Release if not exists
if: steps.check_release.outputs.exists == 'false'
id: create_release
uses: actions/github-script@v6
with:
script: |
const { data: release } = await github.repos.createRelease({
owner: context.repo.owner,
repo: context.repo.repo,
tag_name: process.env.RELEASE_NAME,
name: "Latest Release",
body: "This is the latest release for the main branch.",
draft: false,
prerelease: false
});
core.setOutput('id', release.id);
core.setOutput('upload_url', release.upload_url);
- name: Get Upload URL for Existing Release
if: steps.check_release.outputs.exists == 'true'
run: echo "UPLOAD_URL=${{ steps.check_release.outputs.upload_url }}" >> $GITHUB_ENV
# 上传打包文件
- name: Upload app-amd64.tar.gz
uses: actions/upload-release-asset@v1
with:
upload_url: ${{ steps.create_release.outputs.upload_url || env.UPLOAD_URL }}
asset_path: ./app-amd64.tar.gz
asset_name: app-amd64.tar.gz
asset_content_type: application/gzip
- name: Upload app-amd64-lite.tar.gz
uses: actions/upload-release-asset@v1
with:
upload_url: ${{ steps.create_release.outputs.upload_url || env.UPLOAD_URL }}
asset_path: ./app-amd64-lite.tar.gz
asset_name: app-amd64-lite.tar.gz
asset_content_type: application/gzip
- name: Upload app-arm64.tar.gz
uses: actions/upload-release-asset@v1
with:
upload_url: ${{ steps.create_release.outputs.upload_url || env.UPLOAD_URL }}
asset_path: ./app-arm64.tar.gz
asset_name: app-arm64.tar.gz
asset_content_type: application/gzip
- name: Upload app-arm64-lite.tar.gz
uses: actions/upload-release-asset@v1
with:
upload_url: ${{ steps.create_release.outputs.upload_url || env.UPLOAD_URL }}
asset_path: ./app-arm64-lite.tar.gz
asset_name: app-arm64-lite.tar.gz
asset_content_type: application/gzip
- name: Upload app-arm-v7.tar.gz
uses: actions/upload-release-asset@v1
with:
upload_url: ${{ steps.create_release.outputs.upload_url || env.UPLOAD_URL }}
asset_path: ./app-arm-v7.tar.gz
asset_name: app-arm-v7.tar.gz
asset_content_type: application/gzip
- name: Upload app-arm-v7-lite.tar.gz
uses: actions/upload-release-asset@v1
with:
upload_url: ${{ steps.create_release.outputs.upload_url || env.UPLOAD_URL }}
asset_path: ./app-arm-v7-lite.tar.gz
asset_name: app-arm-v7-lite.tar.gz
asset_content_type: application/gzip