diff --git a/internal/pipe/ko/ko.go b/internal/pipe/ko/ko.go index 146a76a2d..4f45458d3 100644 --- a/internal/pipe/ko/ko.go +++ b/internal/pipe/ko/ko.go @@ -138,6 +138,15 @@ func (Pipe) Default(ctx *context.Context) error { return ids.Validate() } +func (p Pipe) Run(ctx *context.Context) error { + if ctx.Snapshot { + // publish actually handles pushing to the local docker daemon when + // snapshot is true. + return p.Publish(ctx) + } + return nil +} + // Publish executes the Pipe. func (Pipe) Publish(ctx *context.Context) error { g := semerrgroup.New(ctx.Parallelism) @@ -249,17 +258,32 @@ func doBuild(ctx *context.Context, ko config.Ko) func() error { return fmt.Errorf("build: %w", err) } - po := []publish.Option{publish.WithTags(opts.tags), publish.WithNamer(options.MakeNamer(&options.PublishOptions{ + po := &options.PublishOptions{ DockerRepo: opts.imageRepo, Bare: opts.bare, PreserveImportPaths: opts.preserveImportPaths, BaseImportPaths: opts.baseImportPaths, Tags: opts.tags, - })), publish.WithAuthFromKeychain(keychain)} - - p, err := publish.NewDefault(opts.imageRepo, po...) + Local: ctx.Snapshot, + LocalDomain: "goreleaser.ko.local", + } + var p publish.Interface + if ctx.Snapshot { + p, err = publish.NewDaemon( + options.MakeNamer(po), + opts.tags, + publish.WithLocalDomain(po.LocalDomain), + ) + } else { + p, err = publish.NewDefault( + opts.imageRepo, + publish.WithTags(opts.tags), + publish.WithNamer(options.MakeNamer(po)), + publish.WithAuthFromKeychain(keychain), + ) + } if err != nil { - return fmt.Errorf("newDefault: %w", err) + return fmt.Errorf("newPublisher: %w", err) } defer func() { _ = p.Close() }() ref, err := p.Publish(ctx, r, opts.importPath) diff --git a/internal/pipe/ko/ko_test.go b/internal/pipe/ko/ko_test.go index 9a1bd2d2e..1f449a3c9 100644 --- a/internal/pipe/ko/ko_test.go +++ b/internal/pipe/ko/ko_test.go @@ -377,6 +377,41 @@ func TestPublishPipeSuccess(t *testing.T) { } } +func TestSnapshot(t *testing.T) { + ctx := testctx.NewWithCfg(config.Project{ + ProjectName: "test", + Builds: []config.Build{ + { + ID: "foo", + BuildDetails: config.BuildDetails{ + Ldflags: []string{"-s", "-w"}, + Flags: []string{"-tags", "netgo"}, + Env: []string{"GOCACHE=" + t.TempDir()}, + }, + }, + }, + Kos: []config.Ko{ + { + ID: "default", + Build: "foo", + Repository: "testimage", + WorkingDir: "./testdata/app/", + Tags: []string{"latest"}, + }, + }, + }, testctx.WithVersion("1.2.0"), testctx.Snapshot) + + require.NoError(t, Pipe{}.Default(ctx)) + require.NoError(t, Pipe{}.Run(ctx)) + + manifests := ctx.Artifacts.Filter(artifact.ByType(artifact.DockerManifest)).List() + require.Len(t, manifests, 1) + require.NotEmpty(t, manifests[0].Name) + require.Equal(t, manifests[0].Name, manifests[0].Path) + require.NotEmpty(t, manifests[0].Extra[artifact.ExtraDigest]) + require.Equal(t, "default", manifests[0].Extra[artifact.ExtraID]) +} + func TestKoValidateMainPathIssue4382(t *testing.T) { // testing the validation of the main path directly to cover many cases require.NoError(t, validateMainPath("")) diff --git a/internal/pipeline/pipeline.go b/internal/pipeline/pipeline.go index 2fe8aac12..2e3a356fa 100644 --- a/internal/pipeline/pipeline.go +++ b/internal/pipeline/pipeline.go @@ -20,6 +20,7 @@ import ( "github.com/goreleaser/goreleaser/v2/internal/pipe/env" "github.com/goreleaser/goreleaser/v2/internal/pipe/git" "github.com/goreleaser/goreleaser/v2/internal/pipe/gomod" + "github.com/goreleaser/goreleaser/v2/internal/pipe/ko" "github.com/goreleaser/goreleaser/v2/internal/pipe/krew" "github.com/goreleaser/goreleaser/v2/internal/pipe/metadata" "github.com/goreleaser/goreleaser/v2/internal/pipe/nfpm" @@ -146,6 +147,8 @@ var Pipeline = append( reportsizes.Pipe{}, // create and push docker images docker.Pipe{}, + // create and push docker images using ko + ko.Pipe{}, // publishes artifacts publish.New(), // creates a artifacts.json files in the dist directory diff --git a/www/docs/customization/ko.md b/www/docs/customization/ko.md index ee584a050..b6d9f95d7 100644 --- a/www/docs/customization/ko.md +++ b/www/docs/customization/ko.md @@ -133,7 +133,7 @@ builds: - linux goarch: - amd64 - - arch64 + - arm64 kos: - repository: ghcr.io/caarlos0/test-ko