From 9f6d8645301d1c082b507475b9eb8142901abaa0 Mon Sep 17 00:00:00 2001 From: Christophe Fergeau Date: Wed, 15 Nov 2023 12:08:10 +0100 Subject: [PATCH] version: Add versioning for github tarballs When building from github tarballs, getting the version from `git describe` won't work, or worse, will be irrelevant [1] However, we can make use of $Format$ and .gitattributes as described in [2] to automatically substitute the correct version in a go variable when `git archive` is used. In particular, the correct version number will automatically be substituted when GitHub creates releass tarballs. [1] rpms/gvisor-tap-vsock.spec uses `%autosetup -Sgit -n %{name}-%{version}` which unpacks the release tarball in a newly created git repository. [2] https://icinga.com/blog/2022/05/25/embedding-git-commit-information-in-go-binaries/ Signed-off-by: Christophe Fergeau --- .gitattributes | 1 + pkg/types/version.go | 12 +++++++++++- rpm/gvisor-tap-vsock.spec | 4 ++-- 3 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 000000000..d1273eaf9 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +/pkg/types/version.go export-subst diff --git a/pkg/types/version.go b/pkg/types/version.go index 8e2ea6d94..805c36bcd 100644 --- a/pkg/types/version.go +++ b/pkg/types/version.go @@ -4,9 +4,16 @@ import ( "flag" "fmt" "runtime/debug" + "strings" ) -var ModuleVersion = "" +var ( + // set using the '-X github.com/containers/gvisor-tap-vsock/pkg/types.ModuleVersion' linker flag + ModuleVersion = "" + // set through .gitattributes when `git archive` is used + // see https://icinga.com/blog/2022/05/25/embedding-git-commit-information-in-go-binaries/ + gitArchiveVersion = "$Format:%(describe)$" +) type version struct { binaryName string @@ -33,6 +40,9 @@ func (ver *version) ShowVersion() bool { func moduleVersion() string { switch { + // This will be substituted when building from a GitHub tarball + case !strings.HasPrefix(gitArchiveVersion, "$Format:"): + return gitArchiveVersion // This will be set when building from git using make case ModuleVersion != "": return ModuleVersion diff --git a/rpm/gvisor-tap-vsock.spec b/rpm/gvisor-tap-vsock.spec index 2233b11f4..c691daebf 100644 --- a/rpm/gvisor-tap-vsock.spec +++ b/rpm/gvisor-tap-vsock.spec @@ -92,8 +92,8 @@ CGO_CFLAGS=$(echo $CGO_CFLAGS | sed 's/-specs=\/usr\/lib\/rpm\/redhat\/redhat-an export CGO_CFLAGS+=" -m64 -mtune=generic -fcf-protection=full" %endif -# reset LDFLAGS for plugins and gvisor binaries, but ensure gvproxy --version and such is set -LDFLAGS="-X github.com/containers/gvisor-tap-vsock/pkg/types.PackageVersion=%{version}" +# reset LDFLAGS for plugins and gvisor binaries +LDFLAGS='' # build gvisor-tap-vsock binaries %gobuild -o bin/gvproxy ./cmd/gvproxy