Skip to content

Commit b953abb

Browse files
authored
Merge pull request #5424 from camilamacedo86/fix-release-dirty
🐛 CLI release version): Preserve clean version tags in GoReleaser builds
2 parents d956ca2 + 10d1973 commit b953abb

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

internal/version/version.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ func resolveMainVersion(main debug.Module) string {
7676
return develVersion
7777
}
7878

79+
// isPseudoVersion reports whether a version is a pseudo-version
80+
// (e.g., v0.0.0-20191109021931-daa7c04131f5 or v1.2.4-0.20191109021931-daa7c04131f5)
81+
func isPseudoVersion(v string) bool {
82+
return strings.Contains(v, "-0.")
83+
}
84+
7985
func (v *Version) applyVCSMetadata(settings []debug.BuildSetting) {
8086
var isDirty bool
8187

@@ -91,9 +97,15 @@ func (v *Version) applyVCSMetadata(settings []debug.BuildSetting) {
9197
}
9298

9399
if isDirty {
94-
if !strings.Contains(v.KubeBuilderVersion, "dirty") {
95-
v.KubeBuilderVersion += "-dirty"
100+
// For development builds (not proper releases), use develVersion to avoid
101+
// polluting PROJECT files with unstable -dirty version strings.
102+
// For tagged releases, ignore the dirty flag to support GoReleaser builds
103+
// that may create artifacts during the build process.
104+
if v.KubeBuilderVersion == develVersion || isPseudoVersion(v.KubeBuilderVersion) {
105+
v.KubeBuilderVersion = develVersion
96106
}
107+
// Note: We don't append -dirty to tagged release versions to support
108+
// GoReleaser and similar build tools that may modify files during build.
97109

98110
if !strings.Contains(v.GitCommit, "dirty") {
99111
v.GitCommit += "-dirty"

internal/version/version_test.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func TestNew(t *testing.T) {
4848
}
4949
})
5050

51-
t.Run("VCS metadata resolution", func(t *testing.T) {
51+
t.Run("VCS metadata resolution with tagged release", func(t *testing.T) {
5252
v := &Version{KubeBuilderVersion: "v1.0.0"}
5353
settings := []debug.BuildSetting{
5454
{Key: "vcs.revision", Value: "abcdef123"},
@@ -60,8 +60,9 @@ func TestNew(t *testing.T) {
6060
if !strings.HasSuffix(v.GitCommit, "-dirty") {
6161
t.Errorf("expected commit to be dirty, got %s", v.GitCommit)
6262
}
63-
if !strings.Contains(v.KubeBuilderVersion, "-dirty") {
64-
t.Errorf("expected version to be dirty, got %s", v.KubeBuilderVersion)
63+
// For tagged releases, we ignore dirty flag to support GoReleaser builds
64+
if v.KubeBuilderVersion != "v1.0.0" {
65+
t.Errorf("expected version to remain v1.0.0, got %s", v.KubeBuilderVersion)
6566
}
6667
})
6768

@@ -148,9 +149,32 @@ func TestApplyVCSMetadata(t *testing.T) {
148149
{Key: "vcs.time", Value: "2025-12-29T19:30:00Z"},
149150
},
150151
expectCommit: "abcdef123-dirty",
151-
expectVersion: "(devel)-dirty",
152+
expectVersion: "(devel)",
152153
expectDate: "2025-12-29T19:30:00Z",
153154
},
155+
{
156+
name: "Dirty tagged release (GoReleaser scenario)",
157+
initialVersion: "v4.5.3-rc.1",
158+
settings: []debug.BuildSetting{
159+
{Key: "vcs.revision", Value: "abcdef123"},
160+
{Key: "vcs.modified", Value: "true"},
161+
{Key: "vcs.time", Value: "2025-12-30T10:00:00Z"},
162+
},
163+
expectCommit: "abcdef123-dirty",
164+
expectVersion: "v4.5.3-rc.1", // Stays clean for tagged releases
165+
expectDate: "2025-12-30T10:00:00Z",
166+
},
167+
{
168+
name: "Dirty pseudo-version",
169+
initialVersion: "v1.2.4-0.20191109021931-daa7c04131f5",
170+
settings: []debug.BuildSetting{
171+
{Key: "vcs.revision", Value: "abcdef123"},
172+
{Key: "vcs.modified", Value: "true"},
173+
},
174+
expectCommit: "abcdef123-dirty",
175+
expectVersion: "(devel)", // Pseudo-versions become (devel) when dirty
176+
expectDate: "",
177+
},
154178
}
155179

156180
for _, tt := range tests {

0 commit comments

Comments
 (0)