Skip to content

fix: refactor version constants and add os parameter for 3018 fix #68

fix: refactor version constants and add os parameter for 3018 fix

fix: refactor version constants and add os parameter for 3018 fix #68

Workflow file for this run

name: Docker Publish
on:
# Tag push:推送 tag 时自动构建并推送 latest + vX.Y.Z
push:
tags:
- 'v*'
# 手动触发:自动发现当前最新 vX.Y.Z,并与 latest 一起发布(同一构建)
workflow_dispatch:
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }} # 将在步骤中强制转小写
jobs:
# 1) Tag push:推送 tag 时自动构建并推送 latest + vX.Y.Z(同一构建 -> 同 digest)
build-and-push-on-tag:
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
- name: Force IMAGE_NAME lowercase (GHCR requires lowercase)
run: echo "IMAGE_NAME=${GITHUB_REPOSITORY,,}" >> "$GITHUB_ENV"
- uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx (pin BuildKit version)
uses: docker/setup-buildx-action@v3
with:
driver-opts: |
image=moby/buildkit:v0.25.1
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Docker metadata (auto tag from push)
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=raw,value=latest
type=ref,event=tag
- name: Compute VERSION
id: version
run: |
V="${{ steps.meta.outputs.version }}"
if [ -z "$V" ]; then V="${{ github.event.release.tag_name }}"; fi
echo "VALUE=$V" >> "$GITHUB_OUTPUT"
- name: Build and push (tag)
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
VCS_REF=${{ github.sha }}
VERSION=${{ steps.version.outputs.VALUE }}
# 2) 手动触发:自动发现"当前最新的 vX.Y.Z",一次构建推 latest + vX.Y.Z(同 digest)
manual-publish-latest:
if: github.event_name == 'workflow_dispatch'
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # 保证能拿到 tags 历史
- name: Force IMAGE_NAME lowercase (GHCR requires lowercase)
run: echo "IMAGE_NAME=${GITHUB_REPOSITORY,,}" >> "$GITHUB_ENV"
- name: Fetch all tags
run: |
git fetch --tags --force --prune --prune-tags
- name: Resolve latest semver tag (vX.Y.Z)
id: pick
shell: bash
run: |
TAG=$(git tag -l 'v*' --sort=-v:refname | head -n 1)
if [[ -z "$TAG" ]]; then
echo "No tags found matching pattern v*"
exit 1
fi
if [[ ! "$TAG" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "Latest tag is '$TAG' but not pure vX.Y.Z. Adjust logic if you want to allow pre-releases."
exit 1
fi
VER="${TAG#v}"
echo "FULL=$TAG" >> "$GITHUB_OUTPUT"
echo "VER=$VER" >> "$GITHUB_OUTPUT"
echo "Resolved latest tag: $TAG"
- uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx (pin BuildKit version)
uses: docker/setup-buildx-action@v3
with:
driver-opts: |
image=moby/buildkit:v0.25.1
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Docker metadata (manual latest + vX.Y.Z)
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=raw,value=latest
type=raw,value=${{ steps.pick.outputs.FULL }}
- name: Build and push (manual)
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
VCS_REF=${{ github.sha }}
VERSION=${{ steps.pick.outputs.VER }}