Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for build command overrides #1211

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion args.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ const (
// ArgAppDeployment is the deployment ID.
ArgAppDeployment = "deployment"
// ArgAppDevConfig is the path to the app dev link config.
ArgAppDevConfig = "appdev-config"
ArgAppDevConfig = "dev-config"
// ArgAppDevBuildCommand is an optional build command to set for local development.
ArgAppDevBuildCommand = "build-command"
// ArgAppLogFollow follow logs.
ArgAppLogFollow = "follow"
// ArgAppLogTail tail logs.
Expand Down
23 changes: 19 additions & 4 deletions commands/apps_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ func AppsDev() *Command {
"Additional environment variables to inject into the build.",
)

AddStringFlag(
build, doctl.ArgAppDevBuildCommand,
"", "",
"Optional build command override for local development.",
)

AddDurationFlag(
build, doctl.ArgTimeout,
"", 0,
Expand Down Expand Up @@ -222,6 +228,14 @@ func RunAppsDevBuild(c *CmdConfig) error {
if err != nil {
return err
}
if registryName == "" {
return errors.New("registry-name is required")
}

buildOverrride, err := conf.GetString(doctl.ArgAppDevBuildCommand)
if err != nil {
return err
}

if Interactive {
choice, err := confirm.New(
Expand Down Expand Up @@ -277,10 +291,11 @@ func RunAppsDevBuild(c *CmdConfig) error {
err = func() error {
defer cancel()
builder, err := c.componentBuilderFactory.NewComponentBuilder(cli, spec, builder.NewBuilderOpts{
Component: component,
Registry: registryName,
Envs: envs,
LogWriter: logWriter,
Component: component,
Registry: registryName,
EnvOverride: envs,
BuildCommandOverride: buildOverrride,
LogWriter: logWriter,
})
if err != nil {
return err
Expand Down
9 changes: 5 additions & 4 deletions commands/apps_dev_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,11 @@ type appDevConfig struct {
}

var validAppDevKeys = map[string]bool{
doctl.ArgApp: true,
doctl.ArgAppSpec: true,
doctl.ArgEnvFile: true,
doctl.ArgRegistryName: true,
doctl.ArgApp: true,
doctl.ArgAppSpec: true,
doctl.ArgEnvFile: true,
doctl.ArgRegistryName: true,
doctl.ArgAppDevBuildCommand: true,
}

func outputValidAppDevKeys() string {
Expand Down
26 changes: 15 additions & 11 deletions internal/apps/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ type ComponentBuilderResult struct {
}

type baseComponentBuilder struct {
cli ContainerEngineClient
spec *godo.AppSpec
component godo.AppBuildableComponentSpec
registry string
envOverrides map[string]string
cli ContainerEngineClient
spec *godo.AppSpec
component godo.AppBuildableComponentSpec
registry string
envOverrides map[string]string
buildCommandOverride string

logWriter io.Writer
}
Expand Down Expand Up @@ -105,10 +106,11 @@ func (b baseComponentBuilder) getEnvMap() map[string]string {

// NewBuilderOpts ...
type NewBuilderOpts struct {
Component string
Registry string
Envs map[string]string
LogWriter io.Writer
Component string
Registry string
EnvOverride map[string]string
BuildCommandOverride string
LogWriter io.Writer
}

// DefaultComponentBuilderFactory is the standard component builder factory.
Expand Down Expand Up @@ -137,7 +139,8 @@ func (f *DefaultComponentBuilderFactory) NewComponentBuilder(cli ContainerEngine
spec,
component,
opts.Registry,
opts.Envs,
opts.EnvOverride,
opts.BuildCommandOverride,
opts.LogWriter,
},
}, nil
Expand All @@ -149,7 +152,8 @@ func (f *DefaultComponentBuilderFactory) NewComponentBuilder(cli ContainerEngine
spec,
component,
opts.Registry,
opts.Envs,
opts.EnvOverride,
opts.BuildCommandOverride,
opts.LogWriter,
},
}, nil
Expand Down
10 changes: 9 additions & 1 deletion internal/apps/builder/cnb.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strings"
"time"

"github.com/MakeNowJust/heredoc"
"github.com/digitalocean/doctl/commands/charm"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
Expand Down Expand Up @@ -139,7 +141,13 @@ func (b *CNBComponentBuilder) cnbEnv() []string {
if b.component.GetSourceDir() != "" {
envs = append(envs, "SOURCE_DIR="+b.component.GetSourceDir())
}
if b.component.GetBuildCommand() != "" {

if b.buildCommandOverride != "" {
charm.TemplatePrint(heredoc.Doc(`
=> Overriding default build command with custom command: {{highlight .}}{{nl}}`,
), b.buildCommandOverride)
envs = append(envs, "BUILD_COMMAND="+b.buildCommandOverride)
} else if b.component.GetBuildCommand() != "" {
envs = append(envs, "BUILD_COMMAND="+b.component.GetBuildCommand())
}

Expand Down
12 changes: 11 additions & 1 deletion internal/apps/builder/cnb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ func TestCNBComponentBuild(t *testing.T) {
Key: "override-1",
Value: "newval",
},
{
Key: "useroverride-1",
Value: "newval",
},
{
Key: "run-build-arg-1",
Value: "run-build-val-1",
Expand Down Expand Up @@ -74,6 +78,10 @@ func TestCNBComponentBuild(t *testing.T) {
cli: mockClient,
spec: spec,
component: service,
envOverrides: map[string]string{
"useroverride-1": "newval",
},
buildCommandOverride: "custom build command",
},
}

Expand All @@ -94,10 +102,12 @@ func TestCNBComponentBuild(t *testing.T) {
Env: []string{
"APP_IMAGE_URL=" + builder.ImageOutputName(),
"APP_PLATFORM_COMPONENT_TYPE=" + string(service.GetType()),
appVarAllowListKey + "=build-arg-1,override-1,run-build-arg-1",
appVarAllowListKey + "=build-arg-1,override-1,run-build-arg-1,useroverride-1",
appVarPrefix + "build-arg-1=build-val-1",
appVarPrefix + "override-1=newval",
appVarPrefix + "run-build-arg-1=run-build-val-1",
appVarPrefix + "useroverride-1=newval",
"BUILD_COMMAND=" + builder.buildCommandOverride,
"SOURCE_DIR=" + service.GetSourceDir(),
},
Cmd: []string{"sh", "-c", "/.app_platform/build.sh"},
Expand Down
8 changes: 8 additions & 0 deletions internal/apps/builder/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"path/filepath"
"time"

"github.com/MakeNowJust/heredoc"
"github.com/digitalocean/doctl/commands/charm"
"github.com/docker/docker/api/types"
"github.com/docker/docker/pkg/archive"
)
Expand Down Expand Up @@ -37,6 +39,12 @@ func (b *DockerComponentBuilder) Build(ctx context.Context) (ComponentBuilderRes
return res, errors.New("no component was provided for the build")
}

if b.buildCommandOverride != "" {
charm.TemplatePrint(heredoc.Doc(`
{{warning "=> Build command overrides are ignored for Dockerfile based builds..."}}{{nl}}`,
), b.buildCommandOverride)
}

buildContext := filepath.Clean(b.component.GetSourceDir())
buildContext, err := filepath.Rel(".", buildContext)
if err != nil {
Expand Down