diff --git a/docs/content/en/docs/pipeline-stages/builders/ko.md b/docs/content/en/docs/pipeline-stages/builders/ko.md index c5969414676..0ba5656a6c6 100644 --- a/docs/content/en/docs/pipeline-stages/builders/ko.md +++ b/docs/content/en/docs/pipeline-stages/builders/ko.md @@ -213,6 +213,11 @@ Useful tips for existing `ko` users: [`default-repo` functionality]({{< relref "/docs/environment/image-registries" >}}). The ko builder does _not_ read the `KO_DOCKER_REPO` environment variable. +- Image naming follows the + [Skaffold image naming strategy]({{< relref "/docs/environment/image-registries" >}}). + Skaffold removes the `ko://` prefix, if present, before determining the image + name. + - The ko builder supports reading [base image configuration](https://github.com/google/ko#overriding-base-images) from the `.ko.yaml` file. If you already configure your base images using @@ -242,8 +247,6 @@ Useful tips for existing `ko` users: cat << EOF >> skaffold.yaml local: concurrency: 0 - tagPolicy: - sha256: {} deploy: kubectl: manifests: @@ -279,6 +282,23 @@ To achieve the same using Skaffold's ko builder, use the `flags` field in skaffold build ``` +#### Capturing image name from `stdout` + +If you want Skaffold to print out the full image name and digest (and nothing +else) to `stdout`, similar to what `ko build` does, use the +[`--quiet` and `--output` flags]({{< relref "/docs/references/cli#skaffold-build" >}}). +These flags enable you to capture the full image references in an environment +variable or redirect to a file, e.g.: + +```shell +skaffold build --quiet --output='{{range .Builds}}{{.Tag}}{{end}}' > out.txt +``` + +Note that Skaffold produces a JSON file with the image names if you run +`skaffold build` with the `--file-output` flag. You can then use this flag as +input to `skaffold render` to render Kubernetes manifests. For details on how +to do this, see the next section. + #### Rendering Kubernetes manifests When you use the Skaffold ko builder, Skaffold takes care of replacing the @@ -311,7 +331,7 @@ Or you can perform the action as two steps: first `build` the images, then ```shell skaffold build --file-output artifacts.json --push -skaffold render --build-artifacts artifacts.json --offline --output out.yaml +skaffold render --build-artifacts artifacts.json --digest-source none --offline --output out.yaml ``` Specify the location of your Kubernetes manifests in `skaffold.yaml`: diff --git a/pkg/skaffold/build/ko/build.go b/pkg/skaffold/build/ko/build.go index 7d826d9254f..6417bb0d521 100644 --- a/pkg/skaffold/build/ko/build.go +++ b/pkg/skaffold/build/ko/build.go @@ -52,7 +52,6 @@ func (b *Builder) Build(ctx context.Context, out io.Writer, a *latestV1.Artifact if err != nil { return "", fmt.Errorf("could not build and publish ko image %q: %w", a.ImageName, err) } - fmt.Fprintln(out, imageRef.Name()) return b.getImageIdentifier(ctx, imageRef, ref) } diff --git a/pkg/skaffold/build/ko/build_test.go b/pkg/skaffold/build/ko/build_test.go index 30c5c87b345..54a325cbd54 100644 --- a/pkg/skaffold/build/ko/build_test.go +++ b/pkg/skaffold/build/ko/build_test.go @@ -17,9 +17,7 @@ limitations under the License. package ko import ( - "bytes" "context" - "strings" "testing" "github.com/docker/docker/client" @@ -34,32 +32,32 @@ import ( ) // TestBuild doesn't actually build (or publish) any container images, because -// it's a unit test. Instead, it only verifies that the Build() function prints -// the image name to the out io.Writer and returns the image identifier. +// it's a unit test. Instead, it only verifies that the Build() returns the +// image identifier. func TestBuild(t *testing.T) { tests := []struct { description string pushImages bool - expectedRef string + imageRef string expectedImageIdentifier string }{ { description: "pushed image with tag", pushImages: true, - expectedRef: "registry.example.com/repo/image1:tag1", + imageRef: "registry.example.com/repo/image1:tag1", expectedImageIdentifier: "tag1", }, { description: "sideloaded image", pushImages: false, - expectedRef: "registry.example.com/repo/image2:any", + imageRef: "registry.example.com/repo/image2:any", expectedImageIdentifier: "ab737430e80b", }, } for _, test := range tests { testutil.Run(t, test.description, func(t *testutil.T) { importPath := "ko://github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/ko" // this package - b := stubKoArtifactBuilder(test.expectedRef, test.expectedImageIdentifier, test.pushImages, importPath) + b := stubKoArtifactBuilder(test.imageRef, test.expectedImageIdentifier, test.pushImages, importPath) artifact := &latestV1.Artifact{ ArtifactType: latestV1.ArtifactType{ @@ -67,12 +65,8 @@ func TestBuild(t *testing.T) { }, ImageName: importPath, } - var outBuffer bytes.Buffer - gotImageIdentifier, err := b.Build(context.Background(), &outBuffer, artifact, test.expectedRef) + gotImageIdentifier, err := b.Build(context.Background(), nil, artifact, test.imageRef) t.CheckNoError(err) - - imageNameOut := strings.TrimSuffix(outBuffer.String(), "\n") - t.CheckDeepEqual(test.expectedRef, imageNameOut) t.CheckDeepEqual(test.expectedImageIdentifier, gotImageIdentifier) }) }