Skip to content

Commit 02a1511

Browse files
committed
Add flag and PublishOption for destination repo
This enables programmatically setting the destination image repository when embedding ko's `publish` functionality in other tools. See ko-build#348
1 parent d498734 commit 02a1511

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

README.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,16 @@ to
6161

6262
## Choose Destination
6363

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.:
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.:
6768

6869
- `KO_DOCKER_REPO=gcr.io/my-project`, or
69-
- `KO_DOCKER_REPO=my-dockerhub-user`
70+
- `--docker-repo=my-dockerhub-user`
71+
72+
If both the flag and the environment variable are set, the flag value takes
73+
precedence.
7074

7175
# Build an Image
7276

pkg/commands/options/publish.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2018 Google LLC All Rights Reserved.
1+
// Copyright 2021 Google LLC All Rights Reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -25,6 +25,9 @@ import (
2525

2626
// PublishOptions encapsulates options when publishing.
2727
type PublishOptions struct {
28+
// DockerRepo overrides the KO_DOCKER_REPO environment variable, if present
29+
DockerRepo string
30+
2831
Tags []string
2932

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

4851
func AddPublishArg(cmd *cobra.Command, po *PublishOptions) {
52+
cmd.Flags().StringVar(&po.DockerRepo, "docker-repo", "", "Repository to push images, overrides KO_DOCKER_REPO")
53+
4954
cmd.Flags().StringSliceVarP(&po.Tags, "tags", "t", []string{"latest"},
5055
"Which tags to use for the produced image instead of the default 'latest' tag "+
5156
"(may not work properly with --base-import-paths or --bare).")

pkg/commands/resolver.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2018 Google LLC All Rights Reserved.
1+
// Copyright 2021 Google LLC All Rights Reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -132,6 +132,9 @@ func makePublisher(po *options.PublishOptions) (publish.Interface, error) {
132132
// to either a docker daemon or a container image registry.
133133
innerPublisher, err := func() (publish.Interface, error) {
134134
repoName := os.Getenv("KO_DOCKER_REPO")
135+
if po.DockerRepo != "" {
136+
repoName = po.DockerRepo
137+
}
135138
namer := options.MakeNamer(po)
136139
if repoName == publish.LocalDomain || po.Local {
137140
// TODO(jonjohnsonjr): I'm assuming that nobody will
@@ -144,7 +147,7 @@ func makePublisher(po *options.PublishOptions) (publish.Interface, error) {
144147
}
145148

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

0 commit comments

Comments
 (0)