Skip to content

Commit 3917b25

Browse files
author
user123
committed
版本注入
1 parent bb61eb5 commit 3917b25

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

.github/workflows/release.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,13 @@ jobs:
6262
- name: 编译二进制文件
6363
run: |
6464
cd src
65+
VERSION=${{ steps.version.outputs.version }}
6566
6667
# Linux AMD64
67-
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o ../build/hubproxy/hubproxy-linux-amd64 .
68+
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w -X main.Version=${VERSION}" -o ../build/hubproxy/hubproxy-linux-amd64 .
6869
6970
# Linux ARM64
70-
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags="-s -w" -o ../build/hubproxy/hubproxy-linux-arm64 .
71+
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags="-s -w -X main.Version=${VERSION}" -o ../build/hubproxy/hubproxy-linux-arm64 .
7172
7273
# 压缩二进制文件
7374
upx -9 ../build/hubproxy/hubproxy-linux-amd64

Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
FROM golang:1.25-alpine AS builder
22

33
ARG TARGETARCH
4+
ARG VERSION=dev
45

56
WORKDIR /app
67
COPY src/go.mod src/go.sum ./
78
RUN go mod download && apk add upx
89

910
COPY src/ .
1011

11-
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${TARGETARCH} go build -ldflags="-s -w" -trimpath -o hubproxy . && upx -9 hubproxy
12+
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${TARGETARCH} go build -ldflags="-s -w -X main.Version=${VERSION}" -trimpath -o hubproxy . && upx -9 hubproxy
1213

1314
FROM alpine
1415

@@ -17,4 +18,4 @@ WORKDIR /root/
1718
COPY --from=builder /app/hubproxy .
1819
COPY --from=builder /app/config.toml .
1920

20-
CMD ["./hubproxy"]
21+
CMD ["./hubproxy"]

src/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ var (
4040
serviceStartTime = time.Now()
4141
)
4242

43+
var Version = "dev"
44+
4345
func buildRouter(cfg *config.AppConfig) *gin.Engine {
4446
gin.SetMode(gin.ReleaseMode)
4547
router := gin.Default()
@@ -148,7 +150,7 @@ func main() {
148150
fmt.Printf("H2c: 已启用\n")
149151
}
150152

151-
fmt.Printf("版本号: v1.2.1\n")
153+
fmt.Printf("版本号: %s\n", Version)
152154
fmt.Printf("项目地址: https://github.com/sky22333/hubproxy\n")
153155

154156
// 创建HTTP2服务器
@@ -205,6 +207,7 @@ func initHealthRoutes(router *gin.Engine) {
205207
c.JSON(http.StatusOK, gin.H{
206208
"ready": true,
207209
"service": "hubproxy",
210+
"version": Version,
208211
"start_time_unix": serviceStartTime.Unix(),
209212
"uptime_sec": uptimeSec,
210213
"uptime_human": uptimeHuman,

0 commit comments

Comments
 (0)