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 id to dockers and docker_manifests #2399

Merged
merged 3 commits into from Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions internal/exec/exec.go
Expand Up @@ -105,6 +105,8 @@ func filterArtifacts(artifacts artifact.Artifacts, publisher config.Publisher) [
artifact.ByType(artifact.UploadableFile),
artifact.ByType(artifact.LinuxPackage),
artifact.ByType(artifact.UploadableBinary),
artifact.ByType(artifact.DockerImage),
artifact.ByType(artifact.DockerManifest),
}

if publisher.Checksum {
Expand Down
45 changes: 44 additions & 1 deletion internal/exec/exec_test.go
Expand Up @@ -35,7 +35,6 @@ func TestExecute(t *testing.T) {
ext string
typ artifact.Type
}{
{"docker", "---", artifact.DockerImage},
{"debpkg", "deb", artifact.LinuxPackage},
{"binary", "bin", artifact.Binary},
{"archive", "tar", artifact.UploadableArchive},
Expand All @@ -57,6 +56,25 @@ func TestExecute(t *testing.T) {
})
}

ctx.Artifacts.Add(&artifact.Artifact{
Name: "foo/bar:amd64",
Goos: "linux",
Goarch: "amd64",
Path: "foo/bar:amd64",
Type: artifact.DockerImage,
Extra: map[string]interface{}{
"ID": "img",
},
})
ctx.Artifacts.Add(&artifact.Artifact{
Name: "foo/bar",
Path: "foo/bar",
Type: artifact.DockerManifest,
Extra: map[string]interface{}{
"ID": "mnf",
},
})

osEnv := func(ignores ...string) []string {
var result []string
outer:
Expand Down Expand Up @@ -108,6 +126,8 @@ func TestExecute(t *testing.T) {
{ExpectedArgs: []string{"a.deb"}, ExitCode: 0, ExpectedEnv: osEnv()},
{ExpectedArgs: []string{"a.ubi"}, ExitCode: 0, ExpectedEnv: osEnv()},
{ExpectedArgs: []string{"a.tar"}, ExitCode: 0, ExpectedEnv: osEnv()},
{ExpectedArgs: []string{"foo/bar"}, ExitCode: 0, ExpectedEnv: osEnv()},
{ExpectedArgs: []string{"foo/bar:amd64"}, ExitCode: 0, ExpectedEnv: osEnv()},
},
}),
},
Expand All @@ -129,6 +149,8 @@ func TestExecute(t *testing.T) {
{ExpectedArgs: []string{"a.ubi"}, ExitCode: 0, ExpectedEnv: osEnv()},
{ExpectedArgs: []string{"a.tar"}, ExitCode: 0, ExpectedEnv: osEnv()},
{ExpectedArgs: []string{"a.sum"}, ExitCode: 0, ExpectedEnv: osEnv()},
{ExpectedArgs: []string{"foo/bar"}, ExitCode: 0, ExpectedEnv: osEnv()},
{ExpectedArgs: []string{"foo/bar:amd64"}, ExitCode: 0, ExpectedEnv: osEnv()},
},
}),
},
Expand All @@ -150,6 +172,27 @@ func TestExecute(t *testing.T) {
{ExpectedArgs: []string{"a.ubi"}, ExitCode: 0, ExpectedEnv: osEnv()},
{ExpectedArgs: []string{"a.tar"}, ExitCode: 0, ExpectedEnv: osEnv()},
{ExpectedArgs: []string{"a.sig"}, ExitCode: 0, ExpectedEnv: osEnv()},
{ExpectedArgs: []string{"foo/bar"}, ExitCode: 0, ExpectedEnv: osEnv()},
{ExpectedArgs: []string{"foo/bar:amd64"}, ExitCode: 0, ExpectedEnv: osEnv()},
},
}),
},
},
},
nil,
},
{
"docker",
[]config.Publisher{
{
Name: "test",
IDs: []string{"img", "mnf"},
Cmd: MockCmd + " {{ .ArtifactName }}",
Env: []string{
MarshalMockEnv(&MockData{
AnyOf: []MockCall{
{ExpectedArgs: []string{"foo/bar"}, ExitCode: 0, ExpectedEnv: osEnv()},
{ExpectedArgs: []string{"foo/bar:amd64"}, ExitCode: 0, ExpectedEnv: osEnv()},
},
}),
},
Expand Down
16 changes: 13 additions & 3 deletions internal/pipe/docker/docker.go
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/deprecate"
"github.com/goreleaser/goreleaser/internal/gio"
"github.com/goreleaser/goreleaser/internal/ids"
"github.com/goreleaser/goreleaser/internal/pipe"
"github.com/goreleaser/goreleaser/internal/semerrgroup"
"github.com/goreleaser/goreleaser/internal/tmpl"
Expand All @@ -36,9 +37,13 @@ func (Pipe) String() string {

// Default sets the pipe defaults.
func (Pipe) Default(ctx *context.Context) error {
ids := ids.New("dockers")
for i := range ctx.Config.Dockers {
docker := &ctx.Config.Dockers[i]

if docker.ID != "" {
ids.Inc(docker.ID)
}
if docker.Goos == "" {
docker.Goos = "linux"
}
Expand Down Expand Up @@ -66,7 +71,7 @@ func (Pipe) Default(ctx *context.Context) error {
}
}
}
return nil
return ids.Validate()
}

func validateImager(use string) error {
Expand Down Expand Up @@ -238,13 +243,18 @@ func dockerPush(ctx *context.Context, image *artifact.Artifact) error {
if err := imagers[docker.Use].Push(ctx, image.Name, docker.PushFlags); err != nil {
return err
}
ctx.Artifacts.Add(&artifact.Artifact{
art := &artifact.Artifact{
Type: artifact.DockerImage,
Name: image.Name,
Path: image.Path,
Goarch: image.Goarch,
Goos: image.Goos,
Goarm: image.Goarm,
})
Extra: map[string]interface{}{},
}
if docker.ID != "" {
art.Extra["ID"] = docker.ID
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity why do this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the field used by the ids filters later...

}
ctx.Artifacts.Add(art)
return nil
}
21 changes: 21 additions & 0 deletions internal/pipe/docker/docker_test.go
Expand Up @@ -1136,6 +1136,27 @@ func TestDefault(t *testing.T) {
require.Equal(t, useDocker, ctx.Config.DockerManifests[1].Use)
}

func TestDefaultDuplicateID(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
Dockers: []config.Docker{
{ID: "foo"},
{ /* empty */ },
{ID: "bar"},
{ID: "foo"},
},
DockerManifests: []config.DockerManifest{
{ID: "bar"},
{ /* empty */ },
{ID: "bar"},
{ID: "foo"},
},
},
}
require.EqualError(t, Pipe{}.Default(ctx), "found 2 dockers with the ID 'foo', please fix your config")
require.EqualError(t, ManifestPipe{}.Default(ctx), "found 2 docker_manifests with the ID 'bar', please fix your config")
}

func TestDefaultInvalidUse(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
Expand Down
22 changes: 16 additions & 6 deletions internal/pipe/docker/manifest.go
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/apex/log"
"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/internal/tmpl"
Expand All @@ -24,16 +25,20 @@ func (ManifestPipe) String() string {

// Default sets the pipe defaults.
func (ManifestPipe) Default(ctx *context.Context) error {
ids := ids.New("docker_manifests")
for i := range ctx.Config.DockerManifests {
manifest := &ctx.Config.DockerManifests[i]
if manifest.ID != "" {
ids.Inc(manifest.ID)
}
if manifest.Use == "" {
manifest.Use = useDocker
}
if err := validateManifester(manifest.Use); err != nil {
return err
}
}
return nil
return ids.Validate()
}

// Publish the docker manifests.
Expand Down Expand Up @@ -69,11 +74,16 @@ func (ManifestPipe) Publish(ctx *context.Context) error {
if err := manifester.Create(ctx, name, images, manifest.CreateFlags); err != nil {
return err
}
ctx.Artifacts.Add(&artifact.Artifact{
Type: artifact.DockerManifest,
Name: name,
Path: name,
})
art := &artifact.Artifact{
Type: artifact.DockerManifest,
Name: name,
Path: name,
Extra: map[string]interface{}{},
}
if manifest.ID != "" {
art.Extra["ID"] = manifest.ID
}
ctx.Artifacts.Add(art)

log.WithField("manifest", name).Info("pushing docker manifest")
return manifester.Push(ctx, name, manifest.PushFlags)
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/config.go
Expand Up @@ -541,6 +541,7 @@ type Checksum struct {

// Docker image config.
type Docker struct {
ID string `yaml:"id,omitempty"`
IDs []string `yaml:"ids,omitempty"`
Goos string `yaml:",omitempty"`
Goarch string `yaml:",omitempty"`
Expand All @@ -557,6 +558,7 @@ type Docker struct {

// DockerManifest config.
type DockerManifest struct {
ID string `yaml:"id,omitempty"`
NameTemplate string `yaml:"name_template,omitempty"`
SkipPush string `yaml:"skip_push,omitempty"`
ImageTemplates []string `yaml:"image_templates,omitempty"`
Expand Down
3 changes: 3 additions & 0 deletions www/docs/customization/docker.md
Expand Up @@ -48,6 +48,9 @@ Of course, you can customize a lot of things:
dockers:
# You can have multiple Docker images.
-
# ID of the image, needed if you want to filter by it later on (e.g. on custom publishers).
id: myimg

# GOOS of the built binaries/packages that should be used.
goos: linux

Expand Down
3 changes: 3 additions & 0 deletions www/docs/customization/docker_manifest.md
Expand Up @@ -28,6 +28,9 @@ options available:
docker_manifests:
# You can have multiple Docker manifests.
-
# ID of the manifest, needed if you want to filter by it later on (e.g. on custom publishers).
id: myimg

# Name template for the manifest.
# Defaults to empty.
name_template: foo/bar:{{ .Version }}
Expand Down