ci(docker): 添加Docker镜像构建和推送的GitHub Actions工作流 (#124)

* ci(docker): 添加Docker镜像构建和推送的GitHub Actions工作流
- 支持在main和develop分支及版本标签的push事件触发
- 支持Pull Request事件及手动触发工作流
- 配置了backend和frontend两个镜像的构建策略
- 使用QEMU和Docker Buildx实现多平台构建(amd64和arm64)
- 集成GitHub Container Registry和Docker Hub登录
- 自动生成镜像元数据和多标签支持
- 支持基于GitHub Actions缓存提升构建速度
- 实现根据事件类型自动决定是否推送镜像
- 输出构建完成的镜像摘要信息
* Update Docker Hub login condition in workflow
* Fix Docker Hub login condition in workflow
* Simplify Docker Hub login step
Removed conditional check for Docker Hub username.
* Change branch names in Docker build workflow
* Update docker-build.yml
This commit is contained in:
vicnoah
2025-11-05 16:55:09 +08:00
committed by GitHub
parent 754dc2f17b
commit f29e4b18d7
+95
View File
@@ -0,0 +1,95 @@
name: Build and Push Docker Images
on:
push:
branches:
- main
- dev
tags:
- 'v*'
pull_request:
branches:
- main
- dev
workflow_dispatch:
env:
REGISTRY_GHCR: ghcr.io
IMAGE_NAME_BACKEND: ${{ github.repository }}/nofx-backend
IMAGE_NAME_FRONTEND: ${{ github.repository }}/nofx-frontend
jobs:
build-and-push:
runs-on: ubuntu-22.04
permissions:
contents: read
packages: write
strategy:
matrix:
include:
- name: backend
dockerfile: ./docker/Dockerfile.backend
image_suffix: backend
- name: frontend
dockerfile: ./docker/Dockerfile.frontend
image_suffix: frontend
steps:
- name: Checkout code
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 GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY_GHCR }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
continue-on-error: true
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: |
${{ env.REGISTRY_GHCR }}/${{ github.repository }}/nofx-${{ matrix.image_suffix }}
${{ secrets.DOCKERHUB_USERNAME && format('{0}/nofx-{1}', secrets.DOCKERHUB_USERNAME, matrix.image_suffix) || '' }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=sha,prefix={{branch}}
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push ${{ matrix.name }} image
uses: docker/build-push-action@v5
with:
context: .
file: ${{ matrix.dockerfile }}
platforms: linux/amd64,linux/arm64
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
build-args: |
BUILD_DATE=${{ github.event.head_commit.timestamp }}
VCS_REF=${{ github.sha }}
VERSION=${{ github.ref_name }}
- name: Image digest
run: echo "Image digest for ${{ matrix.name }} - ${{ steps.meta.outputs.digest }}"