Skip to content

Commit

Permalink
feat: support getting versionInfo in docker (#1673)
Browse files Browse the repository at this point in the history
* feat: support getting versionInfo in docker

* fix: fix build

* fix: fix build

* fix: fix system
  • Loading branch information
longxu0509 committed Mar 21, 2023
1 parent b380607 commit da7336a
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 6 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/build.yml
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion Dockerfile
Expand Up @@ -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/"
Expand All @@ -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"]
Expand Down Expand Up @@ -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"]
Expand Down
11 changes: 8 additions & 3 deletions controllers/system_info.go
Expand Up @@ -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)
Expand Down
49 changes: 49 additions & 0 deletions util/system.go
Expand Up @@ -15,8 +15,13 @@
package util

import (
"bufio"
"os"
"path"
"regexp"
"runtime"
"strconv"
"strings"
"time"

"github.com/go-git/go-git/v5"
Expand Down Expand Up @@ -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
}
8 changes: 7 additions & 1 deletion util/system_test.go
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}

0 comments on commit da7336a

Please sign in to comment.