1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-08 03:31:59 +02:00
goreleaser/internal/pipe/sign/sign_docker.go
Carlos Alexandro Becker ad57a133fb
feat: sign docker images with cosign (#2423)
* feat: sign docker images with cosign

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>

* fix: improve sign logging

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>

* fix: do not sign if skip publish is set

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>

* fix: install cosign

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>

* docs: fix wrong docs

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
2021-08-24 11:22:09 -03:00

81 lines
1.9 KiB
Go

package sign
import (
"fmt"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/ids"
"github.com/goreleaser/goreleaser/internal/pipe"
"github.com/goreleaser/goreleaser/internal/semerrgroup"
"github.com/goreleaser/goreleaser/pkg/context"
)
// Pipe that signs docker images and manifests.
type DockerPipe struct{}
func (DockerPipe) String() string {
return "signing docker images"
}
// Default sets the Pipes defaults.
func (DockerPipe) Default(ctx *context.Context) error {
ids := ids.New("docker_signs")
for i := range ctx.Config.DockerSigns {
cfg := &ctx.Config.DockerSigns[i]
if cfg.Cmd == "" {
cfg.Cmd = "cosign"
}
if len(cfg.Args) == 0 {
cfg.Args = []string{"sign", "-key=cosign.key", "$artifact"}
}
if cfg.Artifacts == "" {
cfg.Artifacts = "none"
}
if cfg.ID == "" {
cfg.ID = "default"
}
ids.Inc(cfg.ID)
}
return ids.Validate()
}
// Run executes the Pipe.
func (DockerPipe) Run(ctx *context.Context) error {
if ctx.SkipSign {
return pipe.ErrSkipSignEnabled
}
if ctx.SkipPublish {
return pipe.ErrSkipSignEnabled
}
g := semerrgroup.New(ctx.Parallelism)
for i := range ctx.Config.DockerSigns {
cfg := ctx.Config.DockerSigns[i]
g.Go(func() error {
var filters []artifact.Filter
switch cfg.Artifacts {
case "images":
filters = append(filters, artifact.ByType(artifact.DockerImage))
case "manifests":
filters = append(filters, artifact.ByType(artifact.DockerManifest))
case "all":
filters = append(filters, artifact.Or(
artifact.ByType(artifact.DockerImage),
artifact.ByType(artifact.DockerManifest),
))
case "none":
return pipe.ErrSkipSignEnabled
default:
return fmt.Errorf("invalid list of artifacts to sign: %s", cfg.Artifacts)
}
if len(cfg.IDs) > 0 {
filters = append(filters, artifact.ByIDs(cfg.IDs...))
}
return sign(ctx, cfg, ctx.Artifacts.Filter(artifact.And(filters...)).List())
})
}
return g.Wait()
}