Skip to content

Commit

Permalink
version: Add versioning for github tarballs
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
cfergeau committed Nov 22, 2023
1 parent fce89c8 commit 9f6d864
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/pkg/types/version.go export-subst
12 changes: 11 additions & 1 deletion pkg/types/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions rpm/gvisor-tap-vsock.spec
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9f6d864

Please sign in to comment.