From 2d691c9538a4a6ffd55ed17f843ef647d9274efd Mon Sep 17 00:00:00 2001 From: xiaoxia Date: Thu, 9 Jul 2026 13:19:12 +0800 Subject: [PATCH] ci: optimize docker image tag strategy - develop branch: use fixed 'dev' tag (overwrite, no accumulation) - main branch: use 'latest' + commit sha tags - release tags (v*): use version number as tag - PR/other branches: skip docker push --- .gitea/workflows/ci.yml | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 342c4a8..017e4a0 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -3,8 +3,10 @@ name: CI Pipeline on: push: branches: [main, develop] + tags: + - 'v*' pull_request: - branches: [main] + branches: [main, develop] env: REGISTRY: git.xiaoxiajianji.com @@ -50,7 +52,12 @@ jobs: name: Build & Push Docker Image runs-on: ubuntu-latest needs: lint-and-build - if: github.ref == 'refs/heads/main' + # 仅在 main/develop 分支和 release tag 上构建推送镜像 + # PR 和其他分支不推送镜像,避免产生多余 tag + if: | + github.ref == 'refs/heads/main' || + github.ref == 'refs/heads/develop' || + startsWith(github.ref, 'refs/tags/v') steps: - name: Checkout code uses: actions/checkout@v4 @@ -71,13 +78,37 @@ jobs: username: ${{ gitea.actor }} password: ${{ secrets.GITEA_TOKEN }} + - name: Set image tags based on ref + id: tags + run: | + IMAGE="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}" + case "${{ github.ref }}" in + refs/heads/main) + # main 分支:latest + commit sha(版本号用 sha) + TAGS="${IMAGE}:latest,${IMAGE}:${{ gitea.sha }}" + ;; + refs/heads/develop) + # develop 分支:固定 dev tag(覆盖推送,不堆积) + TAGS="${IMAGE}:dev" + ;; + refs/tags/v*) + # release tag:用版本号作为 tag + VERSION="${GITHUB_REF_NAME}" + TAGS="${IMAGE}:${VERSION}" + ;; + *) + echo "::warning::No push for ref ${{ github.ref }}" + TAGS="" + ;; + esac + echo "tags=${TAGS}" + echo "tags=${TAGS}" >> $GITHUB_OUTPUT + - name: Build and push uses: actions/build-push-action@v6 with: context: . push: true - tags: | - ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest - ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ gitea.sha }} + tags: ${{ steps.tags.outputs.tags }} cache-from: type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:buildcache cache-to: type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:buildcache,mode=max