mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-02-01 13:07:49 +02:00
fix: multiple docker instances with the same extra files (#766)
* fix: multiple docker instances with the same extra files * fix: removed unused seed param
This commit is contained in:
parent
e5c8000ff1
commit
34e3f905c3
@ -3,6 +3,7 @@ package docker
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -70,9 +71,8 @@ func (Pipe) Run(ctx *context.Context) error {
|
|||||||
|
|
||||||
func doRun(ctx *context.Context) error {
|
func doRun(ctx *context.Context) error {
|
||||||
var g = semerrgroup.New(ctx.Parallelism)
|
var g = semerrgroup.New(ctx.Parallelism)
|
||||||
for i, docker := range ctx.Config.Dockers {
|
for _, docker := range ctx.Config.Dockers {
|
||||||
docker := docker
|
docker := docker
|
||||||
seed := i
|
|
||||||
g.Go(func() error {
|
g.Go(func() error {
|
||||||
log.WithField("docker", docker).Debug("looking for binaries matching")
|
log.WithField("docker", docker).Debug("looking for binaries matching")
|
||||||
var binaries = ctx.Artifacts.Filter(
|
var binaries = ctx.Artifacts.Filter(
|
||||||
@ -93,15 +93,18 @@ func doRun(ctx *context.Context) error {
|
|||||||
docker.Binary, docker.Goos, docker.Goarch, docker.Goarm,
|
docker.Binary, docker.Goos, docker.Goarch, docker.Goarm,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
return process(ctx, docker, binaries[0], seed)
|
return process(ctx, docker, binaries[0])
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return g.Wait()
|
return g.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
func process(ctx *context.Context, docker config.Docker, artifact artifact.Artifact, seed int) error {
|
func process(ctx *context.Context, docker config.Docker, artifact artifact.Artifact) error {
|
||||||
var root = filepath.Dir(artifact.Path)
|
tmp, err := ioutil.TempDir("", "goreleaserdocker")
|
||||||
var dockerfile = filepath.Join(root, filepath.Base(docker.Dockerfile)) + fmt.Sprintf(".%d", seed)
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "failed to create temporaty dir")
|
||||||
|
}
|
||||||
|
log.Info("tempdir: " + tmp)
|
||||||
// nolint:prealloc
|
// nolint:prealloc
|
||||||
var images []string
|
var images []string
|
||||||
for _, tagTemplate := range docker.TagTemplates {
|
for _, tagTemplate := range docker.TagTemplates {
|
||||||
@ -114,15 +117,18 @@ func process(ctx *context.Context, docker config.Docker, artifact artifact.Artif
|
|||||||
}
|
}
|
||||||
images = append(images, fmt.Sprintf("%s:%s", docker.Image, tag))
|
images = append(images, fmt.Sprintf("%s:%s", docker.Image, tag))
|
||||||
}
|
}
|
||||||
if err := os.Link(docker.Dockerfile, dockerfile); err != nil {
|
if err := os.Link(docker.Dockerfile, filepath.Join(tmp, "Dockerfile")); err != nil {
|
||||||
return errors.Wrap(err, "failed to link dockerfile")
|
return errors.Wrap(err, "failed to link dockerfile")
|
||||||
}
|
}
|
||||||
for _, file := range docker.Files {
|
for _, file := range docker.Files {
|
||||||
if err := link(file, filepath.Join(root, filepath.Base(file))); err != nil {
|
if err := link(file, filepath.Join(tmp, filepath.Base(file))); err != nil {
|
||||||
return errors.Wrapf(err, "failed to link extra file '%s'", file)
|
return errors.Wrapf(err, "failed to link extra file '%s'", file)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err := dockerBuild(ctx, root, dockerfile, images[0]); err != nil {
|
if err := os.Link(artifact.Path, filepath.Join(tmp, filepath.Base(artifact.Path))); err != nil {
|
||||||
|
return errors.Wrap(err, "failed to link binary")
|
||||||
|
}
|
||||||
|
if err := dockerBuild(ctx, tmp, images[0]); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, img := range images[1:] {
|
for _, img := range images[1:] {
|
||||||
@ -175,11 +181,12 @@ func publish(ctx *context.Context, docker config.Docker, images []string) error
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func dockerBuild(ctx *context.Context, root, dockerfile, image string) error {
|
func dockerBuild(ctx *context.Context, root, image string) error {
|
||||||
log.WithField("image", image).Info("building docker image")
|
log.WithField("image", image).Info("building docker image")
|
||||||
/* #nosec */
|
/* #nosec */
|
||||||
var cmd = exec.CommandContext(ctx, "docker", "build", "-f", dockerfile, "-t", image, root)
|
var cmd = exec.CommandContext(ctx, "docker", "build", "-t", image, ".")
|
||||||
log.WithField("cmd", cmd.Args).Info("running")
|
cmd.Dir = root
|
||||||
|
log.WithField("cmd", cmd.Args).WithField("cwd", cmd.Dir).Debug("running")
|
||||||
out, err := cmd.CombinedOutput()
|
out, err := cmd.CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "failed to build docker image: \n%s", string(out))
|
return errors.Wrapf(err, "failed to build docker image: \n%s", string(out))
|
||||||
|
@ -98,6 +98,42 @@ func TestRunPipe(t *testing.T) {
|
|||||||
},
|
},
|
||||||
assertError: shouldNotErr,
|
assertError: shouldNotErr,
|
||||||
},
|
},
|
||||||
|
"multiple images with same extra file": {
|
||||||
|
publish: true,
|
||||||
|
dockers: []config.Docker{
|
||||||
|
{
|
||||||
|
Image: registry + "goreleaser/multiplefiles1",
|
||||||
|
Goos: "linux",
|
||||||
|
Goarch: "amd64",
|
||||||
|
Dockerfile: "testdata/Dockerfile",
|
||||||
|
Binary: "mybin",
|
||||||
|
TagTemplates: []string{
|
||||||
|
"latest",
|
||||||
|
},
|
||||||
|
Files: []string{
|
||||||
|
"testdata/extra_file.txt",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Image: registry + "goreleaser/multiplefiles2",
|
||||||
|
Goos: "linux",
|
||||||
|
Goarch: "amd64",
|
||||||
|
Dockerfile: "testdata/Dockerfile",
|
||||||
|
Binary: "mybin",
|
||||||
|
TagTemplates: []string{
|
||||||
|
"latest",
|
||||||
|
},
|
||||||
|
Files: []string{
|
||||||
|
"testdata/extra_file.txt",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expect: []string{
|
||||||
|
registry + "goreleaser/multiplefiles1:latest",
|
||||||
|
registry + "goreleaser/multiplefiles2:latest",
|
||||||
|
},
|
||||||
|
assertError: shouldNotErr,
|
||||||
|
},
|
||||||
"multiple images with same dockerfile": {
|
"multiple images with same dockerfile": {
|
||||||
publish: true,
|
publish: true,
|
||||||
dockers: []config.Docker{
|
dockers: []config.Docker{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user