Skip to content

Commit bbdc420

Browse files
committed
```
feat(ci): 在GitHub工作流中添加构建时间信息 - 在beta、release和test三个工作流中添加build_time变量记录构建时间 - 修改Go编译命令,通过ldflags将构建时间注入到二进制文件中 - 更新main.go文件初始化时设置版本和构建时间信息 - 在pkg/flags包中添加版本和构建时间的相关函数 - 输出日志时显示构建时间信息 ```
1 parent d6bddf7 commit bbdc420

5 files changed

Lines changed: 45 additions & 9 deletions

File tree

.github/workflows/beta.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,19 @@ jobs:
2828
echo "v_xlp=${v_xlp}" >> $GITHUB_OUTPUT
2929
echo "v_xlp: ${v_xlp}"
3030
31+
build_time=$(date -Is)
32+
echo "build_time=${build_time}" >> $GITHUB_OUTPUT
33+
echo "build_time: ${build_time}"
34+
3135
- name: 配置环境
3236
uses: actions/setup-go@v6
3337
with:
3438
go-version: ">=1.25.0"
3539

3640
- name: 编译程序
3741
run: |
38-
GOARCH=amd64 go build -ldflags="-s -w" -trimpath -o ./artifacts/xlp-amd64 ./cmd/xlp
39-
GOARCH=arm64 go build -ldflags="-s -w" -trimpath -o ./artifacts/xlp-arm64 ./cmd/xlp
42+
GOARCH=amd64 go build -ldflags="-s -w -X main.BuildTime=${{ steps.version.outputs.build_time }}" -trimpath -o ./artifacts/xlp-amd64 ./cmd/xlp
43+
GOARCH=arm64 go build -ldflags="-s -w -X main.BuildTime=${{ steps.version.outputs.build_time }}" -trimpath -o ./artifacts/xlp-arm64 ./cmd/xlp
4044
ls -lh ./artifacts
4145
env:
4246
CGO_ENABLED: 0

.github/workflows/release.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ jobs:
2828
echo "v_xlp=${v_xlp}" >> $GITHUB_OUTPUT
2929
echo "v_xlp: ${v_xlp}"
3030
31+
build_time=$(date -Is)
32+
echo "build_time=${build_time}" >> $GITHUB_OUTPUT
33+
echo "build_time: ${build_time}"
34+
3135
- name: 设置镜像标签
3236
id: docker_meta
3337
uses: docker/metadata-action@v5
@@ -50,8 +54,8 @@ jobs:
5054

5155
- name: 编译程序
5256
run: |
53-
GOARCH=amd64 go build -ldflags="-s -w" -trimpath -o ./artifacts/xlp-amd64 ./cmd/xlp
54-
GOARCH=arm64 go build -ldflags="-s -w" -trimpath -o ./artifacts/xlp-arm64 ./cmd/xlp
57+
GOARCH=amd64 go build -ldflags="-s -w -X main.BuildTime=${{ steps.version.outputs.build_time }}" -trimpath -o ./artifacts/xlp-amd64 ./cmd/xlp
58+
GOARCH=arm64 go build -ldflags="-s -w -X main.BuildTime=${{ steps.version.outputs.build_time }}" -trimpath -o ./artifacts/xlp-arm64 ./cmd/xlp
5559
ls -lh ./artifacts
5660
env:
5761
CGO_ENABLED: 0

.github/workflows/test.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,19 @@ jobs:
4040
echo "v_date: ${v_date}"
4141
echo "v_test: ${v_test}"
4242
43+
build_time=$(date -Is)
44+
echo "build_time=${build_time}" >> $GITHUB_OUTPUT
45+
echo "build_time: ${build_time}"
46+
4347
- name: 配置环境
4448
uses: actions/setup-go@v6
4549
with:
4650
go-version: "stable"
4751

4852
- name: 编译程序
4953
run: |
50-
GOARCH=amd64 go build -ldflags="-s -w" -trimpath -o ./artifacts/xlp-amd64 ./cmd/xlp
51-
GOARCH=arm64 go build -ldflags="-s -w" -trimpath -o ./artifacts/xlp-arm64 ./cmd/xlp
54+
GOARCH=amd64 go build -ldflags="-s -w -X main.BuildTime=${{ steps.version.outputs.build_time }}" -trimpath -o ./artifacts/xlp-amd64 ./cmd/xlp
55+
GOARCH=arm64 go build -ldflags="-s -w -X main.BuildTime=${{ steps.version.outputs.build_time }}" -trimpath -o ./artifacts/xlp-arm64 ./cmd/xlp
5256
ls -lh ./artifacts
5357
env:
5458
CGO_ENABLED: 0

cmd/xlp/main.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,26 @@ import (
77
"os"
88
"os/signal"
99
"syscall"
10+
"time"
1011

1112
"github.com/cnk3x/xunlei"
13+
"github.com/cnk3x/xunlei/pkg/flags"
1214
"github.com/cnk3x/xunlei/pkg/log"
1315
"github.com/cnk3x/xunlei/pkg/utils"
1416
"github.com/cnk3x/xunlei/pkg/vms"
1517
)
1618

19+
var BuildTime string
20+
21+
func init() {
22+
flags.SetVersion(xunlei.Version)
23+
if BuildTime != "" {
24+
if bt, err := time.Parse(time.RFC3339, BuildTime); err == nil {
25+
flags.SetBuildTime(bt)
26+
}
27+
}
28+
}
29+
1730
func main() {
1831
var cfg xunlei.Config
1932
if err := xunlei.ConfigBind(&cfg); err != nil {
@@ -31,6 +44,7 @@ func main() {
3144
slog.InfoContext(ctx, ` \/ | | |\ | | |___ |`)
3245
slog.InfoContext(ctx, `_/\_ |__| | \| |___ |___ |`)
3346
slog.InfoContext(ctx, fmt.Sprintf(`daemon version: %s`, xunlei.Version))
47+
slog.InfoContext(ctx, fmt.Sprintf(`build time: %s`, flags.GetBuildTime().In(time.Local).Format(time.RFC3339)))
3448
slog.InfoContext(ctx, fmt.Sprintf("debug: %t", cfg.Debug))
3549
slog.InfoContext(ctx, fmt.Sprintf("port: %d", cfg.Port))
3650
slog.InfoContext(ctx, fmt.Sprintf("ip: %s", cfg.Ip))

pkg/flags/flags.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ func init() {
1616
pflag.ErrHelp = fmt.Errorf("\nstart with %s [...OPTIONS]", filepath.Base(os.Args[0]))
1717
pflag.Usage = func() {
1818
fmt.Fprint(os.Stderr, filepath.Base(os.Args[0]))
19-
if Version != "" {
20-
fmt.Fprint(os.Stderr, " - version "+Version)
19+
if version != "" {
20+
fmt.Fprintf(os.Stderr, " - version %s", version)
21+
}
22+
if !buildTime.IsZero() {
23+
fmt.Fprintf(os.Stderr, " - build %s", buildTime.In(time.Local).Format(time.RFC3339))
2124
}
2225
fmt.Fprintln(os.Stderr)
2326
fmt.Fprintln(os.Stderr, "wrap system env as synology for xunlei")
@@ -29,9 +32,16 @@ func init() {
2932

3033
var (
3134
CommandLine = pflag.CommandLine
32-
Version string
35+
36+
version string
37+
buildTime time.Time
3338
)
3439

40+
func SetBuildTime(t time.Time) { buildTime = t }
41+
func SetVersion(v string) { version = v }
42+
func GetBuildTime() time.Time { return buildTime }
43+
func GetVersion() string { return version }
44+
3545
type FlagSet = pflag.FlagSet
3646

3747
func Var[T any](v T, name, short, usage string, env ...string) {

0 commit comments

Comments
 (0)