mirror of
https://github.com/GoogleContainerTools/kaniko
synced 2026-10-18 18:58:32 +02:00
Use only the necessary files in the cache keys. (#387)
This commit is contained in:
+11
-7
@@ -125,10 +125,6 @@ func (s *stageBuilder) build() error {
|
||||
|
||||
// Set the initial cache key to be the base image digest, the build args and the SrcContext.
|
||||
compositeKey := NewCompositeCache(s.baseImageDigest)
|
||||
contextHash, err := HashDir(s.opts.SrcContext)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
compositeKey.AddKey(s.opts.BuildArgs...)
|
||||
|
||||
cmds := []commands.DockerCommand{}
|
||||
@@ -148,8 +144,16 @@ func (s *stageBuilder) build() error {
|
||||
|
||||
// Add the next command to the cache key.
|
||||
compositeKey.AddKey(command.String())
|
||||
if command.UsesContext() {
|
||||
compositeKey.AddKey(contextHash)
|
||||
|
||||
// If the command uses files from the context, add them.
|
||||
files, err := command.FilesUsedFromContext(&s.cf.Config, args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, f := range files {
|
||||
if err := compositeKey.AddPath(f); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
logrus.Info(command.String())
|
||||
|
||||
@@ -172,7 +176,7 @@ func (s *stageBuilder) build() error {
|
||||
if err := command.ExecuteCommand(&s.cf.Config, args); err != nil {
|
||||
return err
|
||||
}
|
||||
files := command.FilesToSnapshot()
|
||||
files = command.FilesToSnapshot()
|
||||
var contents []byte
|
||||
|
||||
if !s.shouldTakeSnapshot(index, files) {
|
||||
|
||||
@@ -53,6 +53,32 @@ func (s *CompositeCache) Hash() (string, error) {
|
||||
return util.SHA256(strings.NewReader(s.Key()))
|
||||
}
|
||||
|
||||
func (s *CompositeCache) AddPath(p string) error {
|
||||
sha := sha256.New()
|
||||
fi, err := os.Lstat(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if fi.Mode().IsDir() {
|
||||
k, err := HashDir(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.keys = append(s.keys, k)
|
||||
return nil
|
||||
}
|
||||
fh, err := util.CacheHasher()(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := sha.Write([]byte(fh)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s.keys = append(s.keys, string(sha.Sum(nil)))
|
||||
return nil
|
||||
}
|
||||
|
||||
// HashDir returns a hash of the directory.
|
||||
func HashDir(p string) (string, error) {
|
||||
sha := sha256.New()
|
||||
|
||||
Reference in New Issue
Block a user