Skip to content

Commit

Permalink
build: generate version information at build time
Browse files Browse the repository at this point in the history
auto-generate git commit, build time at build time,
fix go-version hard code.

Signed-off-by: Ace-Tang <[email protected]>
  • Loading branch information
Ace-Tang committed May 18, 2018
1 parent ecbe3b6 commit caf45ec
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 15 deletions.
2 changes: 1 addition & 1 deletion daemon/mgr/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (mgr *SystemManager) Version() (types.SystemVersion, error) {
Arch: runtime.GOARCH,
BuildTime: version.BuildTime,
GitCommit: version.GitCommit,
GoVersion: version.GOVersion,
GoVersion: runtime.Version(),
KernelVersion: kernelVersion,
Os: runtime.GOOS,
Version: version.Version,
Expand Down
9 changes: 8 additions & 1 deletion hack/build
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@ DIR="$( cd "$( dirname "$0" )/.." && pwd )"
cd $DIR/
GOPATH=$BUILDPATH:$BUILDPATH/src/github.com/docker/libnetwork/Godeps/_workspace


# Go parameters
VERSION="0.4.0"
APIVERSION="1.24"
GITCOMMIT=$(git describe --dirty --always --tags 2> /dev/null || true)
BUILDTIME=$(date --rfc-3339 s 2> /dev/null | sed -e 's/ /T/')
PKG=github.com/alibaba/pouch
GOBUILD="go build"
GOLDFLAGS="-X $PKG/version.GitCommit=${GITCOMMIT} -X $PKG/version.Version=${VERSION} -X $PKG/version.ApiVersion=${APIVERSION} -X $PKG/version.BuildTime=$BUILDTIME"

# Binary name of CLI and Daemon
BINARY_NAME=pouchd
Expand All @@ -30,7 +37,7 @@ function server()
{
cd $BUILDPATH/src/github.com/alibaba/pouch
echo "GOOS=linux $GOBUILD -o $BINARY_NAME"
GOOS=linux $GOBUILD -o $BINARY_NAME -tags 'selinux'
GOOS=linux $GOBUILD -ldflags "${GOLDFLAGS}" -o $BINARY_NAME -tags 'selinux'
}

function client()
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func parseFlags(cmd *cobra.Command, flags []string) {
func runDaemon() error {
//user specifies --version or -v, print version and return.
if printVersion {
fmt.Println(version.Version)
fmt.Printf("pouchd version: %s, build: %s, build at: %s\n", version.Version, version.GitCommit, version.BuildTime)
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion test/cli_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (suite *PouchVersionSuite) TestPouchVersion(c *check.C) {
res := command.PouchRun("version").Assert(c, icmd.Success)
kv := versionToKV(res.Combined())

c.Assert(kv["GoVersion"], check.Equals, version.GOVersion)
c.Assert(kv["GoVersion"], check.Equals, runtime.Version())
c.Assert(kv["APIVersion"], check.Equals, version.APIVersion)
c.Assert(kv["Arch"], check.Equals, runtime.GOARCH)
c.Assert(kv["Os"], check.Equals, runtime.GOOS)
Expand Down
22 changes: 11 additions & 11 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package version

// Version represents the version of pouchd.
const Version = "0.4.0"
// Version package values is auto-generated, the following values will be overwrited at build time.
var (
// Version represents the version of pouchd.
Version = "0.4.0"

// BuildTime is the time when this binary of daemon is built
var BuildTime = "unknown"
// BuildTime is the time when pouch binary is built
BuildTime = "unknown"

// APIVersion means the api version daemon serves
var APIVersion = "1.24"
// APIVersion means the api version daemon serves
APIVersion = "1.24"

// GOVersion is the go version to build Pouch
var GOVersion = "go1.9.1"

// GitCommit is the commit id to build Pouch
var GitCommit = "unknown"
// GitCommit is the commit id to build Pouch
GitCommit = "unknown"
)

0 comments on commit caf45ec

Please sign in to comment.