-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement standardized versioning (#44)
* build: normalize version strings Normalize version strings so they are consistently reported as vVERSION, where VERSION is a valid semantic version string. Custom versions that are not valid semvers are returned as is, and empty strings are returned as v0.0.0. * tools/gen-versioned-files: move version file to root as VERSION * docs: update developer docs for managing VERSION file * tools/image-tag: report consistent build information This commit updates tools/image-tag to always report consistent build information, with escape hatches to force the reported version: 1. If RELEASE_TAG is set (which happens via Drone pipelines), then the script emits the value of the RELEASE_TAG environment variable. 2. If a build is being performed against a Git tag, then the script emits the value of that Git tag. 3. Finally, if neither of the above are true, the version from the VERSION file is used, followed by the prerelease being set to `devel` and the short SHA added as build metadata. Provided healthy workflwos where RELEASE_TAG and Git tags are always semantic versions, this guarantees that versions reported by builds are consistent and can be parsed properly.
- Loading branch information
Showing
9 changed files
with
126 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# This file is used to determine the semantic version for the build of Grafana | ||
# Alloy being constructed is. | ||
# | ||
# For builds produced against release branches, the major and minor version | ||
# indicated in this file match the release branch. For example, for the branch | ||
# release/v1.0, this file will report v1.0.0. The patch version indicated by this | ||
# file syncs with the patch release being planned. For example, if a v1.0.1 | ||
# patch release is planned, this file will report v1.0.1 before that release is | ||
# produced. | ||
# | ||
# For builds produced against main branches, the major and minor version | ||
# reported by this file matches the next minor or major version to be released. | ||
# For example, if v1.0.0 was just released, this file (in the main branch) will | ||
# report v1.1.0, the next release planned. | ||
# | ||
# The string in this file MUST be a valid semantic version prefixed with "v", | ||
# without any pre-release or build information. | ||
# | ||
# This file is ignored when building binaries from a Git tag. | ||
# | ||
# Lines starting with "#" and blank lines are ignored. | ||
|
||
v1.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package build | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_normalizeVersion(t *testing.T) { | ||
tt := []struct { | ||
input string | ||
expect string | ||
}{ | ||
{"", "v0.0.0"}, | ||
{"v1.2.3", "v1.2.3"}, | ||
{"1.2.3", "v1.2.3"}, | ||
{"1.2.3+SHA", "v1.2.3+SHA"}, | ||
{"v1.2.3+SHA", "v1.2.3+SHA"}, | ||
{"1.2.3-rc.1", "v1.2.3-rc.1"}, | ||
{"v1.2.3-rc.1", "v1.2.3-rc.1"}, | ||
{"1.2.3-rc.1+SHA", "v1.2.3-rc.1+SHA"}, | ||
{"v1.2.3-rc.1+SHA", "v1.2.3-rc.1+SHA"}, | ||
{"not_semver", "not_semver"}, | ||
} | ||
|
||
for _, tc := range tt { | ||
actual := normalizeVersion(tc.input) | ||
assert.Equal(t, tc.expect, actual, | ||
"Expected %q to normalize to %q, got %q", | ||
tc.input, tc.expect, actual, | ||
) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
#!/bin/sh | ||
AGENT_VERSION=$(cat ./tools/gen-versioned-files/agent-version.txt | tr -d '\n') | ||
ALLOY_VERSION=$(sed -e '/^#/d' -e '/^$/d' VERSION | tr -d '\n') | ||
|
||
if [ -z "$AGENT_VERSION" ]; then | ||
echo "AGENT_VERSION can't be found. Are you running this from the repo root?" | ||
if [ -z "$ALLOY_VERSION" ]; then | ||
echo "ALLOY_VERSION can't be found. Are you running this from the repo root?" | ||
exit 1 | ||
fi | ||
|
||
versionMatcher='^v[0-9]+\.[0-9]+\.[0-9]+(-rc\.[0-9]+)?$' | ||
versionMatcher='^v[0-9]+\.[0-9]+\.[0-9]$' | ||
|
||
if ! echo "$AGENT_VERSION" | grep -Eq "$versionMatcher"; then | ||
echo "AGENT_VERSION env var is not in the correct format. It should be in the format of vX.Y.Z or vX.Y.Z-rc.N" | ||
if ! echo "$ALLOY_VERSION" | grep -Eq "$versionMatcher"; then | ||
echo "ALLOY_VERSION env var is not in the correct format. It should be in the format of vX.Y.Z" | ||
exit 1 | ||
fi | ||
|
||
templates=$(find . -type f -name "*.t" -not -path "./.git/*") | ||
for template in $templates; do | ||
echo "Generating ${template%.t}" | ||
sed -e "s/\$AGENT_VERSION/$AGENT_VERSION/g" < "$template" > "${template%.t}" | ||
sed -e "s/\$ALLOY_VERSION/$ALLOY_VERSION/g" < "$template" > "${template%.t}" | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,34 @@ | ||
#!/usr/bin/env bash | ||
# | ||
# image-tag determines which version to embed into a built image. | ||
# | ||
# It prefers the following in precedence order: | ||
# | ||
# 1. RELEASE_TAG environment variable | ||
# 2. The Git tag of the current commit (if any) | ||
# 3. The version in the VERSION file, suffixed with -devel plus build | ||
# information. | ||
set -o errexit | ||
set -o pipefail | ||
|
||
VERSION=$(sed -e '/^#/d' -e '/^$/d' VERSION | tr -d '\n') | ||
DETECTED_TAG=$(git describe --match 'v*' --exact-match 2>/dev/null || echo -n "") | ||
|
||
if [ ! -z "${RELEASE_TAG}" ]; then | ||
echo ${RELEASE_TAG} | ||
exit 0 | ||
elif [ ! -z "${DETECTED_TAG}" ]; then | ||
echo ${DETECTED_TAG} | ||
exit 0 | ||
fi | ||
|
||
set -o nounset | ||
|
||
WIP=$(git diff --quiet || echo '-WIP') | ||
BRANCH=$(git rev-parse --abbrev-ref HEAD | sed 's#/#-#g') | ||
|
||
# When 7 chars are not enough to be unique, git automatically uses more. | ||
# We are forcing to 7 here, as we are doing for grafana/grafana as well. | ||
SHA=$(git rev-parse --short=7 HEAD | head -c7) | ||
|
||
# If this is a tag, use it, otherwise use <branch>-<hash> | ||
TAG=$(git describe --exact-match 2> /dev/null || echo "${BRANCH}-${SHA}") | ||
echo ${TAG}${WIP} | ||
if [[ -z $(git status -s) ]]; then | ||
# There are no changes; report version as VERSION-devel+SHA. | ||
SHA=$(git rev-parse --short HEAD) | ||
echo ${VERSION}-devel+${SHA} | ||
else | ||
# Git is dirty; tag as VERSION-devel+wip. | ||
echo ${VERSION}-devel+wip | ||
fi |