Update build.yml

This commit is contained in:
xxnuo
2025-11-09 00:30:51 +08:00
parent 04faaf789b
commit d4ebf06215

View File

@@ -1,145 +1,83 @@
name: Build and Publish Docker Image
name: Build and Release
on:
workflow_dispatch:
push:
branches: [test]
branches: [main, test]
tags: ["v*"]
pull_request:
branches: [test]
branches: [main, test]
permissions:
contents: read
contents: write
packages: write
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
include:
- os: ubuntu-latest
goos: linux
goarch: amd64
- os: ubuntu-latest
goos: linux
goarch: arm64
- os: ubuntu-latest
goos: linux
goarch: 386
- os: macos-latest
goos: darwin
goarch: amd64
- os: macos-latest
goos: darwin
goarch: arm64
- os: windows-latest
goos: windows
goarch: amd64
runs-on: ${{ matrix.os }}
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Update packages
run: make update
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Go
uses: actions/setup-go@v4
with:
platforms: "arm64,amd64"
go-version: "1.25.3"
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Download resources
run: make download
env:
GOOS: ${{ matrix.goos }}
GOARCH: ${{ matrix.goarch }}
- name: Login to Docker Hub
if: github.event_name != 'pull_request'
uses: docker/login-action@v2
- name: Build binary
run: make build
env:
GOOS: ${{ matrix.goos }}
GOARCH: ${{ matrix.goarch }}
- name: Upload artifact
uses: actions/upload-artifact@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
name: mtranserver-${{ matrix.goos }}-${{ matrix.goarch }}
path: dist/mtranserver-${{ matrix.goos }}-${{ matrix.goarch }}*
- name: Login to GitHub Container Registry
if: github.event_name != 'pull_request'
uses: docker/login-action@v2
release:
needs: build
if: startsWith(github.ref, 'refs/tags/v')
runs-on: ubuntu-latest
steps:
- name: Download all artifacts
uses: actions/download-artifact@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
path: artifacts
- name: Extract metadata
id: meta
uses: docker/metadata-action@v4
- name: Create Release
uses: softprops/action-gh-release@v1
with:
images: |
ghcr.io/${{ github.repository }}
${{ secrets.DOCKERHUB_USERNAME }}/${{ github.event.repository.name }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=raw,value=latest,enable=${{ github.ref_type == 'tag' }}
# 确保标准镜像使用不带后缀的标签
flavor: |
latest=true
- name: Build and push standard image
id: build-standard
uses: docker/build-push-action@v4
with:
context: .
platforms: linux/amd64,linux/arm64/v8
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Extract metadata for Chinese model
id: meta-zh
uses: docker/metadata-action@v4
with:
images: |
ghcr.io/${{ github.repository }}
${{ secrets.DOCKERHUB_USERNAME }}/${{ github.event.repository.name }}
tags: |
type=ref,event=branch,suffix=-zh
type=ref,event=pr,suffix=-zh
type=semver,pattern={{version}}-zh
type=semver,pattern={{major}}.{{minor}}-zh
type=raw,value=latest,enable=${{ github.ref_type == 'tag' }}
# 确保生成的标签与其他镜像不冲突
flavor: |
suffix=-zh,onlatest=true
- name: Build and push Chinese model image
id: build-zh
# 确保中文镜像在标准镜像构建完成后构建
if: ${{ success() && (steps.build-standard.outcome == 'success' || steps.build-standard.outcome == 'skipped') }}
uses: docker/build-push-action@v4
with:
context: .
file: Dockerfile.model
platforms: linux/amd64,linux/arm64/v8
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta-zh.outputs.tags }}
labels: ${{ steps.meta-zh.outputs.labels }}
build-args: |
PRELOAD_SRC_LANG=zh-Hans
PRELOAD_TARGET_LANG=zh-Hans
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Extract metadata for Japanese model
id: meta-ja
uses: docker/metadata-action@v4
with:
images: |
ghcr.io/${{ github.repository }}
${{ secrets.DOCKERHUB_USERNAME }}/${{ github.event.repository.name }}
tags: |
type=ref,event=branch,suffix=-ja
type=ref,event=pr,suffix=-ja
type=semver,pattern={{version}}-ja
type=semver,pattern={{major}}.{{minor}}-ja
type=raw,value=latest,enable=${{ github.ref_type == 'tag' }}
# 确保生成的标签与其他镜像不冲突
flavor: |
suffix=-ja,onlatest=true
- name: Build and push Japanese model image
id: build-ja
# 确保日语镜像在标准镜像构建完成后构建
if: ${{ success() && (steps.build-standard.outcome == 'success' || steps.build-standard.outcome == 'skipped') }}
uses: docker/build-push-action@v4
with:
context: .
file: Dockerfile.model
platforms: linux/amd64,linux/arm64/v8
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta-ja.outputs.tags }}
labels: ${{ steps.meta-ja.outputs.labels }}
build-args: |
PRELOAD_SRC_LANG=ja
PRELOAD_TARGET_LANG=ja
cache-from: type=gha
cache-to: type=gha,mode=max
files: artifacts/**/*
draft: false
prerelease: false
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}