Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add digest to artifacts info of published docker images #3540

Merged
merged 4 commits into from Nov 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion internal/artifact/artifact.go
Expand Up @@ -78,8 +78,10 @@ func (t Type) String() string {
return "Binary"
case LinuxPackage:
return "Linux Package"
case PublishableDockerImage, DockerImage:
case PublishableDockerImage:
return "Docker Image"
case DockerImage:
return "Published Docker Image"
case DockerManifest:
return "Docker Manifest"
case PublishableSnapcraft, Snapcraft:
Expand Down
30 changes: 29 additions & 1 deletion internal/pipe/docker/api.go
Expand Up @@ -34,7 +34,7 @@ func registerImager(use string, impl imager) {
// imager is something that can build and push docker images.
type imager interface {
Build(ctx *context.Context, root string, images, flags []string) error
Push(ctx *context.Context, image string, flags []string) error
Push(ctx *context.Context, image string, flags []string) (digest string, err error)
}

// manifester is something that can create and push docker manifests.
Expand Down Expand Up @@ -66,3 +66,31 @@ func runCommand(ctx *context.Context, dir, binary string, args ...string) error
}
return nil
}

func runCommandWithOutput(ctx *context.Context, dir, binary string, args ...string) ([]byte, error) {
fields := log.Fields{
"cmd": append([]string{binary}, args[0]),
"cwd": dir,
}

/* #nosec */
cmd := exec.CommandContext(ctx, binary, args...)
cmd.Dir = dir
cmd.Env = ctx.Env.Strings()

var b bytes.Buffer
w := gio.Safe(&b)
cmd.Stderr = io.MultiWriter(logext.NewWriter(fields, logext.Error), w)

log.WithFields(fields).WithField("args", args[1:]).Debug("running")
out, err := cmd.Output()
if out != nil {
// regardless of command success, always print stdout for backward-compatibility with runCommand()
io.MultiWriter(logext.NewWriter(fields, logext.Error), w).Write(out)
gal-legit marked this conversation as resolved.
Show resolved Hide resolved
}
if err != nil {
return nil, fmt.Errorf("%w: %s", err, b.String())
}

return out, nil
}
19 changes: 15 additions & 4 deletions internal/pipe/docker/api_docker.go
Expand Up @@ -2,6 +2,7 @@ package docker

import (
"fmt"
"regexp"

"github.com/goreleaser/goreleaser/pkg/context"
)
Expand Down Expand Up @@ -43,11 +44,21 @@ type dockerImager struct {
buildx bool
}

func (i dockerImager) Push(ctx *context.Context, image string, flags []string) error {
if err := runCommand(ctx, ".", "docker", "push", image); err != nil {
return fmt.Errorf("failed to push %s: %w", image, err)
var dockerDigestPattern = regexp.MustCompile("sha256:[a-z0-9]{64}")

func (i dockerImager) Push(ctx *context.Context, image string, flags []string) (digest string, err error) {
outBytes, err := runCommandWithOutput(ctx, ".", "docker", "push", image)
if err != nil {
return "", fmt.Errorf("failed to push %s: %w", image, err)
}
return nil

out := string(outBytes)
digest = dockerDigestPattern.FindString(out)
if digest == "" {
return "", fmt.Errorf("failed to find docker digest in docker push output: %v", out)
}

return digest, nil
}

func (i dockerImager) Build(ctx *context.Context, root string, images, flags []string) error {
Expand Down
6 changes: 5 additions & 1 deletion internal/pipe/docker/docker.go
Expand Up @@ -20,6 +20,7 @@ import (

const (
dockerConfigExtra = "DockerConfig"
dockerDigestExtra = "digest"

useBuildx = "buildx"
useDocker = "docker"
Expand Down Expand Up @@ -248,7 +249,8 @@ func dockerPush(ctx *context.Context, image *artifact.Artifact) error {
return pipe.Skip("prerelease detected with 'auto' push, skipping docker publish: " + image.Name)
}

if err := imagers[docker.Use].Push(ctx, image.Name, docker.PushFlags); err != nil {
digest, err := imagers[docker.Use].Push(ctx, image.Name, docker.PushFlags)
if err != nil {
return err
}

Expand All @@ -264,6 +266,8 @@ func dockerPush(ctx *context.Context, image *artifact.Artifact) error {
if docker.ID != "" {
art.Extra[artifact.ExtraID] = docker.ID
}
art.Extra[dockerDigestExtra] = digest

ctx.Artifacts.Add(art)
return nil
}
4 changes: 2 additions & 2 deletions internal/pipeline/pipeline.go
Expand Up @@ -108,10 +108,10 @@ var Pipeline = append(
scoop.Pipe{},
// create and push docker images
docker.Pipe{},
// creates a metadata.json and an artifacts.json files in the dist folder
metadata.Pipe{},
// publishes artifacts
publish.Pipe{},
// creates a metadata.json and an artifacts.json files in the dist folder
metadata.Pipe{},
// announce releases
announce.Pipe{},
)