Skip to content

Commit

Permalink
fix: allow multiple images with the same dockerfile
Browse files Browse the repository at this point in the history
closes #673
  • Loading branch information
caarlos0 committed May 23, 2018
1 parent 333da09 commit f5554bc
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 120 deletions.
9 changes: 5 additions & 4 deletions pipeline/docker/docker.go
Expand Up @@ -82,8 +82,9 @@ func (Pipe) Run(ctx *context.Context) error {
func doRun(ctx *context.Context) error {
var g errgroup.Group
sem := make(chan bool, ctx.Parallelism)
for _, docker := range ctx.Config.Dockers {
for i, docker := range ctx.Config.Dockers {
docker := docker
seed := i
sem <- true
g.Go(func() error {
defer func() {
Expand All @@ -105,7 +106,7 @@ func doRun(ctx *context.Context) error {
log.Warnf("no binaries found for %s", docker.Binary)
}
for _, binary := range binaries {
if err := process(ctx, docker, binary); err != nil {
if err := process(ctx, docker, binary, seed); err != nil {
return err
}
}
Expand Down Expand Up @@ -146,9 +147,9 @@ func tagName(ctx *context.Context, tagTemplate string) (string, error) {
return out.String(), err
}

func process(ctx *context.Context, docker config.Docker, artifact artifact.Artifact) error {
func process(ctx *context.Context, docker config.Docker, artifact artifact.Artifact, seed int) error {
var root = filepath.Dir(artifact.Path)
var dockerfile = filepath.Join(root, filepath.Base(docker.Dockerfile))
var dockerfile = filepath.Join(root, filepath.Base(docker.Dockerfile)) + fmt.Sprintf(".%d", seed)
var images []string
for _, tagTemplate := range docker.TagTemplates {
tag, err := tagName(ctx, tagTemplate)
Expand Down
278 changes: 162 additions & 116 deletions pipeline/docker/docker_test.go
Expand Up @@ -61,28 +61,30 @@ func TestRunPipe(t *testing.T) {
}

var table = map[string]struct {
docker config.Docker
dockers []config.Docker
publish bool
expect []string
assertError errChecker
}{
"valid": {
publish: true,
docker: config.Docker{
Image: registry + "goreleaser/test_run_pipe",
Goos: "linux",
Goarch: "amd64",
Dockerfile: "testdata/Dockerfile",
Binary: "mybin",
TagTemplates: []string{
"{{.Tag}}-{{.Env.FOO}}",
"v{{.Major}}",
"v{{.Major}}.{{.Minor}}",
"commint-{{.Commit}}",
"latest",
},
Files: []string{
"testdata/extra_file.txt",
dockers: []config.Docker{
{
Image: registry + "goreleaser/test_run_pipe",
Goos: "linux",
Goarch: "amd64",
Dockerfile: "testdata/Dockerfile",
Binary: "mybin",
TagTemplates: []string{
"{{.Tag}}-{{.Env.FOO}}",
"v{{.Major}}",
"v{{.Major}}.{{.Minor}}",
"commint-{{.Commit}}",
"latest",
},
Files: []string{
"testdata/extra_file.txt",
},
},
},
expect: []string{
Expand All @@ -93,23 +95,51 @@ func TestRunPipe(t *testing.T) {
},
assertError: shouldNotErr,
},
"valid_skip_push": {
"multiple images with same dockerfile": {
publish: true,
docker: config.Docker{
Image: registry + "goreleaser/test_run_pipe",
Goos: "linux",
Goarch: "amd64",
Dockerfile: "testdata/Dockerfile",
Binary: "mybin",
SkipPush: true,
TagTemplates: []string{
"{{.Tag}}-{{.Env.FOO}}",
"v{{.Major}}",
"v{{.Major}}.{{.Minor}}",
"latest",
dockers: []config.Docker{
{
Image: registry + "goreleaser/test_run_pipe",
Goos: "linux",
Goarch: "amd64",
Dockerfile: "testdata/Dockerfile",
Binary: "mybin",
TagTemplates: []string{"latest"},
},
Files: []string{
"testdata/extra_file.txt",
{
Image: registry + "goreleaser/test_run_pipe2",
Goos: "linux",
Goarch: "amd64",
Dockerfile: "testdata/Dockerfile",
Binary: "mybin",
TagTemplates: []string{"latest"},
},
},
expect: []string{
registry + "goreleaser/test_run_pipe:latest",
registry + "goreleaser/test_run_pipe2:latest",
},
assertError: shouldNotErr,
},
"valid_skip_push": {
publish: true,
dockers: []config.Docker{
{
Image: registry + "goreleaser/test_run_pipe",
Goos: "linux",
Goarch: "amd64",
Dockerfile: "testdata/Dockerfile",
Binary: "mybin",
SkipPush: true,
TagTemplates: []string{
"{{.Tag}}-{{.Env.FOO}}",
"v{{.Major}}",
"v{{.Major}}.{{.Minor}}",
"latest",
},
Files: []string{
"testdata/extra_file.txt",
},
},
},
expect: []string{
Expand All @@ -122,17 +152,19 @@ func TestRunPipe(t *testing.T) {
},
"valid_no_latest": {
publish: true,
docker: config.Docker{
Image: registry + "goreleaser/test_run_pipe",
Goos: "linux",
Goarch: "amd64",
Dockerfile: "testdata/Dockerfile",
Binary: "mybin",
TagTemplates: []string{
"{{.Version}}",
},
Files: []string{
"testdata/extra_file.txt",
dockers: []config.Docker{
{
Image: registry + "goreleaser/test_run_pipe",
Goos: "linux",
Goarch: "amd64",
Dockerfile: "testdata/Dockerfile",
Binary: "mybin",
TagTemplates: []string{
"{{.Version}}",
},
Files: []string{
"testdata/extra_file.txt",
},
},
},
expect: []string{
Expand All @@ -142,18 +174,20 @@ func TestRunPipe(t *testing.T) {
},
"valid_dont_publish": {
publish: false,
docker: config.Docker{
Image: registry + "goreleaser/test_run_pipe",
Goos: "linux",
Goarch: "amd64",
Dockerfile: "testdata/Dockerfile",
Binary: "mybin",
TagTemplates: []string{
"{{.Tag}}-{{.Env.FOO}}",
"latest",
},
Files: []string{
"testdata/extra_file.txt",
dockers: []config.Docker{
{
Image: registry + "goreleaser/test_run_pipe",
Goos: "linux",
Goarch: "amd64",
Dockerfile: "testdata/Dockerfile",
Binary: "mybin",
TagTemplates: []string{
"{{.Tag}}-{{.Env.FOO}}",
"latest",
},
Files: []string{
"testdata/extra_file.txt",
},
},
},
expect: []string{
Expand All @@ -164,59 +198,67 @@ func TestRunPipe(t *testing.T) {
},
"bad_dockerfile": {
publish: true,
docker: config.Docker{
Image: registry + "goreleaser/test_run_pipe",
Goos: "linux",
Goarch: "amd64",
Dockerfile: "testdata/Dockerfile.bad",
Binary: "mybin",
TagTemplates: []string{
"{{.Version}}",
dockers: []config.Docker{
{
Image: registry + "goreleaser/test_run_pipe",
Goos: "linux",
Goarch: "amd64",
Dockerfile: "testdata/Dockerfile.bad",
Binary: "mybin",
TagTemplates: []string{
"{{.Version}}",
},
},
},
assertError: shouldErr("pull access denied for nope, repository does not exist"),
},
"template_error": {
publish: true,
docker: config.Docker{
Image: registry + "goreleaser/test_run_pipe",
Goos: "linux",
Goarch: "amd64",
Dockerfile: "testdata/Dockerfile",
Binary: "mybin",
TagTemplates: []string{
"{{.Tag}",
dockers: []config.Docker{
{
Image: registry + "goreleaser/test_run_pipe",
Goos: "linux",
Goarch: "amd64",
Dockerfile: "testdata/Dockerfile",
Binary: "mybin",
TagTemplates: []string{
"{{.Tag}",
},
},
},
assertError: shouldErr(`template: tag:1: unexpected "}" in operand`),
},
"missing_env_on_template": {
publish: true,
docker: config.Docker{
Image: registry + "goreleaser/test_run_pipe",
Goos: "linux",
Goarch: "amd64",
Dockerfile: "testdata/Dockerfile",
Binary: "mybin",
TagTemplates: []string{
"{{.Env.NOPE}}",
dockers: []config.Docker{
{
Image: registry + "goreleaser/test_run_pipe",
Goos: "linux",
Goarch: "amd64",
Dockerfile: "testdata/Dockerfile",
Binary: "mybin",
TagTemplates: []string{
"{{.Env.NOPE}}",
},
},
},
assertError: shouldErr(`template: tag:1:6: executing "tag" at <.Env.NOPE>: map has no entry for key "NOPE"`),
},
"no_permissions": {
publish: true,
docker: config.Docker{
Image: "docker.io/nope",
Goos: "linux",
Goarch: "amd64",
Binary: "mybin",
Dockerfile: "testdata/Dockerfile",
TagTemplates: []string{
"{{.Tag}}",
"latest",
dockers: []config.Docker{
{
Image: "docker.io/nope",
Goos: "linux",
Goarch: "amd64",
Binary: "mybin",
Dockerfile: "testdata/Dockerfile",
TagTemplates: []string{
"{{.Tag}}",
"latest",
},
Latest: true,
},
Latest: true,
},
expect: []string{
"docker.io/nope:latest",
Expand All @@ -226,43 +268,49 @@ func TestRunPipe(t *testing.T) {
},
"dockerfile_doesnt_exist": {
publish: true,
docker: config.Docker{
Image: "whatever",
Goos: "linux",
Goarch: "amd64",
Binary: "mybin",
Dockerfile: "testdata/Dockerfilezzz",
TagTemplates: []string{
"{{.Tag}}",
dockers: []config.Docker{
{
Image: "whatever",
Goos: "linux",
Goarch: "amd64",
Binary: "mybin",
Dockerfile: "testdata/Dockerfilezzz",
TagTemplates: []string{
"{{.Tag}}",
},
},
},
assertError: shouldErr(`failed to link dockerfile`),
},
"extra_file_doesnt_exist": {
publish: true,
docker: config.Docker{
Image: "whatever",
Goos: "linux",
Goarch: "amd64",
Binary: "mybin",
Files: []string{
"testdata/nope.txt",
},
Dockerfile: "testdata/Dockerfile",
TagTemplates: []string{
"{{.Tag}}",
dockers: []config.Docker{
{
Image: "whatever",
Goos: "linux",
Goarch: "amd64",
Binary: "mybin",
Files: []string{
"testdata/nope.txt",
},
Dockerfile: "testdata/Dockerfile",
TagTemplates: []string{
"{{.Tag}}",
},
},
},
assertError: shouldErr(`failed to link extra file 'testdata/nope.txt'`),
},
"no_matching_binaries": {
publish: true,
docker: config.Docker{
Image: "whatever",
Goos: "darwin",
Goarch: "amd64",
Binary: "mybinnnn",
Dockerfile: "testdata/Dockerfile",
dockers: []config.Docker{
{
Image: "whatever",
Goos: "darwin",
Goarch: "amd64",
Binary: "mybinnnn",
Dockerfile: "testdata/Dockerfile",
},
},
assertError: shouldNotErr,
},
Expand All @@ -286,9 +334,7 @@ func TestRunPipe(t *testing.T) {
var ctx = context.New(config.Project{
ProjectName: "mybin",
Dist: dist,
Dockers: []config.Docker{
docker.docker,
},
Dockers: docker.dockers,
})
ctx.SkipPublish = !docker.publish
ctx.Env = map[string]string{
Expand Down

0 comments on commit f5554bc

Please sign in to comment.