8000 fix: ensure fallback image works with invalid git repo · coder/envbuilder@3c34c12 · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit 3c34c12

Browse files
committed
fix: ensure fallback image works with invalid git repo
1 parent 89354fb commit 3c34c12

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

envbuilder.go

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -243,14 +243,15 @@ func Run(ctx context.Context, options Options) error {
243243
Depth: options.GitCloneDepth,
244244
CABundle: caBundle,
245245
})
246-
if err != nil {
247-
return err
248-
}
249-
250-
if cloned {
251-
endStage("📦 Cloned repository!")
246+
if err == nil {
247+
if cloned {
248+
endStage("📦 Cloned repository!")
249+
} else {
250+
endStage("📦 The repository already exists!")
251+
}
252252
} else {
253-
endStage("📦 The repository already exists!")
253+
logf(codersdk.LogLevelError, "Failed to clone repository: %s", err.Error())
254+
logf(codersdk.LogLevelError, "Falling back to the default image...")
254255
}
255256
}
256257

@@ -452,6 +453,17 @@ func Run(ctx context.Context, options Options) error {
452453
}
453454
os.Setenv("HOME", user.HomeDir)
454455

456+
environ, err := os.ReadFile("/etc/environment")
457+
if err == nil {
458+
for _, env := range strings.Split(string(environ), "\n") {
459+
pair := strings.SplitN(env, "=", 2)
460+
if len(pair) != 2 {
461+
continue
462+
}
463+
os.Setenv(pair[0], pair[1])
464+
}
465+
}
466+
455467
// It must be set in this parent process otherwise nothing will be found!
456468
for _, env := range configFile.Config.Env {
457469
pair := strings.SplitN(env, "=", 2)
@@ -495,6 +507,11 @@ func Run(ctx context.Context, options Options) error {
495507
return fmt.Errorf("remove docker config: %w", err)
496508
}
497509

510+
err = os.MkdirAll(options.WorkspaceFolder, 0755)
511+
if err != nil {
512+
return fmt.Errorf("create workspace folder: %w", err)
513+
}
514+
498515
logf(codersdk.LogLevelInfo, "=== Running the init command %q as the %q user...", options.InitScript, user.Username)
499516
cmd := exec.CommandContext(ctx, "/bin/sh", "-c", options.InitScript)
500517
cmd.Env = os.Environ()
@@ -520,7 +537,13 @@ func Run(ctx context.Context, options Options) error {
520< 104D5 /code>537
cmd.Stderr = &buf
521538
}
522539

523-
return cmd.Run()
540+
err = cmd.Run()
541+
if err != nil {
542+
fmt.Printf("FAILED! %s\n", err)
543+
time.Sleep(time.Hour)
544+
return fmt.Errorf("run init script: %w", err)
545+
}
546+
return nil
524547
}
525548

526549
// DefaultWorkspaceFolder returns the default workspace folder

integration/integration_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,17 @@ func TestBuildCustomCertificates(t *testing.T) {
111111
require.Equal(t, "hello", strings.TrimSpace(output))
112112
}
113113

114+
func TestCloneFailsFallback(t *testing.T) {
115+
t.Parallel()
116+
t.Run("BadRepo", func(t *testing.T) {
117+
t.Parallel()
118+
_, err := runEnvbuilder(t, []string{
119+
"GIT_URL=bad-value",
120+
})
121+
require.ErrorContains(t, err, envbuilder.ErrNoFallbackImage.Error())
122+
})
123+
}
124+
114125
func TestBuildFailsFallback(t *testing.T) {
115126
t.Parallel()
116127
t.Run("BadDockerfile", func(t *testing.T) {

0 commit comments

Comments
 (0)
0