@@ -87,23 +87,41 @@ func DoBuild(opts *options.KanikoOptions) (v1.Image, error) {
8787 if err := dockerCommand .ExecuteCommand (& imageConfig .Config , buildArgs ); err != nil {
8888 return nil , err
8989 }
90- // Don't snapshot if it's not the final stage and not the final command
91- // Also don't snapshot if it's the final stage, not the final command, and single snapshot is set
92- if (! finalStage && ! finalCmd ) || (finalStage && ! finalCmd && opts .SingleSnapshot ) {
93- continue
94- }
95- // Now, we get the files to snapshot from this command and take the snapshot
9690 snapshotFiles := dockerCommand .FilesToSnapshot ()
97- if finalCmd {
98- snapshotFiles = nil
91+ var contents []byte
92+
93+ // If this is an intermediate stage, we only snapshot for the last command and we
94+ // want to snapshot the entire filesystem since we aren't tracking what was changed
95+ // by previous commands.
96+ if ! finalStage {
97+ if finalCmd {
98+ contents , err = snapshotter .TakeSnapshotFS ()
99+ }
100+ } else {
101+ // If we are in single snapshot mode, we only take a snapshot once, after all
102+ // commands have completed.
103+ if opts .SingleSnapshot {
104+ if finalCmd {
105+ contents , err = snapshotter .TakeSnapshotFS ()
106+ }
107+ } else {
108+ // Otherwise, in the final stage we take a snapshot at each command. If we know
109+ // the files that were changed, we'll snapshot those explicitly, otherwise we'll
110+ // check if anything in the filesystem changed.
111+ if snapshotFiles != nil {
112+ contents , err = snapshotter .TakeSnapshot (snapshotFiles )
113+ } else {
114+ contents , err = snapshotter .TakeSnapshotFS ()
115+ }
116+ }
99117 }
100- contents , err := snapshotter .TakeSnapshot (snapshotFiles )
101118 if err != nil {
102- return nil , err
119+ return nil , fmt . Errorf ( "Error taking snapshot of files for command %s: %s" , dockerCommand , err )
103120 }
121+
104122 util .MoveVolumeWhitelistToWhitelist ()
105123 if contents == nil {
106- logrus .Info ("No files were changed, appending empty layer to config." )
124+ logrus .Info ("No files were changed, appending empty layer to config. No layer added to image. " )
107125 continue
108126 }
109127 // Append the layer to the image
0 commit comments