feat: support GetVersionInfo() API in released binary (#4860)

This commit is contained in:
DacongDA
2026-01-20 18:05:11 +08:00
committed by GitHub
parent 039c12afa3
commit 2d1ace427e
6 changed files with 59 additions and 68 deletions

View File

@@ -14,6 +14,7 @@ before:
- go mod tidy - go mod tidy
# you may remove this if you don't need go generate # you may remove this if you don't need go generate
#- go generate ./... #- go generate ./...
- go test -v -run TestGetVersionInfo ./util/system_test.go ./util/system.go ./util/variable.go
builds: builds:
- env: - env:

View File

@@ -19,8 +19,8 @@ RUN go mod download
# Copy source files # Copy source files
COPY . . COPY . .
RUN go test -v -run TestGetVersionInfo ./util/system_test.go ./util/system.go ./util/variable.go
RUN ./build.sh RUN ./build.sh
RUN go test -v -run TestGetVersionInfo ./util/system_test.go ./util/system.go > version_info.txt
FROM alpine:latest AS STANDARD FROM alpine:latest AS STANDARD
LABEL MAINTAINER="https://casdoor.org/" LABEL MAINTAINER="https://casdoor.org/"
@@ -46,7 +46,6 @@ WORKDIR /
COPY --from=BACK --chown=$USER:$USER /go/src/casdoor/server_${BUILDX_ARCH} ./server COPY --from=BACK --chown=$USER:$USER /go/src/casdoor/server_${BUILDX_ARCH} ./server
COPY --from=BACK --chown=$USER:$USER /go/src/casdoor/swagger ./swagger COPY --from=BACK --chown=$USER:$USER /go/src/casdoor/swagger ./swagger
COPY --from=BACK --chown=$USER:$USER /go/src/casdoor/conf/app.conf ./conf/app.conf COPY --from=BACK --chown=$USER:$USER /go/src/casdoor/conf/app.conf ./conf/app.conf
COPY --from=BACK --chown=$USER:$USER /go/src/casdoor/version_info.txt ./go/src/casdoor/version_info.txt
COPY --from=FRONT --chown=$USER:$USER /web/build ./web/build COPY --from=FRONT --chown=$USER:$USER /web/build ./web/build
ENTRYPOINT ["/server"] ENTRYPOINT ["/server"]
@@ -74,7 +73,6 @@ COPY --from=BACK /go/src/casdoor/server_${BUILDX_ARCH} ./server
COPY --from=BACK /go/src/casdoor/swagger ./swagger COPY --from=BACK /go/src/casdoor/swagger ./swagger
COPY --from=BACK /go/src/casdoor/docker-entrypoint.sh /docker-entrypoint.sh COPY --from=BACK /go/src/casdoor/docker-entrypoint.sh /docker-entrypoint.sh
COPY --from=BACK /go/src/casdoor/conf/app.conf ./conf/app.conf COPY --from=BACK /go/src/casdoor/conf/app.conf ./conf/app.conf
COPY --from=BACK /go/src/casdoor/version_info.txt ./go/src/casdoor/version_info.txt
COPY --from=FRONT /web/build ./web/build COPY --from=FRONT /web/build ./web/build
ENTRYPOINT ["/bin/bash"] ENTRYPOINT ["/bin/bash"]

View File

@@ -15,7 +15,10 @@
package controllers package controllers
import ( import (
"errors"
"github.com/casdoor/casdoor/util" "github.com/casdoor/casdoor/util"
"github.com/go-git/go-git/v5"
) )
// GetSystemInfo // GetSystemInfo
@@ -46,10 +49,10 @@ func (c *ApiController) GetSystemInfo() {
// @Success 200 {object} util.VersionInfo The Response object // @Success 200 {object} util.VersionInfo The Response object
// @router /get-version-info [get] // @router /get-version-info [get]
func (c *ApiController) GetVersionInfo() { func (c *ApiController) GetVersionInfo() {
errInfo := ""
versionInfo, err := util.GetVersionInfo() versionInfo, err := util.GetVersionInfo()
if err != nil { if err != nil && !errors.Is(err, git.ErrRepositoryNotExists) {
errInfo = "Git error: " + err.Error() c.ResponseError(err.Error())
return
} }
if versionInfo.Version != "" { if versionInfo.Version != "" {
@@ -57,14 +60,7 @@ func (c *ApiController) GetVersionInfo() {
return return
} }
versionInfo, err = util.GetVersionInfoFromFile() c.ResponseOk(util.GetBuiltInVersionInfo())
if err != nil {
errInfo = errInfo + ", File error: " + err.Error()
c.ResponseError(errInfo)
return
}
c.ResponseOk(versionInfo)
} }
// Health // Health

View File

@@ -15,14 +15,9 @@
package util package util
import ( import (
"bufio"
"os" "os"
"path" "path"
"path/filepath"
"regexp"
"runtime" "runtime"
"strconv"
"strings"
"time" "time"
"github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5"
@@ -161,46 +156,10 @@ func GetVersionInfo() (*VersionInfo, error) {
return res, nil return res, nil
} }
func GetVersionInfoFromFile() (*VersionInfo, error) { func GetBuiltInVersionInfo() *VersionInfo {
res := &VersionInfo{ return &VersionInfo{
Version: "", Version: Version,
CommitId: "", CommitId: CommitId,
CommitOffset: -1, CommitOffset: CommitOffset,
} }
_, filename, _, _ := runtime.Caller(0)
rootPath := path.Dir(path.Dir(filename))
file, err := os.Open(filepath.Clean(path.Join(rootPath, "version_info.txt")))
if err != nil {
return res, err
}
defer file.Close()
// Read file contents line by line
scanner := bufio.NewScanner(file)
for scanner.Scan() {
// Use regular expressions to match strings
re := regexp.MustCompile(`\{([^{}]+)\}`)
versionInfo := scanner.Text()
matches := re.FindStringSubmatch(versionInfo)
if len(matches) > 1 {
split := strings.Split(matches[1], " ")
version := split[0]
commitId := split[1]
commitOffset, _ := strconv.Atoi(split[2])
res = &VersionInfo{
Version: version,
CommitId: commitId,
CommitOffset: commitOffset,
}
break
}
}
if err := scanner.Err(); err != nil {
return res, err
}
return res, nil
} }

View File

@@ -17,7 +17,10 @@
package util package util
import ( import (
"fmt"
"os"
"path" "path"
"regexp"
"runtime" "runtime"
"testing" "testing"
@@ -41,8 +44,27 @@ func TestGetMemoryUsage(t *testing.T) {
func TestGetVersionInfo(t *testing.T) { func TestGetVersionInfo(t *testing.T) {
versionInfo, err := GetVersionInfo() versionInfo, err := GetVersionInfo()
assert.Nil(t, err) assert.NoError(t, err)
t.Log(versionInfo) assert.NotNil(t, versionInfo)
_, filename, _, _ := runtime.Caller(0)
variablePath := path.Join(path.Dir(filename), "variable.go")
content, err := os.ReadFile(variablePath)
assert.NoError(t, err)
replacements := map[*regexp.Regexp]string{
regexp.MustCompile(`Version\s*=\s*".*"`): fmt.Sprintf(`Version = "%s"`, versionInfo.Version),
regexp.MustCompile(`CommitId\s*=\s*".*"`): fmt.Sprintf(`CommitId = "%s"`, versionInfo.CommitId),
regexp.MustCompile(`CommitOffset\s*=\s*-?\d+`): fmt.Sprintf(`CommitOffset = %d`, versionInfo.CommitOffset),
}
updated := string(content)
for re, val := range replacements {
updated = re.ReplaceAllString(updated, val)
}
err = os.WriteFile(variablePath, []byte(updated), 0o644)
assert.NoError(t, err)
} }
func TestGetVersion(t *testing.T) { func TestGetVersion(t *testing.T) {
@@ -92,9 +114,3 @@ func TestGetVersion(t *testing.T) {
assert.Equal(t, 3, aheadCnt) assert.Equal(t, 3, aheadCnt)
assert.Equal(t, "v1.257.0", releaseVersion) assert.Equal(t, "v1.257.0", releaseVersion)
} }
func TestFromFile(t *testing.T) {
versionInfo, err := GetVersionInfoFromFile()
assert.Nil(t, err)
t.Log(versionInfo)
}

21
util/variable.go Normal file
View File

@@ -0,0 +1,21 @@
// Copyright 2026 The Casdoor Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package util
var (
Version = "dev"
CommitId = "unknown"
CommitOffset = 0
)