From 497d803824a26bc5e6bf1e358a5ada996f46c2c0 Mon Sep 17 00:00:00 2001 From: Hamish Date: Mon, 16 Dec 2024 22:20:08 +1100 Subject: [PATCH] Added the ability to specify build targets for kaniko build and an option to skip building unused layers --- devcontainer/devcontainer.go | 2 ++ docs/env-variables.md | 1 + envbuilder.go | 2 ++ options/options.go | 12 ++++++++++++ options/testdata/options.golden | 4 ++++ 5 files changed, 21 insertions(+) diff --git a/devcontainer/devcontainer.go b/devcontainer/devcontainer.go index 0bf7cc3..4ae8b61 100644 --- a/devcontainer/devcontainer.go +++ b/devcontainer/devcontainer.go @@ -69,6 +69,7 @@ type Compiled struct { BuildContext string FeatureContexts map[string]string BuildArgs []string + Target string User string ContainerEnv map[string]string @@ -140,6 +141,7 @@ func (s Spec) HasDockerfile() bool { func (s *Spec) Compile(fs billy.Filesystem, devcontainerDir, scratchDir string, fallbackDockerfile, workspaceFolder string, useBuildContexts bool, lookupEnv func(string) (string, bool)) (*Compiled, error) { params := &Compiled{ User: s.ContainerUser, + Target: s.Build.Target, ContainerEnv: s.ContainerEnv, RemoteEnv: s.RemoteEnv, } diff --git a/docs/env-variables.md b/docs/env-variables.md index 861f31b..889af06 100644 --- a/docs/env-variables.md +++ b/docs/env-variables.md @@ -24,6 +24,7 @@ | `--ignore-paths` | `ENVBUILDER_IGNORE_PATHS` | | The comma separated list of paths to ignore when building the workspace. | | `--build-secrets` | `ENVBUILDER_BUILD_SECRETS` | | The list of secret environment variables to use when building the image. | | `--skip-rebuild` | `ENVBUILDER_SKIP_REBUILD` | | Skip building if the MagicFile exists. This is used to skip building when a container is restarting. e.g. docker stop -> docker start This value can always be set to true - even if the container is being started for the first time. | +| `--skip-unused-stages` | `ENVBUILDER_SKIP_UNUSED_STAGES` | | Skip building all unused docker stages. Otherwise it builds by default all stages, even the unnecessary ones until it reaches the target stage / end of Dockerfile. | | `--git-url` | `ENVBUILDER_GIT_URL` | | The URL of a Git repository containing a Devcontainer or Docker image to clone. This is optional. | | `--git-clone-depth` | `ENVBUILDER_GIT_CLONE_DEPTH` | | The depth to use when cloning the Git repository. | | `--git-clone-single-branch` | `ENVBUILDER_GIT_CLONE_SINGLE_BRANCH` | | Clone only a single branch of the Git repository. | diff --git a/envbuilder.go b/envbuilder.go index ad2c84f..29345f8 100644 --- a/envbuilder.go +++ b/envbuilder.go @@ -539,6 +539,8 @@ func run(ctx context.Context, opts options.Options, execArgs *execArgsInfo) erro CustomPlatform: platforms.Format(platforms.Normalize(platforms.DefaultSpec())), SnapshotMode: "redo", RunV2: true, + SkipUnusedStages: opts.SkipUnusedStages, + Target: buildParams.Target, RunStdout: stdoutWriter, RunStderr: stderrWriter, Destinations: destinations, diff --git a/options/options.go b/options/options.go index c2b6efe..e4121fb 100644 --- a/options/options.go +++ b/options/options.go @@ -100,6 +100,10 @@ type Options struct { // This value can always be set to true - even if the container is being // started for the first time. SkipRebuild bool + // SkipUnusedStages builds only used stages if defined to true. Otherwise, + // it builds by default all stages, even the unnecessary ones until it + // reaches the target stage / end of Dockerfile + SkipUnusedStages bool // GitURL is the URL of the Git repository to clone. This is optional. GitURL string // GitCloneDepth is the depth to use when cloning the Git repository. @@ -357,6 +361,14 @@ func (o *Options) CLI() serpent.OptionSet { "docker start This value can always be set to true - even if the " + "container is being started for the first time.", }, + { + Flag: "skip-unused-stages", + Env: WithEnvPrefix("SKIP_UNUSED_STAGES"), + Value: serpent.BoolOf(&o.SkipUnusedStages), + Description: "Skip building all unused docker stages. Otherwise it builds by " + + "default all stages, even the unnecessary ones until it reaches the " + + "target stage / end of Dockerfile.", + }, { Flag: "git-url", Env: WithEnvPrefix("GIT_URL"), diff --git a/options/testdata/options.golden b/options/testdata/options.golden index 6a8145a..b03a633 100644 --- a/options/testdata/options.golden +++ b/options/testdata/options.golden @@ -173,6 +173,10 @@ OPTIONS: when a container is restarting. e.g. docker stop -> docker start This value can always be set to true - even if the container is being started for the first time. + --skip-unused-stages bool, $ENVBUILDER_SKIP_UNUSED_STAGES + Skip building all unused docker stages. Otherwise it builds by default + all stages, even the unnecessary ones until it reaches the target + stage / end of Dockerfile. --ssl-cert-base64 string, $ENVBUILDER_SSL_CERT_BASE64 The content of an SSL cert file. This is useful for self-signed