From 3bf7d5a9c99e2654699ecbfb8dae8d9143628849 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Fri, 7 Feb 2025 13:49:34 +0100 Subject: [PATCH] add git hash to binary, print on startup (#2415) * add git hash to binary, print on startup Signed-off-by: Kristoffer Dalby * update changelog Signed-off-by: Kristoffer Dalby --------- Signed-off-by: Kristoffer Dalby --- .goreleaser.yml | 4 +++- CHANGELOG.md | 2 ++ Dockerfile.integration | 2 +- cmd/headscale/cli/root.go | 6 +++--- cmd/headscale/cli/version.go | 8 +++++--- docs/setup/install/source.md | 2 +- flake.nix | 15 +++++++++------ hscontrol/app.go | 2 +- hscontrol/types/version.go | 4 ++++ 9 files changed, 29 insertions(+), 16 deletions(-) create mode 100644 hscontrol/types/version.go diff --git a/.goreleaser.yml b/.goreleaser.yml index 400cd12f..a1cb6ef1 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -27,7 +27,9 @@ builds: flags: - -mod=readonly ldflags: - - -s -w -X github.com/juanfont/headscale/cmd/headscale/cli.Version=v{{.Version}} + - -s -w + - -X github.com/juanfont/headscale/hscontrol/types.Version={{ .Version }} + - -X github.com/juanfont/headscale/hscontrol/types.GitCommitHash={{ .Commit }} tags: - ts2019 diff --git a/CHANGELOG.md b/CHANGELOG.md index 40b7d1e2..bf7ae27b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,8 @@ [#2396](https://github.com/juanfont/headscale/pull/2396) - Rehaul HTTP errors, return better status code and errors to users [#2398](https://github.com/juanfont/headscale/pull/2398) +- Print headscale version and commit on server startup + [#2415](https://github.com/juanfont/headscale/pull/2415) ## 0.24.3 (2025-02-07) diff --git a/Dockerfile.integration b/Dockerfile.integration index 735cdba5..95d07375 100644 --- a/Dockerfile.integration +++ b/Dockerfile.integration @@ -18,7 +18,7 @@ RUN go mod download COPY . . -RUN CGO_ENABLED=0 GOOS=linux go install -ldflags="-s -w -X github.com/juanfont/headscale/cmd/headscale/cli.Version=$VERSION" -a ./cmd/headscale && test -e /go/bin/headscale +RUN CGO_ENABLED=0 GOOS=linux go install -a ./cmd/headscale && test -e /go/bin/headscale # Need to reset the entrypoint or everything will run as a busybox script ENTRYPOINT [] diff --git a/cmd/headscale/cli/root.go b/cmd/headscale/cli/root.go index 7bac79ce..f9c08647 100644 --- a/cmd/headscale/cli/root.go +++ b/cmd/headscale/cli/root.go @@ -66,18 +66,18 @@ func initConfig() { disableUpdateCheck := viper.GetBool("disable_check_updates") if !disableUpdateCheck && !machineOutput { if (runtime.GOOS == "linux" || runtime.GOOS == "darwin") && - Version != "dev" { + types.Version != "dev" { githubTag := &latest.GithubTag{ Owner: "juanfont", Repository: "headscale", } - res, err := latest.Check(githubTag, Version) + res, err := latest.Check(githubTag, types.Version) if err == nil && res.Outdated { //nolint log.Warn().Msgf( "An updated version of Headscale has been found (%s vs. your current %s). Check it out https://github.com/juanfont/headscale/releases\n", res.Current, - Version, + types.Version, ) } } diff --git a/cmd/headscale/cli/version.go b/cmd/headscale/cli/version.go index 2b440af3..b007d05c 100644 --- a/cmd/headscale/cli/version.go +++ b/cmd/headscale/cli/version.go @@ -1,11 +1,10 @@ package cli import ( + "github.com/juanfont/headscale/hscontrol/types" "github.com/spf13/cobra" ) -var Version = "dev" - func init() { rootCmd.AddCommand(versionCmd) } @@ -16,6 +15,9 @@ var versionCmd = &cobra.Command{ Long: "The version of headscale.", Run: func(cmd *cobra.Command, args []string) { output, _ := cmd.Flags().GetString("output") - SuccessOutput(map[string]string{"version": Version}, Version, output) + SuccessOutput(map[string]string{ + "version": types.Version, + "commit": types.GitCommitHash, + }, types.Version, output) }, } diff --git a/docs/setup/install/source.md b/docs/setup/install/source.md index 27074855..eb4f4e43 100644 --- a/docs/setup/install/source.md +++ b/docs/setup/install/source.md @@ -30,7 +30,7 @@ latestTag=$(git describe --tags `git rev-list --tags --max-count=1`) git checkout $latestTag -go build -ldflags="-s -w -X github.com/juanfont/headscale/cmd/headscale/cli.Version=$latestTag" github.com/juanfont/headscale +go build -ldflags="-s -w -X github.com/juanfont/headscale/hscontrol/types.Version=$latestTag" -X github.com/juanfont/headscale/hscontrol/types.GitCommitHash=HASH" github.com/juanfont/headscale # make it executable chmod a+x headscale diff --git a/flake.nix b/flake.nix index 8f114518..ef2f5974 100644 --- a/flake.nix +++ b/flake.nix @@ -12,17 +12,15 @@ flake-utils, ... }: let - headscaleVersion = - if (self ? shortRev) - then self.shortRev - else "dev"; + headscaleVersion = self.shortRev or self.dirtyShortRev; + commitHash = self.rev or self.dirtyRev; in { overlay = _: prev: let pkgs = nixpkgs.legacyPackages.${prev.system}; buildGo = pkgs.buildGo123Module; in { - headscale = buildGo rec { + headscale = buildGo { pname = "headscale"; version = headscaleVersion; src = pkgs.lib.cleanSource self; @@ -36,7 +34,12 @@ subPackages = ["cmd/headscale"]; - ldflags = ["-s" "-w" "-X github.com/juanfont/headscale/cmd/headscale/cli.Version=v${version}"]; + ldflags = [ + "-s" + "-w" + "-X github.com/juanfont/headscale/hscontrol/types.Version=${headscaleVersion}" + "-X github.com/juanfont/headscale/hscontrol/types.GitCommitHash=${commitHash}" + ]; }; protoc-gen-grpc-gateway = buildGo rec { diff --git a/hscontrol/app.go b/hscontrol/app.go index 1d4f3010..5623c76a 100644 --- a/hscontrol/app.go +++ b/hscontrol/app.go @@ -566,8 +566,8 @@ func (h *Headscale) Serve() error { spew.Dump(h.cfg) } + log.Info().Str("version", types.Version).Str("commit", types.GitCommitHash).Msg("Starting Headscale") log.Info(). - Caller(). Str("minimum_version", capver.TailscaleVersion(capver.MinSupportedCapabilityVersion)). Msg("Clients with a lower minimum version will be rejected") diff --git a/hscontrol/types/version.go b/hscontrol/types/version.go new file mode 100644 index 00000000..e84087fb --- /dev/null +++ b/hscontrol/types/version.go @@ -0,0 +1,4 @@ +package types + +var Version = "dev" +var GitCommitHash = "dev"