Skip to content

Commit

Permalink
refactor(go): hardcode version and get commit_hash via runtime/debug
Browse files Browse the repository at this point in the history
  • Loading branch information
qwqcode committed Sep 18, 2024
1 parent 53aaf27 commit 6b0e46c
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 32 deletions.
1 change: 0 additions & 1 deletion .github/workflows/build-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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' || '' }}
5 changes: 4 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 6 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -17,19 +17,16 @@ 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)

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)
Expand Down Expand Up @@ -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;
6 changes: 2 additions & 4 deletions cmd/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"github.com/spf13/cobra"
)

var Version = config.Version + `/` + config.CommitHash

var Banner = `
________ ________ _________ ________ ___ ___ __
|\ __ \|\ __ \|\___ ___\\ __ \|\ \ |\ \|\ \
Expand All @@ -24,7 +22,7 @@ var Banner = `
\ \__\ \__\ \__\\ _\ \ \__\ \ \__\ \__\ \_______\ \__\\ \__\
\|__|\|__|\|__|\|__| \|__| \|__|\|__|\|_______|\|__| \|__|
Artalk (` + Version + `)
Artalk (` + config.VersionString() + `)
-> A Self-hosted Comment System.
-> https://artalk.js.org
Expand Down Expand Up @@ -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")
Expand Down
3 changes: 2 additions & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"

"github.com/artalkjs/artalk/v2/internal/config"
"github.com/spf13/cobra"
)

Expand All @@ -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,
Expand Down
7 changes: 0 additions & 7 deletions internal/config/var.go

This file was deleted.

41 changes: 41 additions & 0 deletions internal/config/version.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion server/common/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func GetApiVersionDataMap() ApiVersionData {
return ApiVersionData{
App: "artalk",
Version: strings.TrimPrefix(config.Version, "v"),
CommitHash: config.CommitHash,
CommitHash: config.CommitHash(),
}
}

Expand Down
7 changes: 1 addition & 6 deletions server/handler/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -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)
})
}
6 changes: 5 additions & 1 deletion ui/artalk-sidebar/src/pages/login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ function selectUser(username: string) {
userSelector.value = null
login(username)
}
const versionInfo = computed(() => {
return `v${version.value + (buildHash.value ? ' / ' + buildHash.value : '')}`
})
</script>

<template>
Expand All @@ -92,7 +96,7 @@ function selectUser(username: string) {
<div class="copyright">
Powered by
<a href="https://artalk.js.org" target="_blank">Artalk</a>
(v{{ version }} / {{ buildHash }})
({{ versionInfo }})
</div>

<div v-if="userSelector" class="layer">
Expand Down

0 comments on commit 6b0e46c

Please sign in to comment.