build/cli: Add BuildDate (#16216)

* build: add BuildDate to version info

will be used in enterprise to compare to license expiration time

* cli: multi-line version output, add BuildDate

before:
$ nomad version
Nomad v1.4.3 (coolfakecommithashomgoshsuchacoolonewoww)

after:
$ nomad version
Nomad v1.5.0-dev
BuildDate 2023-02-17T19:29:26Z
Revision coolfakecommithashomgoshsuchacoolonewoww

compare consul:
$ consul version
Consul v1.14.4
Revision dae670fe
Build Date 2023-01-26T15:47:10Z
Protocol 2 spoken by default, blah blah blah...

and vault:
$ vault version
Vault v1.12.3 (209b3dd99fe8ca320340d08c70cff5f620261f9b), built 2023-02-02T09:07:27Z

* docs: update version command output
This commit is contained in:
Daniel Bennett 2023-02-27 11:27:40 -06:00 committed by GitHub
parent 79844048e6
commit 39e3a1ac3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 46 additions and 8 deletions

3
.changelog/16216.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:improvement
cli: multi-line `nomad version` output, add BuildDate
```

View File

@ -3,10 +3,17 @@ PROJECT_ROOT := $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
THIS_OS := $(shell uname | cut -d- -f1) THIS_OS := $(shell uname | cut -d- -f1)
THIS_ARCH := $(shell uname -m) THIS_ARCH := $(shell uname -m)
GO_MODULE = github.com/hashicorp/nomad
GIT_COMMIT := $(shell git rev-parse HEAD) GIT_COMMIT := $(shell git rev-parse HEAD)
GIT_DIRTY := $(if $(shell git status --porcelain),+CHANGES) GIT_DIRTY := $(if $(shell git status --porcelain),+CHANGES)
GIT_COMMIT_FLAG = $(GO_MODULE)/version.GitCommit=$(GIT_COMMIT)$(GIT_DIRTY)
GO_LDFLAGS := "-X github.com/hashicorp/nomad/version.GitCommit=$(GIT_COMMIT)$(GIT_DIRTY)" # build date is based on most recent commit, in RFC3339 format
BUILD_DATE ?= $(shell TZ=UTC0 git show -s --format=%cd --date=format-local:'%Y-%m-%dT%H:%M:%SZ' HEAD)
BUILD_DATE_FLAG = $(GO_MODULE)/version.BuildDate=$(BUILD_DATE)
GO_LDFLAGS = -X $(GIT_COMMIT_FLAG) -X $(BUILD_DATE_FLAG)
ifneq (MSYS_NT,$(THIS_OS)) ifneq (MSYS_NT,$(THIS_OS))
# GOPATH supports PATH style multi-paths; assume the first entry is favorable. # GOPATH supports PATH style multi-paths; assume the first entry is favorable.
@ -91,7 +98,7 @@ endif
GOOS=$(firstword $(subst _, ,$*)) \ GOOS=$(firstword $(subst _, ,$*)) \
GOARCH=$(lastword $(subst _, ,$*)) \ GOARCH=$(lastword $(subst _, ,$*)) \
CC=$(CC) \ CC=$(CC) \
go build -trimpath -ldflags $(GO_LDFLAGS) -tags "$(GO_TAGS)" -o $(GO_OUT) go build -trimpath -ldflags "$(GO_LDFLAGS)" -tags "$(GO_TAGS)" -o $(GO_OUT)
ifneq (armv7l,$(THIS_ARCH)) ifneq (armv7l,$(THIS_ARCH))
pkg/linux_arm/nomad: CC = arm-linux-gnueabihf-gcc pkg/linux_arm/nomad: CC = arm-linux-gnueabihf-gcc

View File

@ -171,6 +171,7 @@ func convertServerConfig(agentConfig *Config) (*nomad.Config, error) {
conf.EnableDebug = agentConfig.EnableDebug conf.EnableDebug = agentConfig.EnableDebug
conf.Build = agentConfig.Version.VersionNumber() conf.Build = agentConfig.Version.VersionNumber()
conf.BuildDate = agentConfig.Version.BuildDate
conf.Revision = agentConfig.Version.Revision conf.Revision = agentConfig.Version.Revision
if agentConfig.Region != "" { if agentConfig.Region != "" {
conf.Region = agentConfig.Region conf.Region = agentConfig.Region

View File

@ -127,6 +127,9 @@ type Config struct {
// operators track which versions are actively deployed // operators track which versions are actively deployed
Build string Build string
// BuildDate is the time of the git commit used to build the program.
BuildDate time.Time
// Revision is a string that carries the version.GitCommit of Nomad that // Revision is a string that carries the version.GitCommit of Nomad that
// was compiled. // was compiled.
Revision string Revision string

View File

@ -1,6 +1,8 @@
package nomad package nomad
import ( import (
"time"
"github.com/hashicorp/go-hclog" "github.com/hashicorp/go-hclog"
"golang.org/x/exp/slices" "golang.org/x/exp/slices"
) )
@ -8,6 +10,9 @@ import (
// LicenseConfig allows for tunable licensing config // LicenseConfig allows for tunable licensing config
// primarily used for enterprise testing // primarily used for enterprise testing
type LicenseConfig struct { type LicenseConfig struct {
// BuildDate is the time of the git commit used to build the program.
BuildDate time.Time
// LicenseEnvBytes is the license bytes to use for the server's license // LicenseEnvBytes is the license bytes to use for the server's license
LicenseEnvBytes string LicenseEnvBytes string

View File

@ -3,9 +3,14 @@ package version
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"time"
) )
var ( var (
// BuildDate is the time of the git commit used to build the program,
// in RFC3339 format. It is filled in by the compiler via makefile.
BuildDate string
// The git commit that was compiled. This will be filled in by the compiler. // The git commit that was compiled. This will be filled in by the compiler.
GitCommit string GitCommit string
GitDescribe string GitDescribe string
@ -24,6 +29,7 @@ var (
// VersionInfo // VersionInfo
type VersionInfo struct { type VersionInfo struct {
BuildDate time.Time
Revision string Revision string
Version string Version string
VersionPrerelease string VersionPrerelease string
@ -50,7 +56,11 @@ func GetVersion() *VersionInfo {
rel = "dev" rel = "dev"
} }
// on parse error, will be zero value time.Time{}
built, _ := time.Parse(time.RFC3339, BuildDate)
return &VersionInfo{ return &VersionInfo{
BuildDate: built,
Revision: GitCommit, Revision: GitCommit,
Version: ver, Version: ver,
VersionPrerelease: rel, VersionPrerelease: rel,
@ -84,8 +94,12 @@ func (c *VersionInfo) FullVersionNumber(rev bool) string {
fmt.Fprintf(&versionString, "+%s", c.VersionMetadata) fmt.Fprintf(&versionString, "+%s", c.VersionMetadata)
} }
if !c.BuildDate.IsZero() {
fmt.Fprintf(&versionString, "\nBuildDate %s", c.BuildDate.Format(time.RFC3339))
}
if rev && c.Revision != "" { if rev && c.Revision != "" {
fmt.Fprintf(&versionString, " (%s)", c.Revision) fmt.Fprintf(&versionString, "\nRevision %s", c.Revision)
} }
return versionString.String() return versionString.String()

View File

@ -8,7 +8,7 @@ description: |
# Command: version # Command: version
The `version` command displays build information about the running binary, The `version` command displays build information about the running binary,
including the release version and the exact revision. including the release version, build date, and the exact revision.
## Usage ## Usage
@ -18,13 +18,18 @@ nomad version
## Output ## Output
This command prints both the version number as well as the exact commit SHA used This command prints the version number and info about the git commit that was
during the build. The SHA may also have the string `+CHANGES` appended to the used to build the binary. `BuildDate` is when the commit was made,
end, indicating that local, uncommitted changes were detected at build time. and `Revision` is the exact commit SHA.
The SHA may also have the string `+CHANGES` appended to the end,
indicating that local, uncommitted changes were detected at build time.
## Examples ## Examples
```shell-session ```shell-session
$ nomad version $ nomad version
Nomad v0.0.0-615-gcf3c6aa-dev (cf3c6aa8a75a689987b689d75ae2ba73458465cb+CHANGES) Nomad v1.5.0
BuildDate 2023-02-17T19:29:26Z
Revision a536284ebcfb4ff26065955abae446d81cc92b87+CHANGES
``` ```