Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 36 additions & 54 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
@@ -1,81 +1,63 @@
name: Docker Build and Publish
# .github/workflows/build_and_push.yml
name: Build and Push Python Docker Image to Docker Hub (Manual Trigger)

on:
workflow_run:
workflows: ["Update Version File"]
types:
- completed
branches:
- master
- main
push:
tags:
- 'v*'
pull_request:
branches:
- master
- main
workflow_dispatch:
workflow_dispatch: # 触发:仅允许手动启动
inputs:
version:
description: '必须提供一个版本/标签 (例如 1.0.0 或 my-test-build)'
required: true
type: string
update_latest:
description: '是否也更新 latest 标签?'
required: false
type: boolean
default: false # 默认不更新 latest

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
# !!! 镜像名已设置为 gcli2ali !!!
IMAGE_NAME: ${{ secrets.DOCKERHUB_USERNAME }}/gcli2ali

jobs:
build-and-push:
runs-on: ubuntu-latest
# 只在 workflow_run 成功时运行,或者非 workflow_run 触发时运行
if: ${{ github.event_name != 'workflow_run' || github.event.workflow_run.conclusion == 'success' }}
permissions:
contents: read
packages: write

steps:
# 步骤 1: 检出代码
- name: Checkout repository
uses: actions/checkout@v4
with:
# workflow_run 触发时需要获取最新的代码(包括 version.txt 的更新)
ref: ${{ github.event_name == 'workflow_run' && github.event.workflow_run.head_branch || github.ref }}

- 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
# 步骤 2: 登录到 Docker Hub
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Extract metadata
# 步骤 3: 生成 Docker 镜像元数据 (标签和 Labels)
- name: Extract metadata (tags and labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
images: ${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=ref,event=tag
type=ref,event=pr
type=raw,value=latest,enable={{is_default_branch}}
type=sha,prefix={{branch}}-
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
# 规则 1: 使用手动输入的版本号作为标签
type=raw,value=${{ inputs.version }}
# 规则 2: 如果手动触发时 update_latest=true, 则添加 'latest' 标签
type=raw,value=latest,enable=${{ inputs.update_latest }}

# 步骤 4: 设置 Docker Buildx
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

# 步骤 5: 构建 Docker 镜像并推送到 Docker Hub
- name: Build and push Docker image
uses: docker/build-push-action@v5
uses: docker/build-push-action@v6
with:
context: .
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
context: . # Docker 构建上下文为当前目录
file: ./Dockerfile # 指定用于构建的 Dockerfile
push: true # 推送到仓库
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
BUILD_DATE=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.created'] }}
VERSION=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.version'] }}
REVISION=${{ github.sha }}