Enhance versionning on build packages (#165)
Some checks failed
Go-Tests / sonar (push) Has been cancelled
Go-Tests / tests (push) Has been cancelled

It was too challenging to only use "tags" without a prefix to trigger builds. So, the new rule is to prefix releases tags by "releases/"

Reviewed-on: #165
This commit is contained in:
2025-08-22 08:33:32 +00:00
parent d30690a45a
commit 46f41ea01d
7 changed files with 54 additions and 13 deletions

View File

@@ -15,6 +15,7 @@ env:
LICENSE: MIT LICENSE: MIT
DESCRIPTION: "Effortless conversion from compose files (docker, podman) to Helm Charts" DESCRIPTION: "Effortless conversion from compose files (docker, podman) to Helm Charts"
URL: "https://katenary.io" URL: "https://katenary.io"
VERSION: ${{ gitea.ref_name }}
jobs: jobs:
build-packages: build-packages:
@@ -35,7 +36,8 @@ jobs:
steps: steps:
- name: Extract version - name: Extract version
run: | run: |
VERSION="${GITEA_REFNAME#releases/} VERSION="${VERSION#releases/}"
echo "Exporting variable VERSION=$VERSION"
echo "VERSION=$VERSION" >> $GITEA_OUTPUT echo "VERSION=$VERSION" >> $GITEA_OUTPUT
echo "VERSION=$VERSION" >> $GITEA_ENV echo "VERSION=$VERSION" >> $GITEA_ENV
@@ -61,6 +63,7 @@ jobs:
- name: Compile binary - name: Compile binary
if: matrix.goos == 'linux' || matrix.goos == 'freebsd' || matrix.goos == 'darwin' if: matrix.goos == 'linux' || matrix.goos == 'freebsd' || matrix.goos == 'darwin'
run: |- run: |-
echo "Building binary version $VERSION"
mkdir -p dist mkdir -p dist
GOARCH=${{ matrix.goarch }} GOOS=${{ matrix.goos }}\ GOARCH=${{ matrix.goarch }} GOOS=${{ matrix.goos }}\
go build -ldflags="-X 'repo.katenary.io/katenary/katenary/internal/generator.Version=$VERSION'" \ go build -ldflags="-X 'repo.katenary.io/katenary/katenary/internal/generator.Version=$VERSION'" \

View File

@@ -9,6 +9,7 @@ env:
REGISTRY: repo.katenary.io REGISTRY: repo.katenary.io
IMAGE_NAME: ${{ gitea.repository }} IMAGE_NAME: ${{ gitea.repository }}
STORAGE_DRIVER: vfs STORAGE_DRIVER: vfs
VERSION: ${{ gitea.ref_name }}
jobs: jobs:
build: build:
@@ -19,7 +20,8 @@ jobs:
steps: steps:
- name: Extract version - name: Extract version
run: | run: |
VERSION="${GITEA_REFNAME#releases/} VERSION="${VERSION#releases/}"
echo "Exporting variable VERSION=$VERSION"
echo "VERSION=$VERSION" >> $GITEA_OUTPUT echo "VERSION=$VERSION" >> $GITEA_OUTPUT
echo "VERSION=$VERSION" >> $GITEA_ENV echo "VERSION=$VERSION" >> $GITEA_ENV
@@ -42,6 +44,7 @@ jobs:
- name: Build and tag - name: Build and tag
run: |- run: |-
echo "Building image $REGISTRY/${IMAGE_NAME,,}:$VERSION / latest"
buildah build --isolation=chroot --build-arg VERSION=$VERSION -t katenary -f ./oci/katenary/Containerfile . buildah build --isolation=chroot --build-arg VERSION=$VERSION -t katenary -f ./oci/katenary/Containerfile .
buildah tag katenary $REGISTRY/${IMAGE_NAME,,}:$VERSION buildah tag katenary $REGISTRY/${IMAGE_NAME,,}:$VERSION
buildah tag katenary $REGISTRY/${IMAGE_NAME,,}:latest buildah tag katenary $REGISTRY/${IMAGE_NAME,,}:latest

View File

@@ -3,6 +3,7 @@ package generator
import ( import (
"regexp" "regexp"
"runtime/debug" "runtime/debug"
"strings"
) )
// Version is the version of katenary. It is set at compile time. // Version is the version of katenary. It is set at compile time.
@@ -14,8 +15,15 @@ var Version = "master" // changed at compile time
func GetVersion() string { func GetVersion() string {
// try to get the semantic version from the Version variable (theorically set at compile time) // try to get the semantic version from the Version variable (theorically set at compile time)
if reg := regexp.MustCompile(`^v?\d+.\d+.\d+.*|^release-.*`); reg.MatchString(Version) { if reg := regexp.MustCompile(`^v?\d+.\d+.\d+.*|^release-.*`); reg.MatchString(Version) {
Version = strings.Replace(Version, "release-", "v", 1)
if Version[0] != 'v' {
Version = "v" + Version
}
return Version return Version
} }
if reg := regexp.MustCompile(`^releases/.*`); reg.MatchString(Version) {
return strings.Replace(Version, "releases/", "v", 1)
}
// OK... let's try to get the version from the build info // OK... let's try to get the version from the build info
// get the version from the build info (when installed with go install) // get the version from the build info (when installed with go install)

View File

@@ -6,29 +6,53 @@ import (
) )
func TestVersion(t *testing.T) { func TestVersion(t *testing.T) {
expected := "v1.0.0"
errorMessage := func(value, expected any) string {
return "Expected version to be " + expected.(string) + ", got " + value.(string)
}
// first, let's test the default behavior
// when we compile from source, without setting the Version variable,
// by default, when we are developing, the Version variable is set to "master"
// we build on "devel" branch // we build on "devel" branch
v := GetVersion() v := GetVersion()
// by default, the version comes from build info and it's a development version // by default, the version comes from build info and it's a development version
if !strings.Contains(v, "(devel)") { if !strings.Contains(v, "(devel)") {
t.Errorf("Expected version to be set, got %s", v) errorMessage(v, "(devel)")
} }
// now, imagine we are on a release branch // now, imagine we are on a release branch
Version = "1.0.0" Version = "1.0.0"
v = GetVersion() v = GetVersion()
if !strings.Contains(v, "1.0.0") { if !strings.Contains(v, expected) {
t.Errorf("Expected version to be set, got %s", v) errorMessage(v, expected)
} }
// now, imagine we are on v1.0.0 // now, imagine we are on v1.0.0
Version = "v1.0.0" Version = "v1.0.0"
v = GetVersion() v = GetVersion()
if !strings.Contains(v, "v1.0.0") { if !strings.Contains(v, expected) {
t.Errorf("Expected version to be set, got %s", v) errorMessage(v, expected)
} }
// we can also compile a release branch // we can also compile a release branch
Version = "release-1.0.0" Version = "release-1.0.0"
v = GetVersion() v = GetVersion()
if !strings.Contains(v, "release-1.0.0") { if !strings.Contains(v, expected) {
t.Errorf("Expected version to be set, got %s", v) errorMessage(v, expected)
}
// for releases-* tags
Version = "release-1.0.0"
v = GetVersion()
if !strings.Contains(v, expected) {
errorMessage(v, expected)
}
// and for releases/* tags
Version = "releases/1.0.0"
v = GetVersion()
if !strings.Contains(v, expected) {
errorMessage(v, expected)
} }
} }

View File

@@ -1,5 +1,5 @@
## BUILD ## BUILD
GO_BUILD=go build -ldflags="-X 'repo.katenary.io/katenary/katenary/internal/generator.Version=$(VERSION)'" -trimpath -o $(OUTPUT) ./cmd/katenary GO_BUILD=go build -ldflags="-X 'katenary.io/internal/generator.Version=$(VERSION)'" -trimpath -o $(OUTPUT) ./cmd/katenary
# Simply build the binary for the current OS and architecture # Simply build the binary for the current OS and architecture
build: pull katenary build: pull katenary

View File

@@ -45,8 +45,10 @@ builder-oci-image:
@$(CTN) build -t go-builder:$(GOVERSION) ./oci/builder \ @$(CTN) build -t go-builder:$(GOVERSION) ./oci/builder \
--build-arg GOVERSION=$(GOVERSION) 1>/dev/null --build-arg GOVERSION=$(GOVERSION) 1>/dev/null
katenary-oci: katenary-oci:
$(CTN) build -f oci/katenary/Containerfile -t katenary:$(VERSION) \ VERSION=$(VERSION) \
VERSION=$${VERSION#releases/}; \
$(CTN) build -f oci/katenary/Containerfile -t katenary:$$VERSION \
--build-arg GOVERSION=$(GOVERSION) \ --build-arg GOVERSION=$(GOVERSION) \
--build-arg VERSION=$(VERSION) \ --build-arg VERSION=$(VERSION) \
./ ./
$(CTN) tag katenary:$(VERSION) katenary:latest $(CTN) tag katenary:$$VERSION katenary:latest

View File

@@ -13,7 +13,8 @@ COPY internal /go/src/repo.katenary.io/katenary/katenary/internal
WORKDIR /go/src/repo.katenary.io/katenary/katenary WORKDIR /go/src/repo.katenary.io/katenary/katenary
ENV CGO_ENABLED=0 ENV CGO_ENABLED=0
RUN set -xe; \ RUN set -xe; \
go build -ldflags="-X 'repo.katenary.io/katenary/katenary/internal/generator.Version=v${VERSION}'" -trimpath -o katenary ./cmd/katenary export VERSION=${VERSION#releases/}; \
go build -ldflags="-X 'katenary.io/internal/generator.Version=v${VERSION}'" -trimpath -o katenary ./cmd/katenary
FROM scratch FROM scratch