[功能] 添加可选版本输入以支持自定义 Docker 镜像和发布版本

This commit is contained in:
ryan
2026-03-16 12:53:01 +08:00
parent 5e2503ca50
commit 2cdb844010
2 changed files with 75 additions and 22 deletions
+39 -2
View File
@@ -2,6 +2,11 @@ name: Docker image builds
on: on:
workflow_dispatch: workflow_dispatch:
inputs:
version:
description: "Image version/tag to publish, for example v1.0.0-beta"
required: false
type: string
push: push:
tags: ["v*"] tags: ["v*"]
@@ -35,9 +40,25 @@ jobs:
- name: Set image metadata - name: Set image metadata
shell: bash shell: bash
env:
INPUT_VERSION: ${{ github.event.inputs.version }}
run: | run: |
POINTED_TAG="$(git tag --points-at HEAD --list 'v*' | sort -V | tail -n1)"
INPUT_VERSION="${INPUT_VERSION//[[:space:]]/}"
echo "IMAGE=ghcr.io/${GITHUB_REPOSITORY,,}" >> "$GITHUB_ENV" echo "IMAGE=ghcr.io/${GITHUB_REPOSITORY,,}" >> "$GITHUB_ENV"
echo "VERSION=$(git describe --tags)" >> "$GITHUB_ENV" if [[ "${GITHUB_REF}" == refs/tags/* ]]; then
VERSION="${GITHUB_REF_NAME}"
elif [[ -n "$INPUT_VERSION" ]]; then
VERSION="$INPUT_VERSION"
elif [[ -n "$POINTED_TAG" ]]; then
VERSION="$POINTED_TAG"
else
echo "workflow_dispatch requires an explicit version input when HEAD is not tagged" >&2
exit 1
fi
echo "VERSION=$VERSION" >> "$GITHUB_ENV"
- name: Set up Docker Buildx - name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3 uses: docker/setup-buildx-action@v3
@@ -99,9 +120,25 @@ jobs:
- name: Set image metadata - name: Set image metadata
shell: bash shell: bash
env:
INPUT_VERSION: ${{ github.event.inputs.version }}
run: | run: |
POINTED_TAG="$(git tag --points-at HEAD --list 'v*' | sort -V | tail -n1)"
INPUT_VERSION="${INPUT_VERSION//[[:space:]]/}"
echo "IMAGE=ghcr.io/${GITHUB_REPOSITORY,,}" >> "$GITHUB_ENV" echo "IMAGE=ghcr.io/${GITHUB_REPOSITORY,,}" >> "$GITHUB_ENV"
echo "VERSION=$(git describe --tags)" >> "$GITHUB_ENV" if [[ "${GITHUB_REF}" == refs/tags/* ]]; then
VERSION="${GITHUB_REF_NAME}"
elif [[ -n "$INPUT_VERSION" ]]; then
VERSION="$INPUT_VERSION"
elif [[ -n "$POINTED_TAG" ]]; then
VERSION="$POINTED_TAG"
else
echo "workflow_dispatch requires an explicit version input when HEAD is not tagged" >&2
exit 1
fi
echo "VERSION=$VERSION" >> "$GITHUB_ENV"
- name: Download digests - name: Download digests
uses: actions/download-artifact@v4 uses: actions/download-artifact@v4
+21 -5
View File
@@ -4,6 +4,11 @@ permissions:
on: on:
workflow_dispatch: workflow_dispatch:
inputs:
version:
description: "Release version/tag to publish, for example v1.0.0-beta"
required: false
type: string
push: push:
tags: ["v*"] tags: ["v*"]
@@ -22,17 +27,28 @@ jobs:
- name: Resolve version metadata - name: Resolve version metadata
id: version id: version
env:
INPUT_VERSION: ${{ github.event.inputs.version }}
run: | run: |
SHOULD_RUN=true SHOULD_RUN=true
POINTED_TAG="$(git tag --points-at HEAD --list 'v*' | sort -V | tail -n1)" POINTED_TAG="$(git tag --points-at HEAD --list 'v*' | sort -V | tail -n1)"
INPUT_VERSION="${INPUT_VERSION//[[:space:]]/}"
if [[ "${GITHUB_REF}" == refs/heads/main ]] && [[ -n "$POINTED_TAG" ]]; then if [[ "${GITHUB_REF}" == refs/tags/* ]]; then
SHOULD_RUN=true
VERSION="$POINTED_TAG"
elif [[ "${GITHUB_REF}" == refs/tags/* ]]; then
VERSION="${GITHUB_REF_NAME}" VERSION="${GITHUB_REF_NAME}"
elif [[ -n "$INPUT_VERSION" ]]; then
VERSION="$INPUT_VERSION"
elif [[ -n "$POINTED_TAG" ]]; then
VERSION="$POINTED_TAG"
elif [[ "${GITHUB_EVENT_NAME}" == "workflow_dispatch" ]]; then
echo "workflow_dispatch requires an explicit version input when HEAD is not tagged" >&2
exit 1
elif [[ "${GITHUB_REF}" == refs/heads/main ]]; then
echo "main branch release requires the current commit to be tagged" >&2
exit 1
else else
VERSION="$(git describe --tags)" echo "unable to resolve release version from the current ref" >&2
exit 1
fi fi
echo "should_run=$SHOULD_RUN" >> "$GITHUB_OUTPUT" echo "should_run=$SHOULD_RUN" >> "$GITHUB_OUTPUT"