@@ -109,7 +109,10 @@ func (k *dockerBuildkitProvider) Diff(ctx context.Context, req *rpc.DiffRequest)
109
109
applyDefaults (news )
110
110
news ["registryServer" ] = news ["registry" ].ObjectValue ()["server" ]
111
111
delete (news , "registry" )
112
- contextDigest , err := hashContext (news ["context" ].StringValue ())
112
+ contextDigest , err := hashContext (
113
+ news ["context" ].StringValue (),
114
+ news ["dockerfile" ].StringValue (),
115
+ )
113
116
if err != nil {
114
117
return nil , err
115
118
}
@@ -228,7 +231,7 @@ func (k *dockerBuildkitProvider) dockerBuild(
228
231
username := registry ["username" ]
229
232
password := registry ["password" ]
230
233
231
- contextDigest , err := hashContext (context )
234
+ contextDigest , err := hashContext (context , dockerfile )
232
235
if err != nil {
233
236
return nil , err
234
237
}
@@ -340,7 +343,7 @@ func (w *logWriter) Write(p []byte) (n int, err error) {
340
343
return len (p ), w .host .Log (w .ctx , w .severity , w .urn , string (p ))
341
344
}
342
345
343
- func hashContext (contextPath string ) (string , error ) {
346
+ func hashContext (contextPath string , dockerfile string ) (string , error ) {
344
347
dockerIgnore , err := os .ReadFile (filepath .Join (contextPath , ".dockerignore" ))
345
348
if err != nil && ! os .IsNotExist (err ) {
346
349
return "" , fmt .Errorf ("unable to read .dockerignore file: %w" , err )
@@ -396,6 +399,24 @@ func hashContext(contextPath string) (string, error) {
396
399
if err != nil {
397
400
return "" , fmt .Errorf ("unable to hash build context: %w" , err )
398
401
}
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
+ }
399
420
h := sha256 .New ()
400
421
h .Write (hashInput )
401
422
return hex .EncodeToString (h .Sum (nil )), nil
0 commit comments