Skip to content

Commit 6182fba

Browse files
committed
Set DockerRepo PublishOption from KO_DOCKER_REPO
This enables programmatically setting the destination image repository and avoids exposing a flag.
1 parent 02a1511 commit 6182fba

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

README.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,12 @@ to
6161

6262
## Choose Destination
6363

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

6968
- `KO_DOCKER_REPO=gcr.io/my-project`, or
70-
- `--docker-repo=my-dockerhub-user`
69+
- `KO_DOCKER_REPO=my-dockerhub-user`
7170

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

pkg/commands/options/publish.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package options
1717
import (
1818
"crypto/md5" //nolint: gosec // No strong cryptography needed.
1919
"encoding/hex"
20+
"os"
2021
"path"
2122

2223
"github.com/google/ko/pkg/publish"
@@ -49,7 +50,11 @@ type PublishOptions struct {
4950
}
5051

5152
func AddPublishArg(cmd *cobra.Command, po *PublishOptions) {
52-
cmd.Flags().StringVar(&po.DockerRepo, "docker-repo", "", "Repository to push images, overrides KO_DOCKER_REPO")
53+
// Set DockerRepo from the KO_DOCKER_REPO envionment variable.
54+
// See https://github.com/google/ko/pull/351 for flag discussion.
55+
if dockerRepo, exists := os.LookupEnv("KO_DOCKER_REPO"); exists {
56+
po.DockerRepo = dockerRepo
57+
}
5358

5459
cmd.Flags().StringSliceVarP(&po.Tags, "tags", "t", []string{"latest"},
5560
"Which tags to use for the produced image instead of the default 'latest' tag "+

pkg/commands/resolver.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,7 @@ func makePublisher(po *options.PublishOptions) (publish.Interface, error) {
131131
// Create the publish.Interface that we will use to publish image references
132132
// to either a docker daemon or a container image registry.
133133
innerPublisher, err := func() (publish.Interface, error) {
134-
repoName := os.Getenv("KO_DOCKER_REPO")
135-
if po.DockerRepo != "" {
136-
repoName = po.DockerRepo
137-
}
134+
repoName := po.DockerRepo
138135
namer := options.MakeNamer(po)
139136
if repoName == publish.LocalDomain || po.Local {
140137
// TODO(jonjohnsonjr): I'm assuming that nobody will
@@ -147,7 +144,7 @@ func makePublisher(po *options.PublishOptions) (publish.Interface, error) {
147144
}
148145

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

0 commit comments

Comments
 (0)