diff --git a/Makefile b/Makefile index 5458bffd53..d0d4534089 100644 --- a/Makefile +++ b/Makefile @@ -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) @@ -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 diff --git a/control-plane/build-support/functions/10-util.sh b/control-plane/build-support/functions/10-util.sh index 3f0a140f9c..5542716644 100644 --- a/control-plane/build-support/functions/10-util.sh +++ b/control-plane/build-support/functions/10-util.sh @@ -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) @@ -108,7 +105,7 @@ 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 @@ -116,39 +113,12 @@ function parse_version { 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 @@ -156,22 +126,13 @@ function parse_version { 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 @@ -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 @@ -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 @@ -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 $? } @@ -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 diff --git a/control-plane/build-support/functions/40-publish.sh b/control-plane/build-support/functions/40-publish.sh index aae9a5f719..8b39ccfd85 100644 --- a/control-plane/build-support/functions/40-publish.sh +++ b/control-plane/build-support/functions/40-publish.sh @@ -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" @@ -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)." diff --git a/control-plane/build-support/scripts/version.sh b/control-plane/build-support/scripts/version.sh deleted file mode 100755 index fce325e03c..0000000000 --- a/control-plane/build-support/scripts/version.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env bash -# Copyright (c) HashiCorp, Inc. -# SPDX-License-Identifier: MPL-2.0 - - -version_file=$1 -version=$(awk '$1 == "Version" && $2 == "=" { gsub(/"/, "", $3); print $3 }' < "${version_file}") -prerelease=$(awk '$1 == "VersionPrerelease" && $2 == "=" { gsub(/"/, "", $3); print $3 }' < "${version_file}") - -if [ -n "$prerelease" ]; then - echo "${version}-${prerelease}" -else - echo "${version}" -fi \ No newline at end of file diff --git a/version/VERSION b/version/VERSION new file mode 100644 index 0000000000..add940fe2b --- /dev/null +++ b/version/VERSION @@ -0,0 +1 @@ +1.6.0-dev \ No newline at end of file diff --git a/version/version.go b/version/version.go index e5d773c66d..ba0ffc0636 100644 --- a/version/version.go +++ b/version/version.go @@ -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 @@ -34,7 +39,7 @@ func GetHumanVersion() string { } version = fmt.Sprintf("v%s", version) - release := VersionPrerelease + release := versionPrerelease if GitDescribe == "" && release == "" { release = "dev" }