From f1e2eadd661bff9e716f0a000b6471ec88d72192 Mon Sep 17 00:00:00 2001 From: vicnoah Date: Wed, 5 Nov 2025 16:55:09 +0800 Subject: [PATCH] =?UTF-8?q?ci(docker):=20=E6=B7=BB=E5=8A=A0Docker=E9=95=9C?= =?UTF-8?q?=E5=83=8F=E6=9E=84=E5=BB=BA=E5=92=8C=E6=8E=A8=E9=80=81=E7=9A=84?= =?UTF-8?q?GitHub=20Actions=E5=B7=A5=E4=BD=9C=E6=B5=81=20(#124)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- .github/workflows/docker-build.yml | 95 ++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 .github/workflows/docker-build.yml diff --git a/.github/workflows/docker-build.yml b/.github/workflows/docker-build.yml new file mode 100644 index 00000000..84036674 --- /dev/null +++ b/.github/workflows/docker-build.yml @@ -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 }}"