Skip to content

Commit

Permalink
fix: Add Dockerfile for digest calculation (#9666)
Browse files Browse the repository at this point in the history
feat: Add Dockerfile to digest calculation

Previously changes in Dockerfile did not lead to a new artifact being
built.
  • Loading branch information
AndreasBergmeier6176 authored Jan 22, 2025
1 parent e6c5223 commit 4aeb393
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/skaffold/tag/input_digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ func (t *inputDigestTagger) GenerateTag(ctx context.Context, image latest.Artifa
return "", err
}

if image.DockerArtifact != nil {
srcFiles = append(srcFiles, image.DockerArtifact.DockerfilePath)
}

if image.KanikoArtifact != nil {
srcFiles = append(srcFiles, image.KanikoArtifact.DockerfilePath)
}

if image.CustomArtifact != nil && image.CustomArtifact.Dependencies != nil && image.CustomArtifact.Dependencies.Dockerfile != nil {
srcFiles = append(srcFiles, image.CustomArtifact.Dependencies.Dockerfile.Path)
}

// must sort as hashing is sensitive to the order in which files are processed
sort.Strings(srcFiles)
for _, d := range srcFiles {
Expand Down
72 changes: 72 additions & 0 deletions pkg/skaffold/tag/input_digest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@ limitations under the License.
package tag

import (
"context"
"io"
"os"
"path/filepath"
"testing"

"github.com/google/go-cmp/cmp"

"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/graph"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/runner/runcontext"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/v2/testutil"
)

Expand Down Expand Up @@ -81,3 +88,68 @@ func TestInputDigest(t *testing.T) {
t.CheckFalse(hash1 == hash2)
})
}
func TestGenerateTag(t *testing.T) {
testutil.Run(t, "CompareTagWithAndWithoutDockerfile", func(t *testutil.T) {
runCtx := &runcontext.RunContext{}
dockerfile1Path := filepath.Join(t.TempDir(), "Dockerfile1")
dockerfile2Path := filepath.Join(t.TempDir(), "Dockerfile2")

f, err := os.Create(dockerfile1Path)
if err != nil {
panic(err)
}
defer f.Close()

_, err = io.WriteString(f, `
FROM busybox
CMD [ "ps", "faux" ]
`)
if err != nil {
panic(err)
}

f, err = os.Create(dockerfile2Path)
if err != nil {
panic(err)
}
defer f.Close()

_, err = io.WriteString(f, `
FROM busybox
CMD [ "true" ]
`)
if err != nil {
panic(err)
}

digestExample, _ := NewInputDigestTagger(runCtx, graph.ToArtifactGraph(runCtx.Artifacts()))
tag1, err := digestExample.GenerateTag(context.Background(), latest.Artifact{
ArtifactType: latest.ArtifactType{
DockerArtifact: &latest.DockerArtifact{
DockerfilePath: dockerfile1Path,
},
},
})
if err != nil {
t.Fatalf("Generate first tag failed: %v", err)
}

digestExample, _ = NewInputDigestTagger(runCtx, graph.ToArtifactGraph(runCtx.Artifacts()))
tag2, err := digestExample.GenerateTag(context.Background(), latest.Artifact{
ArtifactType: latest.ArtifactType{
DockerArtifact: &latest.DockerArtifact{
DockerfilePath: dockerfile2Path,
},
},
})
if err != nil {
t.Fatalf("Generate second tag failed: %v", err)
}

if diff := cmp.Diff(tag1, tag2); diff == "" {
t.Error("Tag does not differ between first and second Dockerfile")
}
})
}

0 comments on commit 4aeb393

Please sign in to comment.