Skip to content

Commit fde96bc

Browse files
committed
build: use VERSION file for setting version
Adopt `consul` repo pattern for setting version to simplify management and build scripts.
1 parent 053215c commit fde96bc

File tree

6 files changed

+38
-89
lines changed

6 files changed

+38
-89
lines changed

Makefile

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
VERSION = $(shell ./control-plane/build-support/scripts/version.sh version/version.go)
21
GOLANG_VERSION?=$(shell head -n 1 .go-version)
32
CONSUL_IMAGE_VERSION = $(shell ./control-plane/build-support/scripts/consul-version.sh charts/consul/values.yaml)
43
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
269268

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

274273
.PHONY: consul-version
275274
consul-version: ## print consul version

control-plane/build-support/functions/10-util.sh

+19-60
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,7 @@ function have_gpg_key {
9595

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

111-
local vfile="${1}/version/version.go"
108+
local vfile="${1:-.}/version/VERSION"
112109

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

119-
local include_release="$2"
120-
local use_git_env="$3"
121-
local omit_version="$4"
122-
123116
local git_version=""
124117
local git_commit=""
125118

126-
if test -z "${include_release}"; then
127-
include_release=true
128-
fi
129-
130-
if test -z "${use_git_env}"; then
131-
use_git_env=true
132-
fi
133-
134-
if is_set "${use_git_env}"; then
135-
git_version="${GIT_DESCRIBE}"
136-
git_commit="${GIT_COMMIT}"
137-
fi
138-
139119
# Get the main version out of the source file
140-
version_main=$(awk '$1 == "Version" && $2 == "=" { gsub(/"/, "", $3); print $3 }' <${vfile})
141-
release_main=$(awk '$1 == "VersionPrerelease" && $2 == "=" { gsub(/"/, "", $3); print $3 }' <${vfile})
142-
143-
# try to determine the version if we have build tags
144-
for tag in "$GOTAGS"; do
145-
for vfile in $(find "${1}/version" -name "version_*.go" 2>/dev/null | sort); do
146-
if grep -q "// +build $tag" "${vfile}"; then
147-
version_main=$(awk '$1 == "Version" && $2 == "=" { gsub(/"/, "", $3); print $3 }' <${vfile})
148-
release_main=$(awk '$1 == "VersionPrerelease" && $2 == "=" { gsub(/"/, "", $3); print $3 }' <${vfile})
149-
fi
150-
done
151-
done
120+
version_main=$(cat ${vfile} | awk '{ split($0, arr, "-"); print arr[1]; }')
121+
release_main=$(cat ${vfile} | awk '{ split($0, arr, "-"); print arr[2]; }')
152122

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

159-
local rel_ver=""
160-
if is_set "${include_release}"; then
161-
# Default to pre-release from the source
162-
rel_ver="${release_main}"
163-
164-
# When no GIT_DESCRIBE env var is present and no release is in the source then we
165-
# are definitely in dev mode
166-
if test -z "${git_version}" -a -z "${rel_ver}" && is_set "${use_git_env}"; then
167-
rel_ver="dev"
168-
fi
129+
# Default to pre-release from the source
130+
local rel_ver="${release_main}"
169131

170-
# Add the release to the version
171-
if test -n "${rel_ver}" -a -n "${git_commit}"; then
172-
rel_ver="${rel_ver} (${git_commit})"
173-
fi
174-
fi
132+
# Add the release to the version
133+
if test -n "${rel_ver}" -a -n "${git_commit}"; then
134+
rel_ver="${rel_ver} (${git_commit})"
135+
fi
175136

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

207163
local vers="$VERSION"
208164
if test -z "$vers"; then
209165
# parse the OSS version from version.go
210-
vers="$(parse_version ${1} ${2} ${3})"
166+
vers="$(parse_version ${1})"
211167
fi
212168

213169
if test -z "$vers"; then
@@ -574,7 +530,7 @@ function update_version {
574530
# * - error
575531

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

@@ -586,8 +542,11 @@ function update_version {
586542
local vfile="$1"
587543
local version="$2"
588544
local prerelease="$3"
545+
if ! test -z "$prerelease"; then
546+
version="${version}-${prerelease}"
547+
fi
589548

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

@@ -710,8 +669,8 @@ function set_version {
710669
local consul_vers="$6"
711670
local consul_dataplane_vers="$8"
712671

713-
status_stage "==> Updating version/version.go with version info: ${vers} "$4""
714-
if ! update_version "${sdir}/version/version.go" "${vers}" "$4"; then
672+
status_stage "==> Updating version/VERSION: ${vers} "$4""
673+
if ! update_version "${sdir}/version/VERSION" "${vers}" "$4"; then
715674
return 1
716675
fi
717676

control-plane/build-support/functions/40-publish.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ function verify_release_build {
211211

212212
local sdir="$1"
213213

214-
local vers="$(get_version ${sdir} true false)"
214+
local vers="$(get_version ${sdir})"
215215
if test -n "$2"
216216
then
217217
vers="$2"
@@ -265,7 +265,7 @@ function publish_release {
265265
pub_hc_releases=1
266266
fi
267267

268-
local vers="$(get_version ${sdir} true false)"
268+
local vers="$(get_version ${sdir})"
269269
if test $? -ne 0
270270
then
271271
err "Please specify a version (couldn't parse one from the source)."

control-plane/build-support/scripts/version.sh

-14
This file was deleted.

version/VERSION

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.6.0-dev

version/version.go

+15-11
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,29 @@
44
package version
55

66
import (
7+
_ "embed"
78
"fmt"
89
"strings"
910
)
1011

1112
var (
12-
// The git commit that was compiled. These will be filled in by the compiler.
13-
GitCommit string
13+
// GitCommit is the git commit that was compiled.
14+
// This will be filled in by the compiler.
15+
GitCommit string
16+
// GitDescribe is the most recent reachable tag for the git commit that was compiled.
17+
// This will be filled in by the compiler.
1418
GitDescribe string
1519

16-
// The main version number that is being run at the moment.
17-
//
18-
// Version must conform to the format expected by
19-
// github.com/hashicorp/go-version for tests to work.
20-
Version = "1.6.0"
21-
22-
// A pre-release marker for the version. If this is "" (empty string)
20+
// The next version number that will be released. This will be updated after every release.
21+
// Version must conform to the format expected by github.com/hashicorp/go-version
22+
// for tests to work.
23+
// A pre-release marker for the version can also be specified (e.g -dev). If this is omitted
2324
// then it means that it is a final release. Otherwise, this is a pre-release
2425
// such as "dev" (in development), "beta", "rc1", etc.
25-
VersionPrerelease = "dev"
26+
//go:embed VERSION
27+
fullVersion string
28+
29+
Version, versionPrerelease, _ = strings.Cut(strings.TrimSpace(fullVersion), "-")
2630
)
2731

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

37-
release := VersionPrerelease
41+
release := versionPrerelease
3842
if GitDescribe == "" && release == "" {
3943
release = "dev"
4044
}

0 commit comments

Comments
 (0)