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 --all option to build command #908

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions docs/reference/ko_build.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ ko build IMPORTPATH... [flags]
### Options

```
--all When given, build all configured targets.
--bare Whether to just use KO_DOCKER_REPO without additional context (may not work properly with --tags).
-B, --base-import-paths Whether to use the base path without MD5 hash after KO_DOCKER_REPO (may not work properly with --tags).
--disable-optimizations Disable optimizations when building Go code. Useful when you want to interactively debug the created container.
Expand Down
22 changes: 17 additions & 5 deletions pkg/commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,30 @@ func addBuild(topLevel *cobra.Command) {
return fmt.Errorf("validating options: %w", err)
}

if len(args) == 0 {
// Build the current directory by default.
args = []string{"."}
}

ctx := cmd.Context()

bo.InsecureRegistry = po.InsecureRegistry
builder, err := makeBuilder(ctx, bo)
if err != nil {
return fmt.Errorf("error creating builder: %w", err)
}

if bo.BuildAll {
if len(args) != 0 {
return fmt.Errorf("no paths should be given when --all is set")
}
if len(bo.BuildConfigs) == 0 {
return fmt.Errorf("--all is set, but found no build configurations")
}

for _, cfg := range bo.BuildConfigs {
args = append(args, cfg.Main)
}
} else if len(args) == 0 {
// Build the current directory by default.
args = []string{"."}
}

publisher, err := makePublisher(po)
if err != nil {
return fmt.Errorf("error creating publisher: %w", err)
Expand Down
4 changes: 4 additions & 0 deletions pkg/commands/options/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ type BuildOptions struct {
// `AddBuildOptions()` defaults this field to `true`.
Trimpath bool

BuildAll bool

// BuildConfigs stores the per-image build config from `.ko.yaml`.
BuildConfigs map[string]build.Config
}
Expand All @@ -86,6 +88,8 @@ func AddBuildOptions(cmd *cobra.Command, bo *BuildOptions) {
"Which platform to use when pulling a multi-platform base. Format: all | <os>[/<arch>[/<variant>]][,platform]*")
cmd.Flags().StringSliceVar(&bo.Labels, "image-label", []string{},
"Which labels (key=value) to add to the image.")
cmd.Flags().BoolVar(&bo.BuildAll, "all", false,
"When given, build all configured targets.")
bo.Trimpath = true
}

Expand Down