feat(ci): 在GitHub工作流中添加构建时间信息

- 在beta、release和test三个工作流中添加build_time变量记录构建时间
- 修改Go编译命令,通过ldflags将构建时间注入到二进制文件中
- 更新main.go文件初始化时设置版本和构建时间信息
- 在pkg/flags包中添加版本和构建时间的相关函数
- 输出日志时显示构建时间信息
```
This commit is contained in:
cnk3x 2026-01-19 22:25:30 +08:00
parent d6bddf75fd
commit bbdc420506
5 changed files with 45 additions and 9 deletions

View File

@ -28,6 +28,10 @@ jobs:
echo "v_xlp=${v_xlp}" >> $GITHUB_OUTPUT
echo "v_xlp: ${v_xlp}"
build_time=$(date -Is)
echo "build_time=${build_time}" >> $GITHUB_OUTPUT
echo "build_time: ${build_time}"
- name: 配置环境
uses: actions/setup-go@v6
with:
@ -35,8 +39,8 @@ jobs:
- name: 编译程序
run: |
GOARCH=amd64 go build -ldflags="-s -w" -trimpath -o ./artifacts/xlp-amd64 ./cmd/xlp
GOARCH=arm64 go build -ldflags="-s -w" -trimpath -o ./artifacts/xlp-arm64 ./cmd/xlp
GOARCH=amd64 go build -ldflags="-s -w -X main.BuildTime=${{ steps.version.outputs.build_time }}" -trimpath -o ./artifacts/xlp-amd64 ./cmd/xlp
GOARCH=arm64 go build -ldflags="-s -w -X main.BuildTime=${{ steps.version.outputs.build_time }}" -trimpath -o ./artifacts/xlp-arm64 ./cmd/xlp
ls -lh ./artifacts
env:
CGO_ENABLED: 0

View File

@ -28,6 +28,10 @@ jobs:
echo "v_xlp=${v_xlp}" >> $GITHUB_OUTPUT
echo "v_xlp: ${v_xlp}"
build_time=$(date -Is)
echo "build_time=${build_time}" >> $GITHUB_OUTPUT
echo "build_time: ${build_time}"
- name: 设置镜像标签
id: docker_meta
uses: docker/metadata-action@v5
@ -50,8 +54,8 @@ jobs:
- name: 编译程序
run: |
GOARCH=amd64 go build -ldflags="-s -w" -trimpath -o ./artifacts/xlp-amd64 ./cmd/xlp
GOARCH=arm64 go build -ldflags="-s -w" -trimpath -o ./artifacts/xlp-arm64 ./cmd/xlp
GOARCH=amd64 go build -ldflags="-s -w -X main.BuildTime=${{ steps.version.outputs.build_time }}" -trimpath -o ./artifacts/xlp-amd64 ./cmd/xlp
GOARCH=arm64 go build -ldflags="-s -w -X main.BuildTime=${{ steps.version.outputs.build_time }}" -trimpath -o ./artifacts/xlp-arm64 ./cmd/xlp
ls -lh ./artifacts
env:
CGO_ENABLED: 0

View File

@ -40,6 +40,10 @@ jobs:
echo "v_date: ${v_date}"
echo "v_test: ${v_test}"
build_time=$(date -Is)
echo "build_time=${build_time}" >> $GITHUB_OUTPUT
echo "build_time: ${build_time}"
- name: 配置环境
uses: actions/setup-go@v6
with:
@ -47,8 +51,8 @@ jobs:
- name: 编译程序
run: |
GOARCH=amd64 go build -ldflags="-s -w" -trimpath -o ./artifacts/xlp-amd64 ./cmd/xlp
GOARCH=arm64 go build -ldflags="-s -w" -trimpath -o ./artifacts/xlp-arm64 ./cmd/xlp
GOARCH=amd64 go build -ldflags="-s -w -X main.BuildTime=${{ steps.version.outputs.build_time }}" -trimpath -o ./artifacts/xlp-amd64 ./cmd/xlp
GOARCH=arm64 go build -ldflags="-s -w -X main.BuildTime=${{ steps.version.outputs.build_time }}" -trimpath -o ./artifacts/xlp-arm64 ./cmd/xlp
ls -lh ./artifacts
env:
CGO_ENABLED: 0

View File

@ -7,13 +7,26 @@ import (
"os"
"os/signal"
"syscall"
"time"
"github.com/cnk3x/xunlei"
"github.com/cnk3x/xunlei/pkg/flags"
"github.com/cnk3x/xunlei/pkg/log"
"github.com/cnk3x/xunlei/pkg/utils"
"github.com/cnk3x/xunlei/pkg/vms"
)
var BuildTime string
func init() {
flags.SetVersion(xunlei.Version)
if BuildTime != "" {
if bt, err := time.Parse(time.RFC3339, BuildTime); err == nil {
flags.SetBuildTime(bt)
}
}
}
func main() {
var cfg xunlei.Config
if err := xunlei.ConfigBind(&cfg); err != nil {
@ -31,6 +44,7 @@ func main() {
slog.InfoContext(ctx, ` \/ | | |\ | | |___ |`)
slog.InfoContext(ctx, `_/\_ |__| | \| |___ |___ |`)
slog.InfoContext(ctx, fmt.Sprintf(`daemon version: %s`, xunlei.Version))
slog.InfoContext(ctx, fmt.Sprintf(`build time: %s`, flags.GetBuildTime().In(time.Local).Format(time.RFC3339)))
slog.InfoContext(ctx, fmt.Sprintf("debug: %t", cfg.Debug))
slog.InfoContext(ctx, fmt.Sprintf("port: %d", cfg.Port))
slog.InfoContext(ctx, fmt.Sprintf("ip: %s", cfg.Ip))

View File

@ -16,8 +16,11 @@ func init() {
pflag.ErrHelp = fmt.Errorf("\nstart with %s [...OPTIONS]", filepath.Base(os.Args[0]))
pflag.Usage = func() {
fmt.Fprint(os.Stderr, filepath.Base(os.Args[0]))
if Version != "" {
fmt.Fprint(os.Stderr, " - version "+Version)
if version != "" {
fmt.Fprintf(os.Stderr, " - version %s", version)
}
if !buildTime.IsZero() {
fmt.Fprintf(os.Stderr, " - build %s", buildTime.In(time.Local).Format(time.RFC3339))
}
fmt.Fprintln(os.Stderr)
fmt.Fprintln(os.Stderr, "wrap system env as synology for xunlei")
@ -29,9 +32,16 @@ func init() {
var (
CommandLine = pflag.CommandLine
Version string
version string
buildTime time.Time
)
func SetBuildTime(t time.Time) { buildTime = t }
func SetVersion(v string) { version = v }
func GetBuildTime() time.Time { return buildTime }
func GetVersion() string { return version }
type FlagSet = pflag.FlagSet
func Var[T any](v T, name, short, usage string, env ...string) {