feat: support getting versionInfo in docker (#1673)

* feat: support getting versionInfo in docker

* fix: fix build

* fix: fix build

* fix: fix system
This commit is contained in:
longxu0509 2023-03-21 20:46:17 +08:00 committed by GitHub
parent b3806070ac
commit da7336a9a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 70 additions and 6 deletions

View File

@ -132,7 +132,7 @@ jobs:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
fetch-depth: -1
- name: Setup Node.js
uses: actions/setup-node@v2
with:
@ -192,6 +192,7 @@ jobs:
uses: docker/build-push-action@v3
if: github.repository == 'casdoor/casdoor' && github.event_name == 'push' && steps.should_push.outputs.push=='true'
with:
context: .
target: STANDARD
platforms: linux/amd64,linux/arm64
push: true
@ -201,6 +202,7 @@ jobs:
uses: docker/build-push-action@v3
if: github.repository == 'casdoor/casdoor' && github.event_name == 'push' && steps.should_push.outputs.push=='true'
with:
context: .
target: ALLINONE
platforms: linux/amd64,linux/arm64
push: true

View File

@ -9,7 +9,7 @@ FROM golang:1.17.5 AS BACK
WORKDIR /go/src/casdoor
COPY . .
RUN ./build.sh
RUN go test -v -run TestGetVersionInfo ./util/system_test.go ./util/system.go > version_info.txt
FROM alpine:latest AS STANDARD
LABEL MAINTAINER="https://casdoor.org/"
@ -34,6 +34,7 @@ WORKDIR /
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/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
ENTRYPOINT ["/server"]
@ -61,6 +62,7 @@ COPY --from=BACK /go/src/casdoor/server_${BUILDX_ARCH} ./server
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/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
ENTRYPOINT ["/bin/bash"]

View File

@ -48,9 +48,14 @@ func (c *ApiController) GetSystemInfo() {
// @router /get-version-info [get]
func (c *ApiController) GetVersionInfo() {
versionInfo, err := util.GetVersionInfo()
if err != nil {
c.ResponseError(err.Error())
return
if versionInfo.Version == "" {
versionInfo, err = util.GetVersionInfoFromFile()
if err != nil {
c.ResponseError(err.Error())
return
}
}
c.ResponseOk(versionInfo)

View File

@ -15,8 +15,13 @@
package util
import (
"bufio"
"os"
"path"
"regexp"
"runtime"
"strconv"
"strings"
"time"
"github.com/go-git/go-git/v5"
@ -140,3 +145,47 @@ func GetVersionInfo() (*VersionInfo, error) {
}
return res, nil
}
func GetVersionInfoFromFile() (*VersionInfo, error) {
res := &VersionInfo{
Version: "",
CommitId: "",
CommitOffset: -1,
}
_, filename, _, _ := runtime.Caller(0)
rootPath := path.Dir(path.Dir(filename))
file, err := os.Open(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

@ -40,7 +40,7 @@ func TestGetMemoryUsage(t *testing.T) {
t.Log(used, total)
}
func TestGetGitRepoVersion(t *testing.T) {
func TestGetVersionInfo(t *testing.T) {
versionInfo, err := GetVersionInfo()
assert.Nil(t, err)
t.Log(versionInfo)
@ -90,3 +90,9 @@ func TestGetVersion(t *testing.T) {
assert.Equal(t, 3, aheadCnt)
assert.Equal(t, "v1.257.0", releaseVersion)
}
func TestFromFile(t *testing.T) {
versionInfo, err := GetVersionInfoFromFile()
assert.Nil(t, err)
t.Log(versionInfo)
}