-
Notifications
You must be signed in to change notification settings - Fork 45
feat: implement reproducible build and get cached image #213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
d0b4d6a
5a50f4a
6cb1217
7764535
d54ca81
ea91cd5
1979ffd
a962a26
d804fc9
3b18839
b7502ee
aacdbfc
149bdb3
354d885
15904df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,6 +99,9 @@ func Run(ctx context.Context, options Options) error { | |
if options.InitCommand == "" { | ||
options.InitCommand = "/bin/sh" | ||
} | ||
if options.CacheRepo == "" && options.PushImage { | ||
return fmt.Errorf("--cache-repo must be set when using --push-image!") | ||
johnstcn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. review: I don't see any good reason to not do this. It prevents potential head-scratching if someone sets There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dry run maybe? |
||
// Default to the shell! | ||
initArgs := []string{"-c", options.InitScript} | ||
if options.InitArgs != "" { | ||
|
@@ -119,11 +122,11 @@ func Run(ctx context.Context, options Options) error { | |
options.WorkspaceFolder = f | ||
} | ||
|
||
stageNumber := 1 | ||
stageNumber := 0 | ||
startStage := func(format string, args ...any) func(format string, args ...any) { | ||
now := time.Now() | ||
stageNum := stageNumber | ||
stageNumber++ | ||
stageNum := stageNumber | ||
options.Logger(notcodersdk.LogLevelInfo, "#%d: %s", stageNum, fmt.Sprintf(format, args...)) | ||
|
||
return func(format string, args ...any) { | ||
|
@@ -338,7 +341,7 @@ func Run(ctx context.Context, options Options) error { | |
|
||
HijackLogrus(func(entry *logrus.Entry) { | ||
for _, line := range strings.Split(entry.Message, "\r") { | ||
options.Logger(notcodersdk.LogLevelInfo, "#2: %s", color.HiBlackString(line)) | ||
options.Logger(notcodersdk.LogLevelInfo, "#%d: %s", stageNumber, color.HiBlackString(line)) | ||
} | ||
}) | ||
|
||
|
@@ -474,20 +477,24 @@ func Run(ctx context.Context, options Options) error { | |
cacheTTL = time.Hour * 24 * time.Duration(options.CacheTTLDays) | ||
} | ||
|
||
endStage := startStage("ποΈ Building image...") | ||
// At this point we have all the context, we can now build! | ||
registryMirror := []string{} | ||
if val, ok := os.LookupEnv("KANIKO_REGISTRY_MIRROR"); ok { | ||
registryMirror = strings.Split(val, ";") | ||
} | ||
image, err := executor.DoBuild(&config.KanikoOptions{ | ||
var destinations []string | ||
if options.CacheRepo != "" { | ||
destinations = append(destinations, options.CacheRepo) | ||
} | ||
opts := &config.KanikoOptions{ | ||
// Boilerplate! | ||
CustomPlatform: platforms.Format(platforms.Normalize(platforms.DefaultSpec())), | ||
SnapshotMode: "redo", | ||
RunV2: true, | ||
RunStdout: stdoutWriter, | ||
RunStderr: stderrWriter, | ||
Destinations: []string{"local"}, | ||
Destinations: destinations, | ||
NoPush: !options.PushImage || len(destinations) == 0, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm assuming the reason for the logical disjuction here is that Kaniko will push all intermediate cache layers to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep ππ» |
||
CacheRunLayers: true, | ||
CacheCopyLayers: true, | ||
CompressedCaching: true, | ||
|
@@ -518,11 +525,40 @@ func Run(ctx context.Context, options Options) error { | |
RegistryMirrors: registryMirror, | ||
}, | ||
SrcContext: buildParams.BuildContext, | ||
}) | ||
|
||
// For cached image utilization, produce reproducible builds. | ||
Reproducible: options.PushImage, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where is this getting used? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See coder/kaniko#12 |
||
} | ||
|
||
if options.GetCachedImage { | ||
endStage := startStage("ποΈ Checking for cached image...") | ||
image, err := executor.DoCacheProbe(opts) | ||
if err != nil { | ||
return nil, xerrors.Errorf("unable to get cached image: %w", err) | ||
} | ||
digest, err := image.Digest() | ||
if err != nil { | ||
return nil, xerrors.Errorf("get cached image digest: %w", err) | ||
johnstcn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
endStage("ποΈ Found cached image!") | ||
_, _ = fmt.Fprintf(os.Stdout, "%s@%s\n", options.CacheRepo, digest.String()) | ||
os.Exit(0) | ||
} | ||
|
||
endStage := startStage("ποΈ Building image...") | ||
image, err := executor.DoBuild(opts) | ||
if err != nil { | ||
return nil, err | ||
return nil, xerrors.Errorf("do build: %w", err) | ||
} | ||
endStage("ποΈ Built image!") | ||
if options.PushImage { | ||
endStage = startStage("ποΈ Pushing image...") | ||
if err := executor.DoPush(image, opts); err != nil { | ||
return nil, xerrors.Errorf("do push: %w", err) | ||
} | ||
endStage("ποΈ Pushed image!") | ||
} | ||
|
||
return image, err | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.