Skip to content

Commit

Permalink
Add git commit hash and build date to version
Browse files Browse the repository at this point in the history
This commit adds git commit hash and build date to airshipctl version
command.

Relates-To: #630

Signed-off-by: Sreejith Punnapuzha <Sreejith.Punnapuzha@outlook.com>
Change-Id: Ic27db40cf80153cb5d3af3accacd33e85d36010d
  • Loading branch information
sreejithpunnapuzha committed Sep 21, 2021
1 parent 419300b commit 0620deb
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/release-github.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ jobs:
uses: goreleaser/goreleaser-action@v1
with:
version: latest
commit: currentcommit
date: builddate
args: release --release-notes=release-notes.md --skip-validate --debug
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4 changes: 3 additions & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ builds:
ldflags:
- '-extldflags "-static"'
- -X opendev.org/airship/airshipctl/pkg/version.gitVersion={{ .Version }}
- -X opendev.org/airship/airshipctl/pkg/version.gitCommit={{ .Commit }}
- -X opendev.org/airship/airshipctl/pkg/version.buildDate={{ .Date }}
env:
- CGO_ENABLED=0
id: linux
Expand Down Expand Up @@ -46,4 +48,4 @@ archives:
files:
- none*
release:
draft: true
draft: true
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
SHELL := /bin/bash

GIT_VERSION ?= v0.1.0
GIT_COMMIT ?= $(shell git rev-parse HEAD)
BUILD_DATE ?= $(shell date -u +'%Y-%m-%dT%H:%M:%SZ')
GIT_MODULE ?= opendev.org/airship/airshipctl/pkg/version

LDFLAGS += -X ${GIT_MODULE}.gitVersion=${GIT_VERSION}
LDFLAGS += -X ${GIT_MODULE}.gitCommit=${GIT_COMMIT}
LDFLAGS += -X ${GIT_MODULE}.buildDate=${BUILD_DATE}

GO_FLAGS := -ldflags '-extldflags "-static"' -tags=netgo -trimpath
GO_FLAGS += -ldflags "-X ${GIT_MODULE}.gitVersion=${GIT_VERSION}"
GO_FLAGS += -ldflags '$(LDFLAGS)'
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN 2> /dev/null))
GOBIN = $(shell go env GOPATH 2> /dev/null)/bin
Expand Down
2 changes: 1 addition & 1 deletion cmd/testdata/TestVersionGoldenOutput/version.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
airshipctl: devel
airshipctl: version.Info{GitVersion:"devel", GitCommit:"", BuildDate:""}
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ func NewVersionCommand() *cobra.Command {

func clientVersion() string {
v := version.Get()
return v.GitVersion
return fmt.Sprintf("%#v", v)
}
8 changes: 7 additions & 1 deletion pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,26 @@ package version

var (
gitVersion = "devel"
gitCommit = ""
buildDate = ""
)

// Info structure provides version data for airshipctl
// For now it has GitVersion with format 'v0.1.0'
// provided from Makefile during building airshipctl
// or defined in a local var for development purposes
type Info struct {
GitVersion string `json:"gitVersion"`
GitVersion string `json:"gitVersion,omitempty"`
GitCommit string `json:"gitCommit,omitempty"`
BuildDate string `json:"buildDate,omitempty"`
}

// Get function shows airshipctl version
// returns filled Info structure
func Get() Info {
return Info{
GitVersion: gitVersion,
GitCommit: gitCommit,
BuildDate: buildDate,
}
}

0 comments on commit 0620deb

Please sign in to comment.