Skip to content

Commit

Permalink
Add flag and PublishOption for destination repo
Browse files Browse the repository at this point in the history
This enables programmatically setting the destination image repository
when embedding ko's `publish` functionality in other tools.

See ko-build#348
  • Loading branch information
halvards committed Apr 29, 2021
1 parent d498734 commit 02a1511
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,16 @@ to

## Choose Destination

`ko` depends on an environment variable, `KO_DOCKER_REPO`, to identify where it
should push images that it builds. Typically this will be a remote registry,
e.g.:
`ko` must be configured with a destination for where it should push images that
it builds. You can configure this with either the `--docker-repo` flag or the
`KO_DOCKER_REPO` environment variable. Typically, the value will be a remote
registry, e.g.:

- `KO_DOCKER_REPO=gcr.io/my-project`, or
- `KO_DOCKER_REPO=my-dockerhub-user`
- `--docker-repo=my-dockerhub-user`

If both the flag and the environment variable are set, the flag value takes
precedence.

# Build an Image

Expand Down
7 changes: 6 additions & 1 deletion pkg/commands/options/publish.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018 Google LLC All Rights Reserved.
// Copyright 2021 Google LLC All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -25,6 +25,9 @@ import (

// PublishOptions encapsulates options when publishing.
type PublishOptions struct {
// DockerRepo overrides the KO_DOCKER_REPO environment variable, if present
DockerRepo string

Tags []string

// Push publishes images to a registry.
Expand All @@ -46,6 +49,8 @@ type PublishOptions struct {
}

func AddPublishArg(cmd *cobra.Command, po *PublishOptions) {
cmd.Flags().StringVar(&po.DockerRepo, "docker-repo", "", "Repository to push images, overrides KO_DOCKER_REPO")

cmd.Flags().StringSliceVarP(&po.Tags, "tags", "t", []string{"latest"},
"Which tags to use for the produced image instead of the default 'latest' tag "+
"(may not work properly with --base-import-paths or --bare).")
Expand Down
7 changes: 5 additions & 2 deletions pkg/commands/resolver.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018 Google LLC All Rights Reserved.
// Copyright 2021 Google LLC All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -132,6 +132,9 @@ func makePublisher(po *options.PublishOptions) (publish.Interface, error) {
// to either a docker daemon or a container image registry.
innerPublisher, err := func() (publish.Interface, error) {
repoName := os.Getenv("KO_DOCKER_REPO")
if po.DockerRepo != "" {
repoName = po.DockerRepo
}
namer := options.MakeNamer(po)
if repoName == publish.LocalDomain || po.Local {
// TODO(jonjohnsonjr): I'm assuming that nobody will
Expand All @@ -144,7 +147,7 @@ func makePublisher(po *options.PublishOptions) (publish.Interface, error) {
}

if repoName == "" {
return nil, errors.New("KO_DOCKER_REPO environment variable is unset")
return nil, errors.New("either --docker-repo flag or KO_DOCKER_REPO environment variable must be set")
}
if _, err := name.NewRegistry(repoName); err != nil {
if _, err := name.NewRepository(repoName); err != nil {
Expand Down

0 comments on commit 02a1511

Please sign in to comment.