From 38feae6043cf44732f5f076f06986f7d27bfee68 Mon Sep 17 00:00:00 2001 From: 1379 <1379@1379.com> Date: Sat, 18 Mar 2023 16:29:31 +0800 Subject: [PATCH] feat: assign a version number at compile time --- .github/workflows/release.yml | 3 ++- consts/consts.go | 12 +++++++----- event/listener/start.go | 2 -- service/impl/admin.go | 3 +-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index e4818b1..0205082 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -54,6 +54,7 @@ jobs: - name: Show workflow information run: | echo "GOOS: $GOOS, GOARCH: $GOARCH" + echo "BUILD_TIME=$(date --rfc-3339=seconds)" >> $GITHUB_ENV - name: Build uses: crazy-max/ghaction-xgo@v2 @@ -66,7 +67,7 @@ jobs: v: true x: false race: false - ldflags: -s -w + ldflags: -s -w -X 'github.com/go-sonic/sonic/consts.SonicVersion=${{github.ref_name}}' -X 'github.com/go-sonic/sonic/consts.BuildCommit=${{github.sha}}' -X 'github.com/go-sonic/sonic/consts.BuildTime=${{env.BUILD_TIME}}' buildmode: default trimpath: true diff --git a/consts/consts.go b/consts/consts.go index 617433c..58fd491 100644 --- a/consts/consts.go +++ b/consts/consts.go @@ -19,7 +19,6 @@ const ( ) const ( - SonicVersion = "1.0.0" SonicBackupPrefix = "sonic-backup-" SonicDataExportPrefix = "sonic-data-export-" SonicBackupMarkdownPrefix = "sonic-backup-markdown-" @@ -40,7 +39,10 @@ const ( ThemeCustomPostPrefix = "post_" ) -// StartTime 系统启动时间 -var StartTime time.Time - -var DatabaseVersion string +var ( + StartTime time.Time = time.Now() + DatabaseVersion string + SonicVersion = "v1.0.0" + BuildTime string + BuildCommit string +) diff --git a/event/listener/start.go b/event/listener/start.go index f4e1d24..3cf1acf 100644 --- a/event/listener/start.go +++ b/event/listener/start.go @@ -3,7 +3,6 @@ package listener import ( "context" "fmt" - "time" "go.uber.org/zap" "gorm.io/gorm" @@ -37,7 +36,6 @@ func (s *StartListener) HandleEvent(ctx context.Context, startEvent event.Event) if _, ok := startEvent.(*event.StartEvent); !ok { return nil } - consts.StartTime = time.Now() err := s.createOptions() if err != nil { diff --git a/service/impl/admin.go b/service/impl/admin.go index 24a52be..20cf6b2 100644 --- a/service/impl/admin.go +++ b/service/impl/admin.go @@ -8,7 +8,6 @@ import ( "math/rand" "os" "path/filepath" - "runtime" "time" uuid2 "github.com/google/uuid" @@ -184,7 +183,7 @@ func (a *adminServiceImpl) RefreshToken(ctx context.Context, refreshToken string func (a *adminServiceImpl) GetEnvironments(ctx context.Context) *dto.EnvironmentDTO { environments := &dto.EnvironmentDTO{ Database: string(dal.DBType) + " " + consts.DatabaseVersion, - Version: runtime.Version(), + Version: consts.SonicVersion, StartTime: consts.StartTime.UnixMilli(), Mode: util.IfElse(a.Config.Sonic.Mode == "", "production", a.Config.Sonic.Mode).(string), }