Skip to content

Commit

Permalink
build: use VERSION file for setting version
Browse files Browse the repository at this point in the history
Adopt `consul` repo pattern for setting version to simplify management
and build scripts.
  • Loading branch information
zalimeni committed Jun 7, 2024
1 parent 053215c commit 766c7c1
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 89 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
VERSION = $(shell ./control-plane/build-support/scripts/version.sh version/version.go)
GOLANG_VERSION?=$(shell head -n 1 .go-version)
CONSUL_IMAGE_VERSION = $(shell ./control-plane/build-support/scripts/consul-version.sh charts/consul/values.yaml)
CONSUL_ENTERPRISE_IMAGE_VERSION = $(shell ./control-plane/build-support/scripts/consul-enterprise-version.sh charts/consul/values.yaml)
Expand Down Expand Up @@ -269,7 +268,7 @@ ci.aws-acceptance-test-cleanup: ## Deletes AWS resources left behind after faile

.PHONY: version
version: ## print version
@echo $(VERSION)
@source $(CURDIR)/control-plane/build-support/scripts/functions.sh; parse_version $(CURDIR)

.PHONY: consul-version
consul-version: ## print consul version
Expand Down
79 changes: 19 additions & 60 deletions control-plane/build-support/functions/10-util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,7 @@ function have_gpg_key {

function parse_version {
# Arguments:
# $1 - Path to the top level Consul K8s source
# $2 - boolean value for whether the release version should be parsed from the source
# $3 - boolean whether to use GIT_DESCRIBE and GIT_COMMIT environment variables
# $4 - boolean whether to omit the version part of the version string. (optional)
# $1 - Path to the top level Consul K8s source. Defaults to current directory.
#
# Return:
# 0 - success (will write the version to stdout)
Expand All @@ -108,70 +105,34 @@ function parse_version {
# If the GOTAGS environment variable is present then it is used to determine which
# version file to use for parsing.

local vfile="${1}/version/version.go"
local vfile="${1:-.}/version/VERSION"

# ensure the version file exists
if ! test -f "${vfile}"; then
err "Error - File not found: ${vfile}"
return 1
fi

local include_release="$2"
local use_git_env="$3"
local omit_version="$4"

local git_version=""
local git_commit=""

if test -z "${include_release}"; then
include_release=true
fi

if test -z "${use_git_env}"; then
use_git_env=true
fi

if is_set "${use_git_env}"; then
git_version="${GIT_DESCRIBE}"
git_commit="${GIT_COMMIT}"
fi

# Get the main version out of the source file
version_main=$(awk '$1 == "Version" && $2 == "=" { gsub(/"/, "", $3); print $3 }' <${vfile})
release_main=$(awk '$1 == "VersionPrerelease" && $2 == "=" { gsub(/"/, "", $3); print $3 }' <${vfile})

# try to determine the version if we have build tags
for tag in "$GOTAGS"; do
for vfile in $(find "${1}/version" -name "version_*.go" 2>/dev/null | sort); do
if grep -q "// +build $tag" "${vfile}"; then
version_main=$(awk '$1 == "Version" && $2 == "=" { gsub(/"/, "", $3); print $3 }' <${vfile})
release_main=$(awk '$1 == "VersionPrerelease" && $2 == "=" { gsub(/"/, "", $3); print $3 }' <${vfile})
fi
done
done
version_main=$(cat ${vfile} | awk '{ split($0, arr, "-"); print arr[1]; }')
release_main=$(cat ${vfile} | awk '{ split($0, arr, "-"); print arr[2]; }')

local version="${version_main}"
# override the version from source with the value of the GIT_DESCRIBE env var if present
if test -n "${git_version}"; then
version="${git_version}"
fi

local rel_ver=""
if is_set "${include_release}"; then
# Default to pre-release from the source
rel_ver="${release_main}"

# When no GIT_DESCRIBE env var is present and no release is in the source then we
# are definitely in dev mode
if test -z "${git_version}" -a -z "${rel_ver}" && is_set "${use_git_env}"; then
rel_ver="dev"
fi
# Default to pre-release from the source
local rel_ver="${release_main}"

# Add the release to the version
if test -n "${rel_ver}" -a -n "${git_commit}"; then
rel_ver="${rel_ver} (${git_commit})"
fi
fi
# Add the release to the version
if test -n "${rel_ver}" -a -n "${git_commit}"; then
rel_ver="${rel_ver} (${git_commit})"
fi

if test -n "${rel_ver}"; then
if is_set "${omit_version}"; then
Expand All @@ -191,23 +152,18 @@ function parse_version {
function get_version {
# Arguments:
# $1 - Path to the top level Consul K8s source
# $2 - Whether the release version should be parsed from source (optional)
# $3 - Whether to use GIT_DESCRIBE and GIT_COMMIT environment variables
#
# Returns:
# 0 - success (the version is also echoed to stdout)
# 1 - error
#
# Notes:
# If a VERSION environment variable is present it will override any parsing of the version from the source
# In addition to processing the main version.go, version_*.go files will be processed if they have
# a Go build tag that matches the one in the GOTAGS environment variable. This tag processing is
# primitive though and will not match complex build tags in the files with negation etc.
# If a VERSION environment variable is present it will override any parsing of the version from the source.

local vers="$VERSION"
if test -z "$vers"; then
# parse the OSS version from version.go
vers="$(parse_version ${1} ${2} ${3})"
vers="$(parse_version ${1})"
fi

if test -z "$vers"; then
Expand Down Expand Up @@ -574,7 +530,7 @@ function update_version {
# * - error

if ! test -f "$1"; then
err "ERROR: '$1' is not a regular file. update_version must be called with the path to a go version file"
err "ERROR: '$1' is not a regular file. update_version must be called with the path to a version file"
return 1
fi

Expand All @@ -586,8 +542,11 @@ function update_version {
local vfile="$1"
local version="$2"
local prerelease="$3"
if ! test -z "$prerelease"; then
version="${version}-${prerelease}"
fi

sed_i ${SED_EXT} -e "s/(Version[[:space:]]*=[[:space:]]*)\"[^\"]*\"/\1\"${version}\"/g" -e "s/(VersionPrerelease[[:space:]]*=[[:space:]]*)\"[^\"]*\"/\1\"${prerelease}\"/g" "${vfile}"
echo -n "${version}" > "${vfile}"
return $?
}

Expand Down Expand Up @@ -710,8 +669,8 @@ function set_version {
local consul_vers="$6"
local consul_dataplane_vers="$8"

status_stage "==> Updating version/version.go with version info: ${vers} "$4""
if ! update_version "${sdir}/version/version.go" "${vers}" "$4"; then
status_stage "==> Updating version/VERSION: ${vers} "$4""
if ! update_version "${sdir}/version/VERSION" "${vers}" "$4"; then
return 1
fi

Expand Down
4 changes: 2 additions & 2 deletions control-plane/build-support/functions/40-publish.sh
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ function verify_release_build {

local sdir="$1"

local vers="$(get_version ${sdir} true false)"
local vers="$(get_version ${sdir})"
if test -n "$2"
then
vers="$2"
Expand Down Expand Up @@ -265,7 +265,7 @@ function publish_release {
pub_hc_releases=1
fi

local vers="$(get_version ${sdir} true false)"
local vers="$(get_version ${sdir})"
if test $? -ne 0
then
err "Please specify a version (couldn't parse one from the source)."
Expand Down
14 changes: 0 additions & 14 deletions control-plane/build-support/scripts/version.sh

This file was deleted.

1 change: 1 addition & 0 deletions version/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.6.0-dev
27 changes: 16 additions & 11 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,30 @@
package version

import (
_ "embed"
"fmt"
"strings"
)

var (
// The git commit that was compiled. These will be filled in by the compiler.
GitCommit string
// GitCommit is the git commit that was compiled.
// This will be filled in by the compiler.
GitCommit string
// GitDescribe is a bit of a misnomer. It's really the product version we set during CI builds,
// which will match the git tag of that release once it's promoted.
// This will be filled in by the compiler.
GitDescribe string

// The main version number that is being run at the moment.
//
// Version must conform to the format expected by
// github.com/hashicorp/go-version for tests to work.
Version = "1.6.0"

// A pre-release marker for the version. If this is "" (empty string)
// The next version number that will be released. This will be updated after every release.
// Version must conform to the format expected by github.com/hashicorp/go-version
// for tests to work.
// A pre-release marker for the version can also be specified (e.g -dev). If this is omitted
// then it means that it is a final release. Otherwise, this is a pre-release
// such as "dev" (in development), "beta", "rc1", etc.
VersionPrerelease = "dev"
//go:embed VERSION
fullVersion string

Version, versionPrerelease, _ = strings.Cut(strings.TrimSpace(fullVersion), "-")
)

// GetHumanVersion composes the parts of the version in a way that's suitable
Expand All @@ -34,7 +39,7 @@ func GetHumanVersion() string {
}
version = fmt.Sprintf("v%s", version)

release := VersionPrerelease
release := versionPrerelease
if GitDescribe == "" && release == "" {
release = "dev"
}
Expand Down

0 comments on commit 766c7c1

Please sign in to comment.