diff --git a/pkg/skaffold/build/tag/git_commit.go b/pkg/skaffold/build/tag/git_commit.go index d7b9b1e6954..aeeb829afa3 100644 --- a/pkg/skaffold/build/tag/git_commit.go +++ b/pkg/skaffold/build/tag/git_commit.go @@ -39,7 +39,7 @@ func (c *GitCommit) Labels() map[string]string { // GenerateFullyQualifiedImageName tags an image with the supplied image name and the git commit. func (c *GitCommit) GenerateFullyQualifiedImageName(workingDir string, imageName string) (string, error) { - hash, err := runGit(workingDir, "rev-parse", "--short", "HEAD") + ref, err := runGit(workingDir, "describe", "--tags", "--always") if err != nil { logrus.Warnln("Unable to find git commit:", err) return fmt.Sprintf("%s:dirty", imageName), nil @@ -51,16 +51,10 @@ func (c *GitCommit) GenerateFullyQualifiedImageName(workingDir string, imageName } if len(changes) > 0 { - return fmt.Sprintf("%s:%s-dirty", imageName, hash), nil + return fmt.Sprintf("%s:%s-dirty", imageName, ref), nil } - // Ignore error. It means there's no tag. - tag, _ := runGit(workingDir, "describe", "--tags", "--exact-match") - if len(tag) > 0 { - return fmt.Sprintf("%s:%s", imageName, tag), nil - } - - return fmt.Sprintf("%s:%s", imageName, hash), nil + return fmt.Sprintf("%s:%s", imageName, ref), nil } func runGit(workingDir string, arg ...string) (string, error) { diff --git a/pkg/skaffold/build/tag/git_commit_test.go b/pkg/skaffold/build/tag/git_commit_test.go index 46bd71b2383..3ded7e4aff4 100644 --- a/pkg/skaffold/build/tag/git_commit_test.go +++ b/pkg/skaffold/build/tag/git_commit_test.go @@ -78,8 +78,8 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) { }, }, { - description: "ignore tag when dirty", - expectedName: "test:eefe1b9-dirty", + description: "dirty tag", + expectedName: "test:v1-dirty", createGitRepo: func(dir string) { gitInit(t, dir). write("source.go", []byte("code")). @@ -101,8 +101,8 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) { }, }, { - description: "don't use tag if not exact match", - expectedName: "test:3cec6b9", + description: "tag plus one commit", + expectedName: "test:v1-1-g3cec6b9", createGitRepo: func(dir string) { gitInit(t, dir). write("source.go", []byte("code")). @@ -186,7 +186,7 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) { }, { description: "updated artifact in dirty repo", - expectedName: "test:0c60cb8-dirty", + expectedName: "test:v1-dirty", createGitRepo: func(dir string) { gitInit(t, dir). mkdir("artifact1").write("artifact1/source.go", []byte("code")).