diff --git a/pkg/skaffold/build/ko/build.go b/pkg/skaffold/build/ko/build.go index 6417bb0d521..9e2812e3d79 100644 --- a/pkg/skaffold/build/ko/build.go +++ b/pkg/skaffold/build/ko/build.go @@ -37,6 +37,9 @@ import ( // identifier. The image identifier is the tag or digest for pushed images, or // the docker image ID for sideloaded images. func (b *Builder) Build(ctx context.Context, out io.Writer, a *latestV1.Artifact, ref string) (string, error) { + if b.pushImages && strings.HasPrefix(ref, build.StrictScheme) { + return "", fmt.Errorf("default repo must be set when using the 'ko://' prefix and pushing to a registry: %s, see https://skaffold.dev/docs/environment/image-registries/", a.ImageName) + } koBuilder, err := b.newKoBuilder(ctx, a) if err != nil { return "", fmt.Errorf("error creating ko builder: %w", err) diff --git a/pkg/skaffold/build/ko/build_test.go b/pkg/skaffold/build/ko/build_test.go index 54a325cbd54..8eae89a7b92 100644 --- a/pkg/skaffold/build/ko/build_test.go +++ b/pkg/skaffold/build/ko/build_test.go @@ -40,6 +40,7 @@ func TestBuild(t *testing.T) { pushImages bool imageRef string expectedImageIdentifier string + shouldErr bool }{ { description: "pushed image with tag", @@ -53,6 +54,12 @@ func TestBuild(t *testing.T) { imageRef: "registry.example.com/repo/image2:any", expectedImageIdentifier: "ab737430e80b", }, + { + description: "error for missing default repo when using ko:// prefix combined with pushing image to a registry", + pushImages: true, + imageRef: "ko://github.com/google/ko", + shouldErr: true, + }, } for _, test := range tests { testutil.Run(t, test.description, func(t *testutil.T) { @@ -66,7 +73,7 @@ func TestBuild(t *testing.T) { ImageName: importPath, } gotImageIdentifier, err := b.Build(context.Background(), nil, artifact, test.imageRef) - t.CheckNoError(err) + t.CheckErrorAndFailNow(test.shouldErr, err) t.CheckDeepEqual(test.expectedImageIdentifier, gotImageIdentifier) }) }