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 release 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 Dec 20, 2023
1 parent b61aa3a commit e13e93c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 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
7 changes: 7 additions & 0 deletions pkg/types/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import (
"flag"
"fmt"
"runtime/debug"
"strings"
)

var (
// set using the '-X github.com/containers/gvisor-tap-vsock/pkg/types.gitVersion' linker flag
gitVersion = ""
// 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 {
Expand Down Expand Up @@ -36,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 gitVersion != "":
return gitVersion
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.gitVersion=%{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 e13e93c

Please sign in to comment.