Skip to content

Commit f1eee5e

Browse files
committed
fix: 发布构建注入登录页版本号(release 二进制与 Docker 镜像)
目的: 登录页版本号由构建期 ldflags -X main.Version 注入(main.go Version → web.New → VersionDisplay),但 release.yml 与 Dockerfile 的 go build 均未注入,导致 release 产物与 ghcr 镜像的登录页恒显示 dev,无法区分实际发布版本。 主要变更: - release.yml (build-binaries): 新增 Extract version 步骤——tag 触发取 tag 名 去 v 前缀(按 GITHUB_REF_TYPE=tag 判断,避免误用分支名),workflow_dispatch 等分支触发回退 git describe;构建命令追加 -X main.Version - release.yml (build-docker): 同样的版本提取,经 build-args VERSION 传入 Dockerfile - Dockerfile: 新增 ARG VERSION=dev,构建追加 -ldflags "-X main.Version=${VERSION}" - README.md: Docker 手动构建示例补充 --build-arg VERSION 用法说明 影响范围: 仅构建与发布链路(CI 工作流、Dockerfile、文档),不改任何 Go 代码与运行时行为; 本地 go build 不受影响,版本仍回退 dev。下次打 v* tag 发布即生效, 已验证 tag 触发注入 1.2.3、分支触发注入 git describe 结果。
1 parent fe64337 commit f1eee5e

3 files changed

Lines changed: 32 additions & 4 deletions

File tree

‎.github/workflows/release.yml‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,26 @@ jobs:
7878
with:
7979
go-version: '1.22'
8080

81+
# 登录页版本号经 ldflags -X main.Version 注入(见 main.go Version 注释):
82+
# tag 触发时取 tag 名去掉 v 前缀(VersionDisplay 会统一补 v);
83+
# workflow_dispatch 等分支触发时回退 git describe。
84+
- name: Extract version
85+
id: version
86+
run: |
87+
VERSION="$(git describe --tags --always --dirty 2>/dev/null || echo dev)"
88+
if [ "$GITHUB_REF_TYPE" = "tag" ]; then
89+
VERSION="${GITHUB_REF_NAME#v}"
90+
fi
91+
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
92+
8193
- name: Build binary
8294
env:
8395
CGO_ENABLED: '0'
8496
GOOS: ${{ matrix.goos }}
8597
GOARCH: ${{ matrix.goarch }}
8698
run: |
8799
mkdir -p dist
88-
go build -trimpath -ldflags="-s -w" -o dist/qoder2api-${{ matrix.os }}-${{ matrix.arch }}${{ matrix.ext }} .
100+
go build -trimpath -ldflags="-s -w -X main.Version=${{ steps.version.outputs.version }}" -o dist/qoder2api-${{ matrix.os }}-${{ matrix.arch }}${{ matrix.ext }} .
89101
90102
- name: Create archive
91103
run: |
@@ -110,6 +122,16 @@ jobs:
110122
- name: Checkout
111123
uses: actions/checkout@v4
112124

125+
# 与 build-binaries 相同的版本提取:tag 触发取 tag 去 v 前缀,否则回退 git describe。
126+
- name: Extract version
127+
id: version
128+
run: |
129+
VERSION="$(git describe --tags --always --dirty 2>/dev/null || echo dev)"
130+
if [ "$GITHUB_REF_TYPE" = "tag" ]; then
131+
VERSION="${GITHUB_REF_NAME#v}"
132+
fi
133+
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
134+
113135
- name: Set up QEMU
114136
uses: docker/setup-qemu-action@v3
115137

@@ -140,6 +162,8 @@ jobs:
140162
with:
141163
context: .
142164
platforms: linux/amd64,linux/arm64
165+
build-args: |
166+
VERSION=${{ steps.version.outputs.version }}
143167
push: ${{ startsWith(github.ref, 'refs/tags/v') }}
144168
tags: ${{ steps.meta.outputs.tags }}
145169
labels: ${{ steps.meta.outputs.labels }}

‎Dockerfile‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ WORKDIR /build
44
COPY go.mod go.sum ./
55
RUN go mod download
66
COPY . .
7-
RUN CGO_ENABLED=0 go build -o qoder2api .
7+
8+
# 登录页版本号经 ldflags -X main.Version 注入(见 main.go Version 注释)。
9+
# release.yml 的 build-docker 会传 tag 版本;本地 docker build 不传时回退 dev。
10+
ARG VERSION=dev
11+
RUN CGO_ENABLED=0 go build -ldflags="-X main.Version=${VERSION}" -o qoder2api .
812

913
FROM alpine:3.20
1014
RUN apk add --no-cache ca-certificates tzdata

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ go build -o qoder2api .
5757
# 使用 docker-compose
5858
docker-compose up -d
5959

60-
# 或手动构建
61-
docker build -t qoder2api .
60+
# 或手动构建(可传 VERSION 构建参数,登录页将显示对应版本号,缺省为 dev)
61+
docker build --build-arg VERSION=$(git describe --tags --always --dirty 2>/dev/null || echo dev) -t qoder2api .
6262
docker run -d -p 10081:10081 -v qoder2api-data:/app/data -e QODER_DATA_PATH=/app/data/data.json qoder2api
6363
```
6464

0 commit comments

Comments
 (0)