Skip to content

feat: 添加 Docker 支持和 GitHub Actions 自动构建 #1

feat: 添加 Docker 支持和 GitHub Actions 自动构建

feat: 添加 Docker 支持和 GitHub Actions 自动构建 #1

name: Build and Push Docker Images
on:
push:
branches:
- main
tags:
- 'v*'
pull_request:
branches:
- main
workflow_dispatch:
inputs:
push_to_registry:
description: 'Push to Docker registry'
required: true
default: 'true'
type: choice
options:
- 'true'
- 'false'
env:
# Docker Hub 用户名(需要在 GitHub Secrets 中设置 DOCKERHUB_USERNAME)
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
# 镜像名称(可以修改为你的 Docker Hub 仓库名)
IMAGE_NAME: manga-translator
jobs:
build-and-push:
runs-on: ubuntu-latest
strategy:
matrix:
build_type: [cpu, gpu]
steps:
- name: Checkout repository
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: Log in to Docker Hub
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.DOCKERHUB_USERNAME }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch,suffix=-${{ matrix.build_type }}
type=ref,event=pr,suffix=-${{ matrix.build_type }}
type=semver,pattern={{version}},suffix=-${{ matrix.build_type }}
type=semver,pattern={{major}}.{{minor}},suffix=-${{ matrix.build_type }}
type=raw,value=latest-${{ matrix.build_type }},enable={{is_default_branch}}
flavor: |
latest=false
- name: Build and push Docker image
id: build
uses: docker/build-push-action@v5
with:
context: .
file: ./packaging/Dockerfile
push: ${{ github.event_name != 'pull_request' && (github.event.inputs.push_to_registry != 'false') }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
BUILD_TYPE=${{ matrix.build_type }}
CUDA_VERSION=12.1.0
PYTHON_VERSION=3.12
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: linux/amd64
- name: Image digest
run: echo ${{ steps.build.outputs.digest }}