mirror of
https://github.com/casdoor/casdoor.git
synced 2025-05-23 02:35:49 +08:00
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:
parent
b3806070ac
commit
da7336a9a4
4
.github/workflows/build.yml
vendored
4
.github/workflows/build.yml
vendored
@ -132,7 +132,7 @@ jobs:
|
|||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v2
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: -1
|
||||||
- name: Setup Node.js
|
- name: Setup Node.js
|
||||||
uses: actions/setup-node@v2
|
uses: actions/setup-node@v2
|
||||||
with:
|
with:
|
||||||
@ -192,6 +192,7 @@ jobs:
|
|||||||
uses: docker/build-push-action@v3
|
uses: docker/build-push-action@v3
|
||||||
if: github.repository == 'casdoor/casdoor' && github.event_name == 'push' && steps.should_push.outputs.push=='true'
|
if: github.repository == 'casdoor/casdoor' && github.event_name == 'push' && steps.should_push.outputs.push=='true'
|
||||||
with:
|
with:
|
||||||
|
context: .
|
||||||
target: STANDARD
|
target: STANDARD
|
||||||
platforms: linux/amd64,linux/arm64
|
platforms: linux/amd64,linux/arm64
|
||||||
push: true
|
push: true
|
||||||
@ -201,6 +202,7 @@ jobs:
|
|||||||
uses: docker/build-push-action@v3
|
uses: docker/build-push-action@v3
|
||||||
if: github.repository == 'casdoor/casdoor' && github.event_name == 'push' && steps.should_push.outputs.push=='true'
|
if: github.repository == 'casdoor/casdoor' && github.event_name == 'push' && steps.should_push.outputs.push=='true'
|
||||||
with:
|
with:
|
||||||
|
context: .
|
||||||
target: ALLINONE
|
target: ALLINONE
|
||||||
platforms: linux/amd64,linux/arm64
|
platforms: linux/amd64,linux/arm64
|
||||||
push: true
|
push: true
|
||||||
|
@ -9,7 +9,7 @@ FROM golang:1.17.5 AS BACK
|
|||||||
WORKDIR /go/src/casdoor
|
WORKDIR /go/src/casdoor
|
||||||
COPY . .
|
COPY . .
|
||||||
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/"
|
||||||
@ -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/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"]
|
||||||
@ -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/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"]
|
||||||
|
@ -48,9 +48,14 @@ func (c *ApiController) GetSystemInfo() {
|
|||||||
// @router /get-version-info [get]
|
// @router /get-version-info [get]
|
||||||
func (c *ApiController) GetVersionInfo() {
|
func (c *ApiController) GetVersionInfo() {
|
||||||
versionInfo, err := util.GetVersionInfo()
|
versionInfo, err := util.GetVersionInfo()
|
||||||
if err != nil {
|
|
||||||
c.ResponseError(err.Error())
|
if versionInfo.Version == "" {
|
||||||
return
|
versionInfo, err = util.GetVersionInfoFromFile()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
c.ResponseError(err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c.ResponseOk(versionInfo)
|
c.ResponseOk(versionInfo)
|
||||||
|
@ -15,8 +15,13 @@
|
|||||||
package util
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"regexp"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-git/go-git/v5"
|
"github.com/go-git/go-git/v5"
|
||||||
@ -140,3 +145,47 @@ func GetVersionInfo() (*VersionInfo, error) {
|
|||||||
}
|
}
|
||||||
return res, nil
|
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
|
||||||
|
}
|
||||||
|
@ -40,7 +40,7 @@ func TestGetMemoryUsage(t *testing.T) {
|
|||||||
t.Log(used, total)
|
t.Log(used, total)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetGitRepoVersion(t *testing.T) {
|
func TestGetVersionInfo(t *testing.T) {
|
||||||
versionInfo, err := GetVersionInfo()
|
versionInfo, err := GetVersionInfo()
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
t.Log(versionInfo)
|
t.Log(versionInfo)
|
||||||
@ -90,3 +90,9 @@ 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)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user