原 .github/workflows/docker.yml:
- 触发: v* tag + 手动
- 仅推 GHCR
- 无 build 缓存
- buildx action v5
新版本:
- 触发: v*.*.* tag (限定 semver, 避免 vtest 误触发) + 手动按钮
- 双 registry 推送: GHCR (默认) + DockerHub (blueink05/ttshub,需 secrets)
- GHA buildx cache (cache-from + cache-to, mode=max): 首次 ~3-5 min, 后续 30-60s
- buildx action v6
- multi-arch (amd64+arm64, 不变)
- tag 策略: semver + major.minor + tag->latest + short sha
- 安全网: secrets.DOCKERHUB_USERNAME 空时自动跳过 DockerHub login + image name 渲染,
避免生成 'docker.io//ttshub' 这种非法 image 名
- 不验 PR 编译(合到 main 不重跑, 只在打 tag 时构建)
- workflow_dispatch 推镜像: push 表达式保留 '!= pull_request' 兼容未来扩展
需要 GitHub repo 配置的 secrets:
- DOCKERHUB_USERNAME = blueink05
- DOCKERHUB_TOKEN = (hub.docker.com/settings/security 拿 access token)
没加之前,DockerHub login 步骤被 actions 自动跳过,只推 GHCR 一份。
Dockerfile / docker-compose.yml / .dockerignore 不动(Dockerfile 1.26-alpine
base image 已确认存在,tags list 拉到 tip-20260920)。
96 lines
3.6 KiB
YAML
96 lines
3.6 KiB
YAML
name: Docker Publish
|
|
|
|
# 触发条件:
|
|
# - main 上的 semver tag(v*.*.*): 推 GHCR + DockerHub
|
|
# - 手动按钮(workflow_dispatch): 临时跑一次
|
|
#
|
|
# 注意:
|
|
# 1. 需在 GitHub 仓库 Settings → Secrets 加 DOCKERHUB_USERNAME / DOCKERHUB_TOKEN。
|
|
# 没加之前, DockerHub login 步骤被 actions 自动跳过, 只推 GHCR。
|
|
# 2. secrets.DOCKERHUB_USERNAME 用于 metadata-action 渲染 image 名, 不能空 — 留空会
|
|
# 生成 "docker.io//ttshub" 这种非法 image。已加默认值保护(空时禁用 DockerHub image)。
|
|
# 3. 暂不验 PR 编译;PR 合到 main 不会重跑本 workflow, 只在打 tag 时构建。
|
|
on:
|
|
push:
|
|
tags:
|
|
- 'v*.*.*'
|
|
workflow_dispatch:
|
|
|
|
env:
|
|
REGISTRY: ghcr.io
|
|
|
|
jobs:
|
|
build-and-push:
|
|
runs-on: ubuntu-latest
|
|
permissions:
|
|
contents: read
|
|
packages: write
|
|
steps:
|
|
- name: Checkout
|
|
uses: actions/checkout@v4
|
|
with:
|
|
fetch-depth: 0 # 拉完整历史,git describe 能取到 tag
|
|
|
|
- name: Set up QEMU
|
|
uses: docker/setup-qemu-action@v3
|
|
|
|
- name: Set up Docker Buildx
|
|
uses: docker/setup-buildx-action@v3
|
|
|
|
- name: Login to GHCR
|
|
if: github.event_name != 'pull_request'
|
|
uses: docker/login-action@v3
|
|
with:
|
|
registry: ${{ env.REGISTRY }}
|
|
username: ${{ github.actor }}
|
|
password: ${{ secrets.GITHUB_TOKEN }}
|
|
|
|
- name: Login to DockerHub
|
|
if: github.event_name != 'pull_request' && secrets.DOCKERHUB_USERNAME != ''
|
|
uses: docker/login-action@v3
|
|
with:
|
|
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
|
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
|
|
|
- name: Extract version from git
|
|
id: version
|
|
run: |
|
|
echo "version=$(git describe --tags --always --dirty)" >> "$GITHUB_OUTPUT"
|
|
echo "commit=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"
|
|
|
|
- name: Extract Docker metadata
|
|
id: meta
|
|
uses: docker/metadata-action@v5
|
|
with:
|
|
# 多个 image base:metadata-action 会为每个 base 渲染完整 tag 列表。
|
|
# DockerHub image 用 enable 保护:username secret 没设时不渲染,避免非法 image 名。
|
|
images: |
|
|
name=${{ env.REGISTRY }}/${{ github.repository }}
|
|
name=docker.io/${{ secrets.DOCKERHUB_USERNAME }}/ttshub,enable=${{ secrets.DOCKERHUB_USERNAME != '' }}
|
|
tags: |
|
|
type=semver,pattern={{version}}
|
|
type=semver,pattern={{major}}.{{minor}}
|
|
type=raw,value=latest,enable=${{ github.ref_type == 'tag' }}
|
|
type=sha,format=short
|
|
labels: |
|
|
org.opencontainers.image.version=${{ steps.version.outputs.version }}
|
|
org.opencontainers.image.revision=${{ steps.version.outputs.commit }}
|
|
|
|
- name: Build and push
|
|
uses: docker/build-push-action@v6
|
|
with:
|
|
context: .
|
|
platforms: linux/amd64,linux/arm64
|
|
# 当前触发器只有 push (tag) + workflow_dispatch, 无 pull_request, 所以 push 永远为 true。
|
|
# 保留 != 'pull_request' 表达式以兼容未来:若再加 PR 触发, 自动只 build 不 push。
|
|
push: ${{ github.event_name != 'pull_request' }}
|
|
# cache-from: 复用上次构建的层(github actions 内置缓存), 快 5-10x
|
|
# cache-to: 本次构建的层也推回缓存, 下次复用
|
|
cache-from: type=gha
|
|
cache-to: type=gha,mode=max
|
|
tags: ${{ steps.meta.outputs.tags }}
|
|
labels: ${{ steps.meta.outputs.labels }}
|
|
build-args: |
|
|
VERSION=${{ steps.version.outputs.version }}
|
|
COMMIT=${{ steps.version.outputs.commit }}
|