Skip to content

Commit

Permalink
feat: add commit count flag in pre-release version (#2870)
Browse files Browse the repository at this point in the history
* feat: support commit count in pre-release version

* unexport version strings, correct semver

and shortern makefile lines

* add alpha tag immediately after release commit

Co-authored-by: Benjamin Elder <[email protected]>
  • Loading branch information
zaunist and BenTheElder authored Sep 1, 2022
1 parent c6b929b commit 9ad1341
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ REPO_ROOT:=${CURDIR}
OUT_DIR=$(REPO_ROOT)/bin
# record the source commit in the binary, overridable
COMMIT?=$(shell git rev-parse HEAD 2>/dev/null)
# count the commits since the last release
COMMIT_COUNT?=$(shell git describe --tags | rev | cut -d- -f2 | rev)
################################################################################
# ========================= Setup Go With Gimme ================================
# go version to use for build etc.
Expand Down Expand Up @@ -54,7 +56,9 @@ KIND_BINARY_NAME?=kind
# - reproducible builds: -trimpath and -ldflags=-buildid=
# - smaller binaries: -w (trim debugger data, but not panics)
# - metadata: -X=... to bake in git commit
KIND_BUILD_FLAGS?=-trimpath -ldflags="-buildid= -w -X=sigs.k8s.io/kind/pkg/cmd/kind/version.GitCommit=$(COMMIT)"
KIND_VERSION_PKG:=sigs.k8s.io/kind/pkg/cmd/kind/version
KIND_BUILD_LD_FLAGS:=-X=$(KIND_VERSION_PKG).gitCommit=$(COMMIT) -X=$(KIND_VERSION_PKG).gitCommitCount=$(COMMIT_COUNT)
KIND_BUILD_FLAGS?=-trimpath -ldflags="-buildid= -w $(KIND_BUILD_LD_FLAGS)"
################################################################################
# ================================= Building ===================================
# standard "make" target -> builds
Expand Down
5 changes: 3 additions & 2 deletions hack/release/create.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ VERSION_FILE="./pkg/cmd/kind/version/version.go"

# update core version in go code to $1 and pre-release version to $2
set_version() {
${SED} -i "s/VersionCore = .*/VersionCore = \"${1}\"/" "${VERSION_FILE}"
${SED} -i "s/VersionPreRelease = .*/VersionPreRelease = \"${2}\"/" "${VERSION_FILE}"
${SED} -i "s/versionCore = .*/versionCore = \"${1}\"/" "${VERSION_FILE}"
${SED} -i "s/versionPreRelease = .*/versionPreRelease = \"${2}\"/" "${VERSION_FILE}"
echo "Updated ${VERSION_FILE} for ${1}"
}

Expand All @@ -73,6 +73,7 @@ make clean && ./hack/release/build/cross.sh
# update to the second version
set_version "${2}" "alpha"
make_commit "v${2}-alpha"
add_tag "v${2}-alpha"

# print follow-up instructions
echo ""
Expand Down
30 changes: 19 additions & 11 deletions pkg/cmd/kind/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,19 @@ import (

// Version returns the kind CLI Semantic Version
func Version() string {
v := VersionCore
v := versionCore
// add pre-release version info if we have it
if VersionPreRelease != "" {
v += "-" + VersionPreRelease
if versionPreRelease != "" {
v += "-" + versionPreRelease
// If gitCommitCount was set, add to the pre-release version
if gitCommitCount != "" {
v += "." + gitCommitCount
}
// if commit was set, add the + <build>
// we only do this for pre-release versions
if GitCommit != "" {
if gitCommit != "" {
// NOTE: use 14 character short hash, like Kubernetes
v += "+" + truncate(GitCommit, 14)
v += "+" + truncate(gitCommit, 14)
}
}
return v
Expand All @@ -49,16 +53,20 @@ func DisplayVersion() string {
return "kind v" + Version() + " " + runtime.Version() + " " + runtime.GOOS + "/" + runtime.GOARCH
}

// VersionCore is the core portion of the kind CLI version per Semantic Versioning 2.0.0
const VersionCore = "0.15.0"
// versionCore is the core portion of the kind CLI version per Semantic Versioning 2.0.0
const versionCore = "0.15.0"

// VersionPreRelease is the pre-release portion of the kind CLI version per
// versionPreRelease is the base pre-release portion of the kind CLI version per
// Semantic Versioning 2.0.0
const VersionPreRelease = "alpha"
const versionPreRelease = "alpha"

// gitCommitCount count the commits since the last release.
// It is injected at build time.
var gitCommitCount = ""

// GitCommit is the commit used to build the kind binary, if available.
// gitCommit is the commit used to build the kind binary, if available.
// It is injected at build time.
var GitCommit = ""
var gitCommit = ""

// NewCommand returns a new cobra.Command for version
func NewCommand(logger log.Logger, streams cmd.IOStreams) *cobra.Command {
Expand Down

0 comments on commit 9ad1341

Please sign in to comment.