refactor(build): move build metadata to pkg/buildinfo (#4045)

Keep CLI binaries from importing pkg/api/config just for version strings by
centralizing Commit/ReleaseTag/BinaryType/GoVersion in a tiny buildinfo package.
Update ldflags targets and callers accordingly.

Signed-off-by: Andrei Aaron <andreifdaaron@gmail.com>
This commit is contained in:
Andrei Aaron
2026-05-08 19:37:04 +03:00
committed by GitHub
parent fa92366009
commit b89d10834c
7 changed files with 48 additions and 33 deletions
+5 -10
View File
@@ -11,20 +11,15 @@ import (
distspec "github.com/opencontainers/distribution-spec/specs-go"
"github.com/tiendc/go-deepcopy"
"zotregistry.dev/zot/v2/pkg/buildinfo"
"zotregistry.dev/zot/v2/pkg/compat"
extconf "zotregistry.dev/zot/v2/pkg/extensions/config"
storageConstants "zotregistry.dev/zot/v2/pkg/storage/constants"
)
var (
Commit string //nolint: gochecknoglobals
ReleaseTag string //nolint: gochecknoglobals
BinaryType string //nolint: gochecknoglobals
GoVersion string //nolint: gochecknoglobals
openIDSupportedProviders = [...]string{"google", "gitlab", "oidc"} //nolint: gochecknoglobals
oauth2SupportedProviders = [...]string{"github"} //nolint: gochecknoglobals
)
type StorageConfig struct {
@@ -660,10 +655,10 @@ type Config struct {
func New() *Config {
return &Config{
DistSpecVersion: distspec.Version,
GoVersion: GoVersion,
Commit: Commit,
ReleaseTag: ReleaseTag,
BinaryType: BinaryType,
GoVersion: buildinfo.GoVersion,
Commit: buildinfo.Commit,
ReleaseTag: buildinfo.ReleaseTag,
BinaryType: buildinfo.BinaryType,
Storage: GlobalStorageConfig{
StorageConfig: StorageConfig{
Dedupe: true,
+18
View File
@@ -0,0 +1,18 @@
package buildinfo
// This package intentionally stays tiny and dependency-free because it is
// imported by CLI binaries that should not pull in server-only deps.
var (
// Commit is the git commit hash, injected at build time via -ldflags.
Commit string //nolint:gochecknoglobals
// ReleaseTag is the git tag for the release, injected at build time via -ldflags.
ReleaseTag string //nolint:gochecknoglobals
// BinaryType is a short identifier like "server", "cli", etc, injected at build time.
BinaryType string //nolint:gochecknoglobals
// GoVersion is the Go toolchain version used to build the binary, injected at build time.
GoVersion string //nolint:gochecknoglobals
)
+3 -3
View File
@@ -6,7 +6,7 @@ import (
distspec "github.com/opencontainers/distribution-spec/specs-go"
"github.com/spf13/cobra"
"zotregistry.dev/zot/v2/pkg/api/config"
"zotregistry.dev/zot/v2/pkg/buildinfo"
"zotregistry.dev/zot/v2/pkg/log"
)
@@ -21,8 +21,8 @@ func NewCliRootCmd() *cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
if showVersion {
logger := log.NewLogger("info", "")
logger.Info().Str("distribution-spec", distspec.Version).Str("commit", config.Commit).
Str("binary-type", config.BinaryType).Str("go version", config.GoVersion).Msg("version")
logger.Info().Str("distribution-spec", distspec.Version).Str("commit", buildinfo.Commit).
Str("binary-type", buildinfo.BinaryType).Str("go version", buildinfo.GoVersion).Msg("version")
} else {
_ = cmd.Usage()
cmd.SilenceErrors = false
+7 -6
View File
@@ -17,6 +17,7 @@ import (
"zotregistry.dev/zot/v2/pkg/api"
"zotregistry.dev/zot/v2/pkg/api/config"
"zotregistry.dev/zot/v2/pkg/api/constants"
"zotregistry.dev/zot/v2/pkg/buildinfo"
extconf "zotregistry.dev/zot/v2/pkg/extensions/config"
test "zotregistry.dev/zot/v2/pkg/test/common"
)
@@ -54,8 +55,8 @@ func TestServerStatusCommand(t *testing.T) {
space := regexp.MustCompile(`\s+`)
str := space.ReplaceAllString(buff.String(), " ")
actual := strings.TrimSpace(str)
So(actual, ShouldContainSubstring, config.ReleaseTag)
So(actual, ShouldContainSubstring, config.BinaryType)
So(actual, ShouldContainSubstring, buildinfo.ReleaseTag)
So(actual, ShouldContainSubstring, buildinfo.BinaryType)
// JSON
args = []string{"status", "--config", "status-test", "--format", "json"}
@@ -69,8 +70,8 @@ func TestServerStatusCommand(t *testing.T) {
space = regexp.MustCompile(`\s+`)
str = space.ReplaceAllString(buff.String(), " ")
actual = strings.TrimSpace(str)
So(actual, ShouldContainSubstring, config.ReleaseTag)
So(actual, ShouldContainSubstring, config.BinaryType)
So(actual, ShouldContainSubstring, buildinfo.ReleaseTag)
So(actual, ShouldContainSubstring, buildinfo.BinaryType)
// YAML
args = []string{"status", "--config", "status-test", "--format", "yaml"}
@@ -84,8 +85,8 @@ func TestServerStatusCommand(t *testing.T) {
space = regexp.MustCompile(`\s+`)
str = space.ReplaceAllString(buff.String(), " ")
actual = strings.TrimSpace(str)
So(actual, ShouldContainSubstring, config.ReleaseTag)
So(actual, ShouldContainSubstring, config.BinaryType)
So(actual, ShouldContainSubstring, buildinfo.ReleaseTag)
So(actual, ShouldContainSubstring, buildinfo.BinaryType)
// bad type
args = []string{"status", "--config", "status-test", "--format", "badType"}
+2 -1
View File
@@ -5,6 +5,7 @@ import (
"zotregistry.dev/zot/v2/pkg/api/config"
"zotregistry.dev/zot/v2/pkg/api/constants"
"zotregistry.dev/zot/v2/pkg/buildinfo"
"zotregistry.dev/zot/v2/pkg/log"
mTypes "zotregistry.dev/zot/v2/pkg/meta/types"
"zotregistry.dev/zot/v2/pkg/scheduler"
@@ -40,7 +41,7 @@ func GetExtensions(config *config.Config) distext.ExtensionList {
if len(endpoints) > 0 {
extensions = append(extensions, distext.Extension{
Name: constants.BaseExtension,
URL: "https://github.com/project-zot/zot/blob/" + config.ReleaseTag + "/pkg/extensions/_zot.md",
URL: "https://github.com/project-zot/zot/blob/" + buildinfo.ReleaseTag + "/pkg/extensions/_zot.md",
Description: "zot registry extensions",
Endpoints: endpoints,
})