1
0
mirror of https://github.com/hanxi/xiaomusic.git synced 2026-03-15 08:13:16 +08:00

优化镜像构建

This commit is contained in:
涵曦
2026-01-14 16:51:23 +08:00
parent 25b34941fb
commit c48f8720c9
6 changed files with 65 additions and 159 deletions

View File

@@ -1,48 +0,0 @@
name: Build Docker Base Image
on:
push:
branches: ["main"]
paths:
- 'Dockerfile.builder'
- 'Dockerfile.runtime'
- 'install_dependencies.sh'
- '.github/workflows/build-base-image.yml'
workflow_dispatch:
permissions:
contents: read
jobs:
build-image:
runs-on: ubuntu-latest
if: github.event_name != 'pull_request'
strategy:
fail-fast: false
matrix:
image-type: [runtime, builder]
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
submodules: true
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push ${{ matrix.image-type }}
uses: docker/build-push-action@v4
with:
context: .
file: ./Dockerfile.${{ matrix.image-type }}
platforms: linux/amd64, linux/arm64, linux/arm/v7
push: true
tags: ${{ secrets.DOCKERHUB_USERNAME }}/xiaomusic:${{ matrix.image-type }}
# 核心缓存配置:每个镜像类型使用独立缓存标签
cache-from: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/xiaomusic:buildcache-${{ matrix.image-type }}
cache-to: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/xiaomusic:buildcache-${{ matrix.image-type }},mode=max

View File

@@ -22,12 +22,15 @@ jobs:
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 }
platform: [amd64, arm64, armv7]
include:
- platform: amd64
arch: amd64
- platform: arm64
arch: arm64
- platform: armv7
arch: arm/v7
steps:
- uses: actions/checkout@v4
with:
@@ -46,42 +49,27 @@ jobs:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and save Docker image for ${{ matrix.arch.suffix }}
- name: Build Docker image for ${{ matrix.platform }}
uses: docker/build-push-action@v6
with:
platforms: ${{ matrix.arch.platform }}
platforms: linux/${{ matrix.arch }}
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
tags: ${{ env.VERSION_TAG }}-${{ matrix.platform }}
cache-from: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/xiaomusic:buildcache
cache-to: ${{ matrix.platform == 'amd64' && 'type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/xiaomusic:buildcache,mode=max' || '' }}
- name: Save ${{ matrix.arch.suffix }} image to tar
- name: Test Docker image for ${{ matrix.platform }}
run: |
docker save ${{ env.VERSION_TAG }}-${{ matrix.arch.suffix }} -o xiaomusic-${{ github.ref_name }}-${{ matrix.arch.suffix }}.tar
docker run --rm ${{ env.VERSION_TAG }}-${{ matrix.platform }} /app/.venv/bin/python3 /app/xiaomusic.py -h
- 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
- name: Save ${{ matrix.platform }} image to tar
run: |
docker save ${{ env.VERSION_TAG }}-${{ matrix.platform }} -o xiaomusic-${{ github.ref_name }}-${{ matrix.platform }}.tar
# 汇总所有架构的镜像包
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
- name: Upload Docker images as artifacts
if: matrix.platform == 'amd64'
uses: actions/upload-artifact@v4
with:
name: docker-images
@@ -209,4 +197,4 @@ jobs:
- name: Publish package distributions to PyPI
run: pdm publish
continue-on-error: true
continue-on-error: true

View File

@@ -1,21 +1,49 @@
FROM hanxi/xiaomusic:builder AS builder
# 第一阶段:构建阶段
FROM python:3.14-alpine AS builder
# 安装构建依赖
RUN apk add --no-cache \
build-base \
nodejs \
npm \
zlib-dev \
jpeg-dev \
freetype-dev \
lcms2-dev \
openjpeg-dev \
tiff-dev \
libwebp-dev
# 安装PDM
RUN pip install -U pdm
ENV PDM_CHECK_UPDATE=false
WORKDIR /app
COPY pyproject.toml README.md package.json ./
RUN pip install -U pdm && \
pdm install --prod --no-editable -v && \
npm install --loglevel=verbose
# 安装Python和Node.js依赖
RUN pdm install --prod --no-editable -v
RUN npm install --loglevel=verbose
# 复制应用代码
COPY xiaomusic/ ./xiaomusic/
COPY plugins/ ./plugins/
COPY holiday/ ./holiday/
COPY xiaomusic.py .
FROM hanxi/xiaomusic:runtime
# 第二阶段:运行阶段
FROM python:3.14-alpine
# 安装运行时依赖
RUN apk add --no-cache \
ffmpeg \
nodejs \
npm
# 设置工作目录
WORKDIR /app
# 从构建阶段复制产物
COPY --from=builder /app/.venv ./.venv
COPY --from=builder /app/node_modules ./node_modules/
COPY --from=builder /app/xiaomusic/ ./xiaomusic/
@@ -24,15 +52,22 @@ COPY --from=builder /app/holiday/ ./holiday/
COPY --from=builder /app/xiaomusic.py .
COPY --from=builder /app/xiaomusic/__init__.py /base_version.py
COPY --from=builder /app/package.json .
# 创建FFmpeg软链接目录
RUN mkdir -p /app/ffmpeg/bin \
&& ln -s /usr/bin/ffmpeg /app/ffmpeg/bin/ffmpeg \
&& ln -s /usr/bin/ffprobe /app/ffmpeg/bin/ffprobe
RUN touch /app/.dockerenv
COPY supervisord.conf /etc/supervisor/supervisord.conf
RUN rm -f /var/run/supervisor.sock
# 设置卷和暴露端口
VOLUME /app/conf
VOLUME /app/music
EXPOSE 8090
# 设置环境变量
ENV TZ=Asia/Shanghai
ENV PATH=/app/.venv/bin:/usr/local/bin:$PATH
ENTRYPOINT ["/usr/bin/supervisord", "-c", "/etc/supervisor/supervisord.conf"]
# 直接启动xiaomusic应用
CMD ["/app/.venv/bin/python3", "/app/xiaomusic.py"]

View File

@@ -1,28 +0,0 @@
FROM python:3.12-slim-bookworm
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
nodejs \
npm \
zlib1g-dev \
libjpeg-dev \
libfreetype6-dev \
liblcms2-dev \
libopenjp2-7-dev \
libtiff-dev \
libwebp-dev \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
RUN pip install -U pdm
ENV PDM_CHECK_UPDATE=false
WORKDIR /app
COPY pyproject.toml README.md package.json ./
RUN pdm install --prod --no-editable -v
RUN npm install --loglevel=verbose
COPY xiaomusic/ ./xiaomusic/
COPY plugins/ ./plugins/
COPY holiday/ ./holiday/
COPY xiaomusic.py .

View File

@@ -1,13 +0,0 @@
FROM python:3.12-slim-bookworm
RUN apt-get update && apt-get install -y --no-install-recommends \
supervisor \
ffmpeg \
nodejs \
npm \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
RUN mkdir -p /app/ffmpeg/bin \
&& ln -s /usr/bin/ffmpeg /app/ffmpeg/bin/ffmpeg \
&& ln -s /usr/bin/ffprobe /app/ffmpeg/bin/ffprobe

View File

@@ -1,28 +0,0 @@
[unix_http_server]
file=/var/run/supervisor.sock ; Unix套接字文件路径
chmod=0777 ; 套接字权限
[supervisord]
nodaemon=true ; 前台运行
pidfile=/tmp/supervisord.pid ; PID文件路径
logfile=/dev/stdout ; 日志输出到标准输出
logfile_maxbytes=0 ; 禁用日志文件大小限制
loglevel=info ; 日志级别
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; 使用Unix套接字与服务通信
[program:xiaomusic]
command=/app/.venv/bin/python3 /app/xiaomusic.py
directory=/app
autostart=true
autorestart=true
startretries=99999
startsecs=60
redirect_stderr=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0