diff --git a/.gitea/workflows/deploy-dev.yaml b/.gitea/workflows/deploy-dev.yaml index caeb5ed..ad2b40b 100644 --- a/.gitea/workflows/deploy-dev.yaml +++ b/.gitea/workflows/deploy-dev.yaml @@ -11,7 +11,11 @@ jobs: uses: actions/checkout@v4 - name: 构建Docker镜像 - run: docker build -t dypid:latest . + run: | + set -x + git_hash=$(git rev-parse --short "$GITHUB_SHA") + build_date=$(TZ=Asia/Shanghai date +"%Y%m%d%H%M") + docker build --build-arg VERSION="dev - $build_date - $git_hash" -t dypid:latest . - name: 导出镜像 run: mkdir release && docker save -o release/dypid.tar dypid:latest && docker rmi dypid:latest diff --git a/.gitea/workflows/deploy-production.yaml b/.gitea/workflows/deploy-production.yaml index d5a674b..461df8e 100644 --- a/.gitea/workflows/deploy-production.yaml +++ b/.gitea/workflows/deploy-production.yaml @@ -12,7 +12,11 @@ jobs: uses: actions/checkout@v4 - name: 构建Docker镜像 - run: docker build -t dypid:latest . + run: | + set -x + git_hash=$(git rev-parse --short "$GITHUB_SHA") + build_date=$(TZ=Asia/Shanghai date +"%Y%m%d%H%M") + docker build --build-arg VERSION="release - $build_date - $git_hash" -t dypid:latest . - name: 导出镜像 run: mkdir release && docker save -o release/dypid.tar dypid:latest && docker rmi dypid:latest diff --git a/Dockerfile b/Dockerfile index 620448c..ddd4647 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,9 +15,13 @@ WORKDIR /build COPY . . COPY --from=webBuilder /build/dist ./web/dist +ARG VERSION + RUN go env -w CGO_ENABLED=0 \ && go mod tidy \ - && go build -o dypid + && go build \ + -ldflags="-s -w -X 'global.Version=$VERSION'" \ + -o dypid FROM alpine diff --git a/api/api.go b/api/api.go index cd71455..52ebca7 100644 --- a/api/api.go +++ b/api/api.go @@ -1,8 +1,10 @@ package api import ( + "dypid/global" "dypid/internal/controller" "embed" + "fmt" "io/fs" "net/http" @@ -15,6 +17,9 @@ func RegRoutes(r *gin.Engine) { g.GET("/test", func(context *gin.Context) { context.String(http.StatusOK, "ok") }) + g.GET("/version", func(context *gin.Context) { + context.String(http.StatusOK, fmt.Sprintf("程序版本: %s\nGin: %s", global.Version, gin.Version)) + }) } { g.GET("/token", controller.ListTokenHandler) //获取token列表 diff --git a/global/global.go b/global/global.go index 61dfd20..64e1860 100644 --- a/global/global.go +++ b/global/global.go @@ -8,3 +8,5 @@ import ( var RDB *redis.Client var RCtx = context.Background() + +var Version = "dev"