From b05d00ede9e99571203cc31da82b68458c745408 Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Fri, 5 Sep 2025 22:53:49 +0800 Subject: [PATCH] Add versioning support to build artifacts and log outputs - Introduced `Version` variable, set during build via `-ldflags`, to embed application version. - Updated Dockerfile to accept `APP_VERSION` argument for version injection during build. - Modified `.goreleaser.yml` to pass GitHub release tag as version via `ldflags`. - Added version logging in the application startup. --- .github/workflows/release.yaml | 2 ++ .goreleaser.yml | 2 ++ Dockerfile | 4 +++- cmd/server/main.go | 4 ++++ 4 files changed, 11 insertions(+), 1 deletion(-) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index c0558b4a..49cf9848 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -21,6 +21,8 @@ jobs: with: go-version: '>=1.24.0' cache: true + - name: "Set custom GORELEASER_CURRENT_TAG" + run: echo "GORELEASER_CURRENT_TAG=${{ github.event.inputs.tag }}" >> $GITHUB_ENV - uses: goreleaser/goreleaser-action@v3 with: distribution: goreleaser diff --git a/.goreleaser.yml b/.goreleaser.yml index 4f79944f..79661336 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -9,6 +9,8 @@ builds: - arm64 main: ./cmd/server/ binary: cli-proxy-api + ldflags: + - -X 'main.Version={{.Env.GORELEASER_CURRENT_TAG}}' archives: - id: "cli-proxy-api" format: tar.gz diff --git a/Dockerfile b/Dockerfile index 087ebfec..edd642d9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,7 +8,9 @@ RUN go mod download COPY . . -RUN CGO_ENABLED=0 GOOS=linux go build -o ./CLIProxyAPI ./cmd/server/ +ARG APP_VERSION="dev" + +RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-X 'main.Version=${APP_VERSION}'" -o ./CLIProxyAPI ./cmd/server/ FROM alpine:3.22.0 diff --git a/cmd/server/main.go b/cmd/server/main.go index 27dc88ea..fe106d06 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -17,6 +17,8 @@ import ( log "github.com/sirupsen/logrus" ) +var Version = "dev" + // LogFormatter defines a custom log format for logrus. // This formatter adds timestamp, log level, and source location information // to each log entry for better debugging and monitoring. @@ -58,6 +60,8 @@ func init() { // It parses command-line flags, loads configuration, and starts the appropriate // service based on the provided flags (login, codex-login, or server mode). func main() { + log.Infof("CLIProxyAPI Version: %v", Version) + // Command-line flags to control the application's behavior. var login bool var codexLogin bool