From 0dee97e338352491ef58fe519acbe2858bba5d45 Mon Sep 17 00:00:00 2001 From: Karl Trygve Kalleberg Date: Mon, 3 Apr 2023 10:47:53 +0200 Subject: [PATCH] Expose tar_path command line option from Kaniko (#78) --- cmd/kaniko-docker/main.go | 6 ++++++ kaniko.go | 10 ++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/cmd/kaniko-docker/main.go b/cmd/kaniko-docker/main.go index e41b2f1..a0511da 100644 --- a/cmd/kaniko-docker/main.go +++ b/cmd/kaniko-docker/main.go @@ -176,6 +176,11 @@ func main() { Usage: "Set this flag if you only want to build the image, without pushing to a registry", EnvVar: "PLUGIN_NO_PUSH", }, + cli.StringFlag{ + Name: "tar-path", + Usage: "Set this flag to save the image as a tarball at path", + EnvVar: "PLUGIN_TAR_PATH", + }, cli.StringFlag{ Name: "verbosity", Usage: "Set this flag with value as oneof to set the logging level for kaniko. Defaults to info.", @@ -242,6 +247,7 @@ func run(c *cli.Context) error { CacheTTL: c.Int("cache-ttl"), DigestFile: defaultDigestFile, NoPush: noPush, + TarPath: c.String("tar-path"), Verbosity: c.String("verbosity"), Platform: c.String("platform"), SkipUnusedStages: c.Bool("skip-unused-stages"), diff --git a/kaniko.go b/kaniko.go index f5df59e..62d324c 100644 --- a/kaniko.go +++ b/kaniko.go @@ -39,6 +39,7 @@ type ( Verbosity string // Log level Platform string // Allows to build with another default platform than the host, similarly to docker build --platform SkipUnusedStages bool // Build only used stages + TarPath string // Set this flag to save the image as a tarball at path } // Artifact defines content of artifact file @@ -155,14 +156,15 @@ func (p Plugin) Exec() error { fmt.Sprintf("--context=dir://%s", p.Build.Context), } - // Set the destination repository - if !p.Build.NoPush { + // Set the destination repository only when we push or save to tarball + if !p.Build.NoPush || p.Build.TarPath != "" { for _, tag := range tags { for _, label := range p.Build.labelsForTag(tag) { cmdArgs = append(cmdArgs, fmt.Sprintf("--destination=%s:%s", p.Build.Repo, label)) } } } + // Set the build arguments for _, arg := range p.Build.Args { cmdArgs = append(cmdArgs, fmt.Sprintf("--build-arg=%s", arg)) @@ -219,6 +221,10 @@ func (p Plugin) Exec() error { cmdArgs = append(cmdArgs, "--skip-unused-stages") } + if p.Build.TarPath != "" { + cmdArgs = append(cmdArgs, fmt.Sprintf("--tar-path=%s", p.Build.TarPath)) + } + cmd := exec.Command("/kaniko/executor", cmdArgs...) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr