From 78d85a7b4fd2e1b08220de65545c9b9a0d4003a5 Mon Sep 17 00:00:00 2001 From: Jason Hall Date: Mon, 11 Sep 2023 14:12:53 -0400 Subject: [PATCH] include example using go packages (#1145) Signed-off-by: Jason Hall --- docs/advanced/go-packages.md | 55 ++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/docs/advanced/go-packages.md b/docs/advanced/go-packages.md index 3982b2a101..fc5fdba8d8 100644 --- a/docs/advanced/go-packages.md +++ b/docs/advanced/go-packages.md @@ -4,3 +4,58 @@ To build an image, use [`pkg/build`](https://pkg.go.dev/github.com/google/ko/pkg/build), and publish it with [`pkg/publish`](https://pkg.go.dev/github.com/google/ko/pkg/publish). +This is a minimal example of using the packages together, to implement the core subset of `ko`'s functionality: + +```go +package main + +import ( + "context" + "fmt" + "log" + + "github.com/google/go-containerregistry/pkg/authn" + "github.com/google/go-containerregistry/pkg/name" + "github.com/google/go-containerregistry/pkg/v1/remote" + "github.com/google/ko/pkg/build" + "github.com/google/ko/pkg/publish" +) + +const ( + baseImage = "cgr.dev/chainguard/static:latest" + targetRepo = "example.registry/my-repo" + importpath = "github.com/my-org/miniko" + commitSHA = "deadbeef" +) + +func main() { + ctx := context.Background() + + b, err := build.NewGo(ctx, ".", + build.WithPlatforms("linux/amd64"), // only build for these platforms. + build.WithBaseImages(func(ctx context.Context, _ string) (name.Reference, build.Result, error) { + ref := name.MustParseReference(baseImage) + base, err := remote.Index(ref, remote.WithContext(ctx)) + return ref, base, err + })) + if err != nil { + log.Fatalf("NewGo: %v", err) + } + r, err := b.Build(ctx, importpath) + if err != nil { + log.Fatalf("Build: %v", err) + } + + p, err := publish.NewDefault(targetRepo, // publish to example.registry/my-repo + publish.WithTags([]string{commitSHA}), // tag with :deadbeef + publish.WithAuthFromKeychain(authn.DefaultKeychain)) // use credentials from ~/.docker/config.json + if err != nil { + log.Fatalf("NewDefault: %v", err) + } + ref, err := p.Publish(ctx, r, importpath) + if err != nil { + log.Fatalf("Publish: %v", err) + } + fmt.Println(ref.String()) +} +```