add git hash to binary, print on startup (#2415)

* add git hash to binary, print on startup

Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>

* update changelog

Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>

---------

Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
This commit is contained in:
Kristoffer Dalby 2025-02-07 13:49:34 +01:00 committed by GitHub
parent 1d65865425
commit 3bf7d5a9c9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 29 additions and 16 deletions

View file

@ -27,7 +27,9 @@ builds:
flags: flags:
- -mod=readonly - -mod=readonly
ldflags: 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: tags:
- ts2019 - ts2019

View file

@ -37,6 +37,8 @@
[#2396](https://github.com/juanfont/headscale/pull/2396) [#2396](https://github.com/juanfont/headscale/pull/2396)
- Rehaul HTTP errors, return better status code and errors to users - Rehaul HTTP errors, return better status code and errors to users
[#2398](https://github.com/juanfont/headscale/pull/2398) [#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) ## 0.24.3 (2025-02-07)

View file

@ -18,7 +18,7 @@ RUN go mod download
COPY . . 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 # Need to reset the entrypoint or everything will run as a busybox script
ENTRYPOINT [] ENTRYPOINT []

View file

@ -66,18 +66,18 @@ func initConfig() {
disableUpdateCheck := viper.GetBool("disable_check_updates") disableUpdateCheck := viper.GetBool("disable_check_updates")
if !disableUpdateCheck && !machineOutput { if !disableUpdateCheck && !machineOutput {
if (runtime.GOOS == "linux" || runtime.GOOS == "darwin") && if (runtime.GOOS == "linux" || runtime.GOOS == "darwin") &&
Version != "dev" { types.Version != "dev" {
githubTag := &latest.GithubTag{ githubTag := &latest.GithubTag{
Owner: "juanfont", Owner: "juanfont",
Repository: "headscale", Repository: "headscale",
} }
res, err := latest.Check(githubTag, Version) res, err := latest.Check(githubTag, types.Version)
if err == nil && res.Outdated { if err == nil && res.Outdated {
//nolint //nolint
log.Warn().Msgf( 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", "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, res.Current,
Version, types.Version,
) )
} }
} }

View file

@ -1,11 +1,10 @@
package cli package cli
import ( import (
"github.com/juanfont/headscale/hscontrol/types"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var Version = "dev"
func init() { func init() {
rootCmd.AddCommand(versionCmd) rootCmd.AddCommand(versionCmd)
} }
@ -16,6 +15,9 @@ var versionCmd = &cobra.Command{
Long: "The version of headscale.", Long: "The version of headscale.",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
output, _ := cmd.Flags().GetString("output") 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)
}, },
} }

View file

@ -30,7 +30,7 @@ latestTag=$(git describe --tags `git rev-list --tags --max-count=1`)
git checkout $latestTag 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 # make it executable
chmod a+x headscale chmod a+x headscale

View file

@ -12,17 +12,15 @@
flake-utils, flake-utils,
... ...
}: let }: let
headscaleVersion = headscaleVersion = self.shortRev or self.dirtyShortRev;
if (self ? shortRev) commitHash = self.rev or self.dirtyRev;
then self.shortRev
else "dev";
in in
{ {
overlay = _: prev: let overlay = _: prev: let
pkgs = nixpkgs.legacyPackages.${prev.system}; pkgs = nixpkgs.legacyPackages.${prev.system};
buildGo = pkgs.buildGo123Module; buildGo = pkgs.buildGo123Module;
in { in {
headscale = buildGo rec { headscale = buildGo {
pname = "headscale"; pname = "headscale";
version = headscaleVersion; version = headscaleVersion;
src = pkgs.lib.cleanSource self; src = pkgs.lib.cleanSource self;
@ -36,7 +34,12 @@
subPackages = ["cmd/headscale"]; 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 { protoc-gen-grpc-gateway = buildGo rec {

View file

@ -566,8 +566,8 @@ func (h *Headscale) Serve() error {
spew.Dump(h.cfg) spew.Dump(h.cfg)
} }
log.Info().Str("version", types.Version).Str("commit", types.GitCommitHash).Msg("Starting Headscale")
log.Info(). log.Info().
Caller().
Str("minimum_version", capver.TailscaleVersion(capver.MinSupportedCapabilityVersion)). Str("minimum_version", capver.TailscaleVersion(capver.MinSupportedCapabilityVersion)).
Msg("Clients with a lower minimum version will be rejected") Msg("Clients with a lower minimum version will be rejected")

View file

@ -0,0 +1,4 @@
package types
var Version = "dev"
var GitCommitHash = "dev"