diff --git a/.github/workflows/build-app.yml b/.github/workflows/build-app.yml index 93eb9038a..a2a576104 100644 --- a/.github/workflows/build-app.yml +++ b/.github/workflows/build-app.yml @@ -79,7 +79,6 @@ jobs: -v $(pwd)/sysroot:/sysroot \ -v $(pwd):/repo \ -w /repo \ - -e CGO_ENABLED=1 \ --env-file local/.release-env \ ghcr.io/goreleaser/goreleaser-cross:v${GO_VERSION} \ release --release-notes local/release-notes.md ${{ inputs.dry_run && '--skip=publish' || '' }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index ecec0c9fa..98afaac76 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -68,7 +68,10 @@ jobs: changelog=$(git-chglog --config .github/chglog/config.yml --next-tag "${VERSION}" "${VERSION}") echo -e "${changelog}\n" | cat - CHANGELOG.md > temp && mv temp CHANGELOG.md - # modify the version of `package.json` + # modify the version for `internal/config/version.go` + perl -i -pe 's/const Version = ".*?"/const Version = "'${VERSION}'"/g' internal/config/version.go + + # modify the version for `package.json` perl -i -pe 's/"version": ".*?"/"version": "'${VERSION#v}'"/g' ui/artalk/package.json # modify the version in docs diff --git a/.goreleaser.yml b/.goreleaser.yml index f57913d0b..3ea3fe73c 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -36,8 +36,6 @@ builds: binary: "{{.ProjectName}}" main: ./main.go ldflags: &common_ldflags | - -X github.com/artalkjs/artalk/v2/internal/config.Version={{.Version}} - -X github.com/artalkjs/artalk/v2/internal/config.CommitHash={{ .ShortCommit }} -s -w # Linux (arm_64) diff --git a/Makefile b/Makefile index 6aa3cb777..0e16f0f66 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,12 @@ PKG_NAME := github.com/artalkjs/artalk/v2 BIN_NAME := ./bin/artalk -VERSION ?= $(shell git describe --tags --abbrev=0 --match 'v*') -COMMIT_HASH ?= $(shell git rev-parse --short HEAD) HAS_RICHGO := $(shell which richgo) GOTEST ?= $(if $(HAS_RICHGO), richgo test, go test) ARGS ?= server +export CGO_ENABLED := 1 + all: install build install: @@ -17,8 +17,7 @@ run: all build: go build \ - -ldflags "-s -w -X $(PKG_NAME)/internal/config.Version=$(VERSION) \ - -X $(PKG_NAME)/internal/config.CommitHash=$(COMMIT_HASH)" \ + -ldflags "-s -w" \ -o $(BIN_NAME) \ $(PKG_NAME) @@ -26,10 +25,8 @@ build-frontend: ./scripts/build-frontend.sh build-debug: - @echo "Building Artalk $(VERSION) for debugging..." + @echo "Building Artalk for debugging..." @go build \ - -ldflags "-X $(PKG_NAME)/internal/config.Version=$(VERSION) \ - -X $(PKG_NAME)/internal/config.CommitHash=$(COMMIT_HASH)" \ -gcflags "all=-N -l" \ -o $(BIN_NAME) \ $(PKG_NAME) @@ -78,5 +75,6 @@ docker-push: .PHONY: all install run build build-frontend build-debug dev \ test test-coverage test-coverage-html test-frontend-e2e \ - update-i18n update-conf update-conf-docs update-swagger \ + update-i18n update-conf update-conf-docs \ + update-docs-features update-swagger \ docker-build docker-push; diff --git a/cmd/base.go b/cmd/base.go index db8d8c3dc..a3a3a34f5 100644 --- a/cmd/base.go +++ b/cmd/base.go @@ -13,8 +13,6 @@ import ( "github.com/spf13/cobra" ) -var Version = config.Version + `/` + config.CommitHash - var Banner = ` ________ ________ _________ ________ ___ ___ __ |\ __ \|\ __ \|\___ ___\\ __ \|\ \ |\ \|\ \ @@ -24,7 +22,7 @@ var Banner = ` \ \__\ \__\ \__\\ _\ \ \__\ \ \__\ \__\ \_______\ \__\\ \__\ \|__|\|__|\|__|\|__| \|__| \|__|\|__|\|_______|\|__| \|__| -Artalk (` + Version + `) +Artalk (` + config.VersionString() + `) -> A Self-hosted Comment System. -> https://artalk.js.org @@ -52,7 +50,7 @@ func New() *ArtalkCmd { Use: "artalk", Short: "Artalk: A self-hosted comment system", Long: Banner, - Version: Version, + Version: config.VersionString(), Run: func(cmd *cobra.Command, args []string) { fmt.Println(Banner) fmt.Print("-------------------------------\n\n") diff --git a/cmd/version.go b/cmd/version.go index fa786dcf1..9b2b9187d 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -3,6 +3,7 @@ package cmd import ( "fmt" + "github.com/artalkjs/artalk/v2/internal/config" "github.com/spf13/cobra" ) @@ -11,7 +12,7 @@ func NewVersionCommand() *cobra.Command { Use: "version", Short: "Output version information", Run: func(cmd *cobra.Command, args []string) { - fmt.Println("Artalk (" + Version + ")") + fmt.Println("Artalk (" + config.VersionString() + ")") }, Annotations: map[string]string{ BootModeKey: MODE_MINI_BOOT, diff --git a/internal/config/var.go b/internal/config/var.go deleted file mode 100644 index d9dbada60..000000000 --- a/internal/config/var.go +++ /dev/null @@ -1,7 +0,0 @@ -package config - -// 版本信息 -var ( - Version string - CommitHash string -) diff --git a/internal/config/version.go b/internal/config/version.go new file mode 100644 index 000000000..a52d78351 --- /dev/null +++ b/internal/config/version.go @@ -0,0 +1,41 @@ +package config + +import ( + "runtime/debug" + + "github.com/samber/lo" +) + +// The version of Artalk +// +// Which is automatically set by the CI release workflow +const Version = "v2.9.0" + +// The commit hash from which the binary was built (optional) +// +// This value is set by the build script: +// `go build -ldflags "-X 'github.com/artalkjs/artalk/v2/internal/config.CommitHash=$(git rev-parse --short HEAD)'"` +func CommitHash() string { + info, ok := debug.ReadBuildInfo() + if !ok { + return "" + } + setting, found := lo.Find(info.Settings, func(s debug.BuildSetting) bool { return s.Key == "vcs.revision" }) + if !found { + return "" + } + if len(setting.Value) >= 7 { + return setting.Value[:7] + } + return setting.Value +} + +// Get the version string +// (format is "Version/CommitHash" or only "Version" if CommitHash is empty) +func VersionString() string { + var str = Version + if CommitHash() != "" { + str += "/" + CommitHash() + } + return str +} diff --git a/server/common/conf.go b/server/common/conf.go index 8f29f8feb..649d2a4ec 100644 --- a/server/common/conf.go +++ b/server/common/conf.go @@ -23,7 +23,7 @@ func GetApiVersionDataMap() ApiVersionData { return ApiVersionData{ App: "artalk", Version: strings.TrimPrefix(config.Version, "v"), - CommitHash: config.CommitHash, + CommitHash: config.CommitHash(), } } diff --git a/server/handler/version_test.go b/server/handler/version_test.go index fd9240935..bfa5c9b8b 100644 --- a/server/handler/version_test.go +++ b/server/handler/version_test.go @@ -6,7 +6,6 @@ import ( "net/http/httptest" "testing" - "github.com/artalkjs/artalk/v2/internal/config" "github.com/artalkjs/artalk/v2/server/common" "github.com/artalkjs/artalk/v2/server/handler" "github.com/artalkjs/artalk/v2/test" @@ -22,9 +21,6 @@ func TestVersionApi(t *testing.T) { fiberApp := fiber.New() handler.Version(app.App, fiberApp) - config.Version = "v0.0.1" - config.CommitHash = "0000000" - req := httptest.NewRequest("GET", "/version", nil) resp, _ := fiberApp.Test(req) assert.Equal(t, 200, resp.StatusCode) @@ -35,7 +31,6 @@ func TestVersionApi(t *testing.T) { json.Unmarshal(body, &data) assert.Equal(t, "artalk", data.App) - assert.Equal(t, "0.0.1", data.Version) - assert.Equal(t, "0000000", data.CommitHash) + assert.NotEmpty(t, data.Version) }) } diff --git a/ui/artalk-sidebar/src/pages/login.vue b/ui/artalk-sidebar/src/pages/login.vue index 5b08c48c0..7bd05023c 100644 --- a/ui/artalk-sidebar/src/pages/login.vue +++ b/ui/artalk-sidebar/src/pages/login.vue @@ -71,6 +71,10 @@ function selectUser(username: string) { userSelector.value = null login(username) } + +const versionInfo = computed(() => { + return `v${version.value + (buildHash.value ? ' / ' + buildHash.value : '')}` +})