feat: add GitCommit/GitBranch/BuildTime in version info

This commit is contained in:
lilong.129
2025-03-17 20:40:03 +08:00
parent fb3f8a6fca
commit d63e5e0c1a
6 changed files with 40 additions and 39 deletions

View File

@@ -16,7 +16,13 @@ bump: ## bump hrp version, e.g. make bump version=4.0.0
.PHONY: build
build: ## build hrp cli tool
@echo "[info] build hrp cli tool"
@. scripts/build.sh $(tags)
go build -ldflags "\
-s -w \
-X 'github.com/httprunner/httprunner/v5/internal/version.GitCommit=$(shell git rev-parse HEAD)' \
-X 'github.com/httprunner/httprunner/v5/internal/version.GitBranch=$(shell git rev-parse --abbrev-ref HEAD)' \
-X 'github.com/httprunner/httprunner/v5/internal/version.BuildTime=$(shell date "+%y%m%d%H%M")'" \
-o output/hrp ./cmd/cli
./output/hrp -v
.PHONY: install-hooks
install-hooks: ## install git hooks

View File

@@ -51,7 +51,7 @@ Copyright © 2017-present debugtalk. Apache-2.0 License.`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
hrp.InitLogger(logLevel, logJSON)
},
Version: version.VERSION,
Version: version.GetVersionInfo(),
TraverseChildren: true, // parses flags on all parents before executing child command
SilenceUsage: true, // silence usage when an error occurs
}

View File

@@ -1 +1 @@
v5.0.0-beta-2503172016
v5.0.0-beta-2503172040

View File

@@ -1,13 +0,0 @@
package version
import (
_ "embed"
"strings"
)
//go:embed VERSION
var VERSION string
func init() {
VERSION = strings.TrimSpace(VERSION)
}

View File

@@ -0,0 +1,31 @@
package version
import (
"fmt"
"strings"
_ "embed"
)
//go:embed VERSION
var VERSION string
func init() {
VERSION = strings.TrimSpace(VERSION)
}
// 版本信息,在编译时通过 -ldflags 注入
var (
GitCommit = "unknown"
GitBranch = "unknown"
BuildTime = "unknown"
)
func GetVersionInfo() string {
commitID := GitCommit
if len(commitID) > 8 {
commitID = commitID[:8]
}
return fmt.Sprintf("%s (branch=%s, commit=%s, build=%s)",
VERSION, GitBranch, commitID, BuildTime)
}

View File

@@ -1,23 +0,0 @@
#!/bin/bash
# build hrp cli binary for testing
# release will be triggered on github actions, see .github/workflows/release.yml
# Usage:
# $ make build
# or
# $ bash scripts/build.sh
set -e
set -x
# prepare path
mkdir -p "output"
bin_path="output/hrp"
# build
go build -ldflags '-s -w' -o "$bin_path" cmd/cli/main.go
# check output and version
ls -lh "$bin_path"
chmod +x "$bin_path"
./"$bin_path" -v