Skip to content

Commit

Permalink
feat: allow exiting on build failure with fallback
Browse files Browse the repository at this point in the history
This adds support for exiting envbuilder on build failure
even when a `FALLBACK_IMAGE` is specified.
  • Loading branch information
kylecarbs committed Oct 2, 2023
1 parent 8cd6906 commit 2156e37
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
8 changes: 7 additions & 1 deletion envbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ type Options struct {
// a Dockerfile is not found.
FallbackImage string `env:"FALLBACK_IMAGE"`

// ExitOnBuildFailure terminates the container upon a build failure.
// This is handy when preferring the `FALLBACK_IMAGE` in cases where
// no devcontainer.json or image is provided. However, it ensures
// that the container stops if the build process encounters an error.
ExitOnBuildFailure bool `env:"EXIT_ON_BUILD_FAILURE"`

// ForceSafe ignores any filesystem safety checks.
// This could cause serious harm to your system!
// This is used in cases where bypass is needed
Expand Down Expand Up @@ -633,7 +639,7 @@ func Run(ctx context.Context, options Options) error {
case strings.Contains(err.Error(), "unexpected status code 401 Unauthorized"):
logf(codersdk.LogLevelError, "Unable to pull the provided image. Ensure your registry credentials are correct!")
}
if !fallback {
if !fallback || options.ExitOnBuildFailure {
return err
}
logf(codersdk.LogLevelError, "Failed to build: %s", err)
Expand Down
17 changes: 17 additions & 0 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,23 @@ RUN exit 1`,
})
}

func TestExitBuildOnFailure(t *testing.T) {
t.Parallel()
url := createGitServer(t, gitServerOptions{
files: map[string]string{
"Dockerfile": "bad syntax",
},
})
_, err := runEnvbuilder(t, options{env: []string{
"GIT_URL=" + url,
"DOCKERFILE_PATH=Dockerfile",
"FALLBACK_IMAGE=alpine",
// Ensures that the fallback doesn't work when an image is specified.
"EXIT_ON_BUILD_FAILURE=true",
}})
require.ErrorContains(t, err, "parsing dockerfile")
}

func TestPrivateRegistry(t *testing.T) {
t.Parallel()
t.Run("NoAuth", func(t *testing.T) {
Expand Down

0 comments on commit 2156e37

Please sign in to comment.