75 lines
2.3 KiB
YAML
75 lines
2.3 KiB
YAML
# GitHub Actions 工作流 - 构建和推送 Docker 镜像
|
|
name: Build and Push Docker Image
|
|
|
|
on:
|
|
push:
|
|
branches: [ main, develop ]
|
|
tags: [ 'v*' ]
|
|
pull_request:
|
|
branches: [ main ]
|
|
workflow_dispatch:
|
|
inputs:
|
|
image_tag:
|
|
description: "Tag to push (leave empty to use 'latest')"
|
|
required: false
|
|
default: ""
|
|
|
|
jobs:
|
|
docker:
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- name: Checkout
|
|
uses: actions/checkout@v4
|
|
|
|
# 解析镜像标签
|
|
- name: Resolve TAG
|
|
id: meta
|
|
env:
|
|
INPUT_TAG: ${{ github.event.inputs.image_tag || '' }}
|
|
run: |
|
|
set -euo pipefail
|
|
if [ -n "$INPUT_TAG" ]; then
|
|
TAG="$INPUT_TAG"
|
|
elif [ "${{ github.event_name }}" = "push" ] && [[ "${{ github.ref }}" =~ ^refs/tags/v ]]; then
|
|
TAG="${GITHUB_REF#refs/tags/}"
|
|
elif [ "${{ github.event_name }}" = "push" ] && [ "${{ github.ref }}" = "refs/heads/main" ]; then
|
|
TAG="latest"
|
|
elif [ "${{ github.event_name }}" = "push" ]; then
|
|
TAG="${GITHUB_REF#refs/heads/}"
|
|
else
|
|
TAG="dev"
|
|
fi
|
|
|
|
# 规范化标签
|
|
TAG="$(printf '%s' "$TAG" | tr '[:upper:]' '[:lower:]' | sed -E 's#[^a-z0-9._-]#-#g; s#/+#-#g; s#^[.-]+##; s#[.-]+$##')"
|
|
TAG="${TAG:0:128}"
|
|
echo "tag=$TAG" >> "$GITHUB_OUTPUT"
|
|
echo "Resolved TAG: $TAG"
|
|
|
|
- name: Set up Docker Buildx
|
|
uses: docker/setup-buildx-action@v3
|
|
|
|
- name: Login to DockerHub
|
|
uses: docker/login-action@v3
|
|
with:
|
|
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
|
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
|
|
|
- name: Build and Push Docker Image
|
|
uses: docker/build-push-action@v5
|
|
with:
|
|
context: .
|
|
file: ./docker/Dockerfile
|
|
platforms: linux/amd64,linux/arm64
|
|
push: true
|
|
tags: |
|
|
${{ secrets.DOCKERHUB_USERNAME }}/vinatools:${{ steps.meta.outputs.tag }}
|
|
${{ secrets.DOCKERHUB_USERNAME }}/vinatools:latest
|
|
build-args: |
|
|
VINA_VERSION=${{ vars.VINA_VERSION || '1.2.7' }}
|
|
cache-from: type=gha
|
|
cache-to: type=gha,mode=max
|
|
|
|
- name: Image Digest
|
|
run: echo ${{ steps.docker_build.outputs.digest }}
|