1 Commits

Author SHA1 Message Date
xiaoxia 2d691c9538 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
2026-07-09 13:19:12 +08:00
+36 -5
View File
@@ -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