Skip to content

Commit 8f8f932

Browse files
committed
address pr feedback
1 parent 6a0c240 commit 8f8f932

File tree

4 files changed

+28
-50
lines changed

4 files changed

+28
-50
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -388,5 +388,6 @@ On MacOS or Windows systems, we recommend either using a VM or the provided `.de
388388
| `--coder-agent-subsystem` | `CODER_AGENT_SUBSYSTEM` | | Coder agent subsystems to report when forwarding logs. The envbuilder subsystem is always included. |
389389
| `--push-image` | `ENVBUILDER_PUSH_IMAGE` | | Push the built image to a remote registry. This option forces a reproducible build. |
390390
| `--get-cached-image` | `ENVBUILDER_GET_CACHED_IMAGE` | | Print the digest of the cached image, if available. Exits with an error if not found. |
391+
| `--remote-repo-build-mode` | `ENVBUILDER_REMOTE_REPO_BUILD_MODE` | `false` | Use the remote repository as the source of truth when building the image. This means that the users changes to the local files that have been cloned will not be reflected in the image. This is useful as a way to improve cache utilization (get cached image). |
391392
| `--verbose` | `ENVBUILDER_VERBOSE` | | Enable verbose logging. |
392393
<!--- END docsgen --->

envbuilder.go

+8-20
Original file line numberDiff line numberDiff line change
@@ -130,22 +130,16 @@ func Run(ctx context.Context, opts options.Options) error {
130130
opts.Logger(log.LevelError, "Falling back to the default image...")
131131
}
132132

133-
// The repo wasn't cloned (i.e. may not be up-to-date with remote), so
134-
// we need to clone it to get the right build context. This is only
135-
// necessary when the repo is in remote build mode. If the repo is in
136-
// local build mode, the build context is already available on the host
137-
// filesystem.
138-
//
139-
// Skipping clone here if we believe workspace repo matches remote repo
140-
// is a performance optimization.
141-
if fallbackErr == nil && !cloned && opts.RepoBuildMode == options.RepoBuildModeRemote {
133+
// Always clone the repo in remote repo build mode into a location that
134+
// we control that isn't affected by the users changes.
135+
if opts.RemoteRepoBuildMode {
142136
cloneOpts, err := git.CloneOptionsFromOptions(opts)
143137
if err != nil {
144138
return fmt.Errorf("git clone options: %w", err)
145139
}
146140
cloneOpts.Path = constants.MagicRemoteRepoDir
147141

148-
endStage := startStage("📦 Remote build mode enabled, cloning %s to %s for build context...",
142+
endStage := startStage("📦 Remote repo build mode enabled, cloning %s to %s for build context...",
149143
newColor(color.FgCyan).Sprintf(opts.GitURL),
150144
newColor(color.FgCyan).Sprintf(cloneOpts.Path),
151145
)
@@ -895,22 +889,16 @@ func RunCacheProbe(ctx context.Context, opts options.Options) (v1.Image, error)
895889
opts.Logger(log.LevelError, "Falling back to the default image...")
896890
}
897891

898-
// The repo wasn't cloned (i.e. may not be up-to-date with remote), so
899-
// we need to clone it to get the right build context. This is only
900-
// necessary when the repo is in remote build mode. If the repo is in
901-
// local build mode, the build context is already available on the host
902-
// filesystem.
903-
//
904-
// Skipping clone here if we believe workspace repo matches remote repo
905-
// is a performance optimization.
906-
if opts.RepoBuildMode == options.RepoBuildModeRemote {
892+
// Always clone the repo in remote repo build mode into a location that
893+
// we control that isn't affected by the users changes.
894+
if opts.RemoteRepoBuildMode {
907895
cloneOpts, err := git.CloneOptionsFromOptions(opts)
908896
if err != nil {
909897
return nil, fmt.Errorf("git clone options: %w", err)
910898
}
911899
cloneOpts.Path = constants.MagicRemoteRepoDir
912900

913-
endStage := startStage("📦 Remote build mode enabled, cloning %s to %s for build context...",
901+
endStage := startStage("📦 Remote repo build mode enabled, cloning %s to %s for build context...",
914902
newColor(color.FgCyan).Sprintf(opts.GitURL),
915903
newColor(color.FgCyan).Sprintf(cloneOpts.Path),
916904
)

options/options.go

+14-23
Original file line numberDiff line numberDiff line change
@@ -150,23 +150,15 @@ type Options struct {
150150
// and if it is, to return it.
151151
GetCachedImage bool
152152

153-
// RepoBuildMode is the mode to use when building the workspace from a
154-
// repository. When set to `local`, the repository is cloned locally and
155-
// built. If the repository is already present, it is used as the source of
156-
// truth. When set to `remote`, the repository is cloned into a temporary
157-
// directory and built. This is useful when the repository should act as the
158-
// source of truth. Defaults to "local".
159-
RepoBuildMode string
153+
// RemoteRepoBuildMode uses the remote repository as the source of truth
154+
// when building the image. This means that the users changes to the local
155+
// files that have been cloned will not be reflected in the image. This is
156+
// useful as a way to improve cache utilization (get cached image).
157+
RemoteRepoBuildMode bool
160158
}
161159

162160
const envPrefix = "ENVBUILDER_"
163161

164-
// Available modes for repo build mode.
165-
const (
166-
RepoBuildModeLocal = "local"
167-
RepoBuildModeRemote = "remote"
168-
)
169-
170162
// Generate CLI options for the envbuilder command.
171163
func (o *Options) CLI() serpent.OptionSet {
172164
options := serpent.OptionSet{
@@ -435,16 +427,15 @@ func (o *Options) CLI() serpent.OptionSet {
435427
"Exits with an error if not found.",
436428
},
437429
{
438-
Flag: "repo-build-mode",
439-
Env: WithEnvPrefix("REPO_BUILD_MODE"),
440-
Value: serpent.EnumOf(&o.RepoBuildMode, RepoBuildModeLocal, RepoBuildModeRemote),
441-
Default: RepoBuildModeLocal,
442-
Description: "The mode to use when building the workspace from a " +
443-
"repository. When set to `local`, the repository is cloned locally " +
444-
"and built. If the repository is already present, it is used as the " +
445-
"source of truth. When set to `remote`, the repository is cloned into " +
446-
"a temporary directory and built. This is useful when the repository " +
447-
"should act as the source of truth.",
430+
Flag: "remote-repo-build-mode",
431+
Env: WithEnvPrefix("REMOTE_REPO_BUILD_MODE"),
432+
Value: serpent.BoolOf(&o.RemoteRepoBuildMode),
433+
Default: "false",
434+
Description: "Use the remote repository as the source of truth " +
435+
"when building the image. This means that the users changes to " +
436+
"the local files that have been cloned will not be reflected in " +
437+
"the image. This is useful as a way to improve cache utilization " +
438+
"(get cached image).",
448439
},
449440
{
450441
Flag: "verbose",

options/testdata/options.golden

+5-7
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,11 @@ OPTIONS:
138138
Push the built image to a remote registry. This option forces a
139139
reproducible build.
140140

141-
--repo-build-mode local|remote, $ENVBUILDER_REPO_BUILD_MODE (default: local)
142-
The mode to use when building the workspace from a repository. When
143-
set to `local`, the repository is cloned locally and built. If the
144-
repository is already present, it is used as the source of truth. When
145-
set to `remote`, the repository is cloned into a temporary directory
146-
and built. This is useful when the repository should act as the source
147-
of truth.
141+
--remote-repo-build-mode bool, $ENVBUILDER_REMOTE_REPO_BUILD_MODE (default: false)
142+
Use the remote repository as the source of truth when building the
143+
image. This means that the users changes to the local files that have
144+
been cloned will not be reflected in the image. This is useful as a
145+
way to improve cache utilization (get cached image).
148146

149147
--setup-script string, $ENVBUILDER_SETUP_SCRIPT
150148
The script to run before the init script. It runs as the root user

0 commit comments

Comments
 (0)