refactor: 优化Docker工作流,简化标签生成和镜像构建步骤

This commit is contained in:
lan
2025-12-02 11:38:38 +08:00
parent 23be1c563d
commit 394ae7c953

View File

@@ -21,59 +21,44 @@ jobs:
- name: Checkout code - name: Checkout code
uses: actions/checkout@v4 uses: actions/checkout@v4
with: with:
fetch-depth: 0 # 获取完整历史以支持 git describe fetch-depth: 0
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Container Registry - name: Log in to Container Registry
uses: docker/login-action@v3 run: |
with: echo "${{ secrets.REGISTRY_PASSWORD }}" | docker login ${{ env.REGISTRY }} -u "${{ secrets.REGISTRY_USERNAME }}" --password-stdin
registry: ${{ env.REGISTRY }}
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWORD }}
- name: Extract metadata for Docker - name: Generate tags
id: meta id: tags
uses: docker/metadata-action@v5 run: |
with: TAGS="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.ref_name }}"
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} TAGS="$TAGS,${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:sha-$(echo ${{ github.sha }} | cut -c1-7)"
tags: | if [[ "${{ github.ref }}" == "refs/heads/main" || "${{ github.ref }}" == "refs/heads/master" ]]; then
type=ref,event=branch TAGS="$TAGS,${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest"
type=ref,event=tag fi
type=sha,prefix=sha- echo "tags=$TAGS" >> $GITHUB_OUTPUT
type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master' }} echo "生成的标签: $TAGS"
- name: Build and push Docker image - name: Build Docker image
uses: docker/build-push-action@v5 run: |
with: docker build -t build-image:local -f Dockerfile .
context: .
file: ./Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: linux/amd64
- name: Image digest - name: Tag and push Docker image
run: echo "Image pushed with digest ${{ steps.build.outputs.digest }}" run: |
IFS=',' read -ra TAG_ARRAY <<< "${{ steps.tags.outputs.tags }}"
for tag in "${TAG_ARRAY[@]}"; do
echo "推送标签: $tag"
docker tag build-image:local "$tag"
docker push "$tag"
done
# 可选:部署到服务器 - name: Build summary
deploy:
runs-on: ubuntu-latest
needs: build-and-push
if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master'
steps:
- name: Deploy notification
run: | run: |
echo "## 🚀 Docker 镜像构建完成" >> $GITHUB_STEP_SUMMARY echo "## 🚀 Docker 镜像构建完成" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY echo "" >> $GITHUB_STEP_SUMMARY
echo "镜像已推送到: \`${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}\`" >> $GITHUB_STEP_SUMMARY echo "镜像已推送到: \`${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}\`" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY echo "" >> $GITHUB_STEP_SUMMARY
echo "可用标签:" >> $GITHUB_STEP_SUMMARY echo "推送的标签:" >> $GITHUB_STEP_SUMMARY
echo "- \`latest\`" >> $GITHUB_STEP_SUMMARY IFS=',' read -ra TAG_ARRAY <<< "${{ steps.tags.outputs.tags }}"
echo "- \`${{ github.ref_name }}\`" >> $GITHUB_STEP_SUMMARY for tag in "${TAG_ARRAY[@]}"; do
echo "- \`sha-${{ github.sha }}\`" >> $GITHUB_STEP_SUMMARY echo "- \`$tag\`" >> $GITHUB_STEP_SUMMARY
done