add build time

This commit is contained in:
MaysWind
2021-02-02 23:02:21 +08:00
parent dc612aef44
commit e1d4e6774c
3 changed files with 52 additions and 20 deletions
+3 -1
View File
@@ -6,10 +6,12 @@ RUN docker/backend-build-pre-setup.sh
RUN apk add git gcc g++ libc-dev
RUN VERSION=`grep '"version": ' package.json | awk -F ':' '{print $2}' | tr -d ' ' | tr -d ',' | tr -d '"'` \
&& COMMIT_HASH=$(git rev-parse --short HEAD) \
&& BUILD_UNIXTIME="$(date '+%s')" \
&& VERSION_FLAGS="-X github.com/mayswind/lab/pkg/version.Version=${VERSION} -X github.com/mayswind/lab/pkg/version.CommitHash=${COMMIT_HASH} -X github.com/mayswind/lab/pkg/version.BuildUnixTime=${BUILD_UNIXTIME}" \
&& GOOS=linux \
&& GOARCH=amd64 \
&& CGO_ENABLED=1 \
&& go build -a -v -i -trimpath -ldflags "-w -linkmode external -extldflags '-static' -X main.version=${VERSION} -X main.commitHash=${COMMIT_HASH}" -o lab lab.go
&& go build -a -v -i -trimpath -ldflags "-w -linkmode external -extldflags '-static' ${VERSION_FLAGS}" -o lab lab.go
RUN chmod +x lab
# Build frontend files
+2 -19
View File
@@ -1,23 +1,20 @@
package main
import (
"fmt"
"log"
"os"
"github.com/urfave/cli/v2"
"github.com/mayswind/lab/cmd"
"github.com/mayswind/lab/pkg/version"
)
var version string
var commitHash string
func main() {
app := &cli.App{
Name: "lab",
Usage: "A lightweight account book app hosted by yourself.",
Version: getVersion(),
Version: version.GetFullVersion(),
Commands: []*cli.Command{
cmd.WebServer,
cmd.Database,
@@ -37,17 +34,3 @@ func main() {
log.Fatalf("Failed to run lab app with %s: %v", os.Args, err)
}
}
func getVersion() string {
fullVersion := "Local Build"
if version != "" {
fullVersion = version
}
if commitHash != "" {
fullVersion = fmt.Sprintf("%s (%s)", fullVersion, commitHash)
}
return fullVersion
}
+47
View File
@@ -0,0 +1,47 @@
package version
import (
"fmt"
"strings"
"github.com/mayswind/lab/pkg/utils"
)
var (
// Version holds the version of this execution program
Version string
// CommitHash holds the git commit hash of this execution program's source code
CommitHash string
// BuildUnixTime holds the time when starting building this execution program
BuildUnixTime string
)
func GetFullVersion() string {
fullVersion := "Local Build"
if Version != "" {
fullVersion = Version
}
additionalInfos := make([]string, 0, 2)
if CommitHash != "" {
additionalInfos = append(additionalInfos, "commit " + CommitHash)
}
if BuildUnixTime != "" {
unixTime, err := utils.StringToInt64(BuildUnixTime)
if unixTime > 0 && err == nil {
additionalInfos = append(additionalInfos, "build time " + utils.FormatToLongDateTime(utils.ParseFromUnixTime(unixTime)))
}
}
if len(additionalInfos) > 0 {
fullVersion = fmt.Sprintf("%s (%s)", fullVersion, strings.Join(additionalInfos, ", "))
}
return fullVersion
}