Skip to content
This repository was archived by the owner on Mar 11, 2023. It is now read-only.

Commit 93fac84

Browse files
committed
Always add the Dockerfile to the context hash
The Dockerfile might not be inside the context, but it should still go in the context hash, since we need to trigger a rebuild if it changes. Fix #5.
1 parent b2342f5 commit 93fac84

File tree

1 file changed

+24
-3
lines changed
  • cmd/pulumi-resource-docker-buildkit

1 file changed

+24
-3
lines changed

cmd/pulumi-resource-docker-buildkit/main.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,10 @@ func (k *dockerBuildkitProvider) Diff(ctx context.Context, req *rpc.DiffRequest)
109109
applyDefaults(news)
110110
news["registryServer"] = news["registry"].ObjectValue()["server"]
111111
delete(news, "registry")
112-
contextDigest, err := hashContext(news["context"].StringValue())
112+
contextDigest, err := hashContext(
113+
news["context"].StringValue(),
114+
news["dockerfile"].StringValue(),
115+
)
113116
if err != nil {
114117
return nil, err
115118
}
@@ -228,7 +231,7 @@ func (k *dockerBuildkitProvider) dockerBuild(
228231
username := registry["username"]
229232
password := registry["password"]
230233

231-
contextDigest, err := hashContext(context)
234+
contextDigest, err := hashContext(context, dockerfile)
232235
if err != nil {
233236
return nil, err
234237
}
@@ -340,7 +343,7 @@ func (w *logWriter) Write(p []byte) (n int, err error) {
340343
return len(p), w.host.Log(w.ctx, w.severity, w.urn, string(p))
341344
}
342345

343-
func hashContext(contextPath string) (string, error) {
346+
func hashContext(contextPath string, dockerfile string) (string, error) {
344347
dockerIgnore, err := os.ReadFile(filepath.Join(contextPath, ".dockerignore"))
345348
if err != nil && !os.IsNotExist(err) {
346349
return "", fmt.Errorf("unable to read .dockerignore file: %w", err)
@@ -396,6 +399,24 @@ func hashContext(contextPath string) (string, error) {
396399
if err != nil {
397400
return "", fmt.Errorf("unable to hash build context: %w", err)
398401
}
402+
// Add the Dockerfile hash directly, since it might not be in the build
403+
// context.
404+
{
405+
path := filepath.Join(contextPath, dockerfile)
406+
f, err := os.Open(path)
407+
if err != nil {
408+
return "", fmt.Errorf("open %s: %w", path, err)
409+
}
410+
defer f.Close()
411+
h := sha256.New()
412+
_, err = io.Copy(h, f)
413+
if err != nil {
414+
return "", fmt.Errorf("read %s: %w", path, err)
415+
}
416+
hashInput = append(hashInput, path...)
417+
hashInput = append(hashInput, h.Sum(nil)...)
418+
hashInput = append(hashInput, byte(0))
419+
}
399420
h := sha256.New()
400421
h.Write(hashInput)
401422
return hex.EncodeToString(h.Sum(nil)), nil

0 commit comments

Comments
 (0)