Skip to content

Commit

Permalink
feat: docker builds filter (#1275)
Browse files Browse the repository at this point in the history
* feat: docker builds filter

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

* docs: docker builds filter

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

* test: fixed

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

* fix: tests

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
  • Loading branch information
caarlos0 committed Dec 27, 2019
1 parent c95a9c8 commit 6edf669
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 16 deletions.
37 changes: 22 additions & 15 deletions internal/pipe/docker/docker.go
Expand Up @@ -55,6 +55,11 @@ func (Pipe) Default(ctx *context.Context) error {
ctx.Config.Builds[0].Binary,
}
}
if len(ctx.Config.Dockers[0].Builds) == 0 {
ctx.Config.Dockers[0].Builds = []string{
ctx.Config.Builds[0].ID,
}
}
if ctx.Config.Dockers[0].Dockerfile == "" {
ctx.Config.Dockers[0].Dockerfile = "Dockerfile"
}
Expand Down Expand Up @@ -98,22 +103,24 @@ func doRun(ctx *context.Context) error {
}
binaryNames[i] = bin
}
var binaries = ctx.Artifacts.Filter(
artifact.And(
artifact.ByGoos(docker.Goos),
artifact.ByGoarch(docker.Goarch),
artifact.ByGoarm(docker.Goarm),
artifact.ByType(artifact.Binary),
func(a *artifact.Artifact) bool {
for _, bin := range binaryNames {
if a.ExtraOr("Binary", "").(string) == bin {
return true
}
var filters = []artifact.Filter{
artifact.ByGoos(docker.Goos),
artifact.ByGoarch(docker.Goarch),
artifact.ByGoarm(docker.Goarm),
artifact.ByType(artifact.Binary),
func(a *artifact.Artifact) bool {
for _, bin := range binaryNames {
if a.ExtraOr("Binary", "").(string) == bin {
return true
}
return false
},
),
).List()
}
return false
},
}
if len(docker.Builds) > 0 {
filters = append(filters, artifact.ByIDs(docker.Builds...))
}
var binaries = ctx.Artifacts.Filter(artifact.And(filters...)).List()
// TODO: not so good of a check, if one binary match multiple
// binaries and the other match none, this will still pass...
if len(binaries) != len(docker.Binaries) {
Expand Down
35 changes: 34 additions & 1 deletion internal/pipe/docker/docker_test.go
Expand Up @@ -153,6 +153,26 @@ func TestRunPipe(t *testing.T) {
assertError: shouldNotErr,
pubAssertError: shouldNotErr,
},
"valid-with-builds": {
dockers: []config.Docker{
{
ImageTemplates: []string{
registry + "goreleaser/test_run_pipe_build:latest",
},
Goos: "linux",
Goarch: "amd64",
Dockerfile: "testdata/Dockerfile",
Binaries: []string{"mybin"},
Builds: []string{"mybin"},
},
},
expect: []string{
registry + "goreleaser/test_run_pipe_build:latest",
},
assertImageLabels: noLabels,
assertError: shouldNotErr,
pubAssertError: shouldNotErr,
},
"multiple images with same extra file": {
dockers: []config.Docker{
{
Expand Down Expand Up @@ -583,6 +603,7 @@ func TestRunPipe(t *testing.T) {
Type: artifact.Binary,
Extra: map[string]interface{}{
"Binary": bin,
"ID": bin,
},
})
}
Expand Down Expand Up @@ -707,6 +728,11 @@ func TestDefault(t *testing.T) {
func TestDefaultBinaries(t *testing.T) {
var ctx = &context.Context{
Config: config.Project{
Builds: []config.Build{
{
ID: "foo",
},
},
Dockers: []config.Docker{
{
Binaries: []string{"foo"},
Expand All @@ -715,7 +741,7 @@ func TestDefaultBinaries(t *testing.T) {
},
}
assert.NoError(t, Pipe{}.Default(ctx))
assert.Len(t, ctx.Config.Dockers, 1)
require.Len(t, ctx.Config.Dockers, 1)
var docker = ctx.Config.Dockers[0]
assert.Equal(t, "linux", docker.Goos)
assert.Equal(t, "amd64", docker.Goarch)
Expand Down Expand Up @@ -765,6 +791,7 @@ func TestDefaultSet(t *testing.T) {
Config: config.Project{
Dockers: []config.Docker{
{
Builds: []string{"foo"},
Goos: "windows",
Goarch: "i386",
Binaries: []string{"bar"},
Expand All @@ -779,12 +806,18 @@ func TestDefaultSet(t *testing.T) {
assert.Equal(t, "windows", docker.Goos)
assert.Equal(t, "i386", docker.Goarch)
assert.Equal(t, "bar", docker.Binaries[0])
assert.Equal(t, "foo", docker.Builds[0])
assert.Equal(t, "Dockerfile.foo", docker.Dockerfile)
}

func Test_processImageTemplates(t *testing.T) {
var ctx = &context.Context{
Config: config.Project{
Builds: []config.Build{
{
ID: "default",
},
},
Dockers: []config.Docker{
{
Binaries: []string{"foo"},
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Expand Up @@ -276,6 +276,7 @@ type Checksum struct {
// Docker image config
type Docker struct {
Binaries []string `yaml:",omitempty"`
Builds []string `yaml:",omitempty"`
Goos string `yaml:",omitempty"`
Goarch string `yaml:",omitempty"`
Goarm string `yaml:",omitempty"`
Expand Down
12 changes: 12 additions & 0 deletions www/content/docker.md
Expand Up @@ -50,33 +50,45 @@ dockers:
-
# GOOS of the built binary that should be used.
goos: linux

# GOARCH of the built binary that should be used.
goarch: amd64

# GOARM of the built binary that should be used.
goarm: ''

# Name templates of the built binaries that should be used.
binaries:
- mybinary

# Build IDs to gather the binaries from.
builds:
- mybuild

# Templates of the Docker image names.
image_templates:
- "myuser/myimage:latest"
- "myuser/myimage:{{ .Tag }}"
- "myuser/myimage:{{ .Tag }}-{{ .Env.GO_VERSION }}"
- "myuser/myimage:v{{ .Major }}"
- "gcr.io/myuser/myimage:latest"

# Skips the docker push. Could be useful if you also do draft releases.
# If set to auto, the release will not be pushed to the docker repository
# in case there is an indicator for prerelease in the tag e.g. v1.0.0-rc1
# Defaults to false.
skip_push: false

# Path to the Dockerfile (from the project root).
dockerfile: Dockerfile

# Template of the docker build flags.
build_flag_templates:
- "--label=org.label-schema.schema-version=1.0"
- "--label=org.label-schema.version={{.Version}}"
- "--label=org.label-schema.name={{.ProjectName}}"
- "--build-arg=FOO={{.Env.Bar}}"

# If your Dockerfile copies files other than the binary itself,
# you should list them here as well.
# Note that goreleaser will create the same structure inside the temporary
Expand Down

0 comments on commit 6edf669

Please sign in to comment.