1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-10 03:47:03 +02:00
goreleaser/internal/pipe/docker/docker.go

284 lines
7.9 KiB
Go
Raw Normal View History

2017-09-12 04:31:00 +02:00
// Package docker provides a Pipe that creates and pushes a Docker image
package docker
import (
"fmt"
"io/ioutil"
2017-09-12 12:54:43 +02:00
"os"
2017-09-12 04:31:00 +02:00
"os/exec"
"path/filepath"
"strings"
2017-09-12 04:31:00 +02:00
"github.com/apex/log"
2017-12-18 03:11:17 +02:00
"github.com/goreleaser/goreleaser/internal/artifact"
2018-10-20 16:49:14 +02:00
"github.com/goreleaser/goreleaser/internal/deprecate"
2018-09-12 19:18:01 +02:00
"github.com/goreleaser/goreleaser/internal/pipe"
"github.com/goreleaser/goreleaser/internal/semerrgroup"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
2018-10-20 18:45:31 +02:00
"github.com/pkg/errors"
2017-09-12 04:31:00 +02:00
)
2017-09-12 05:22:02 +02:00
// ErrNoDocker is shown when docker cannot be found in $PATH
var ErrNoDocker = errors.New("docker not present in $PATH")
// Pipe for docker
2017-09-12 04:31:00 +02:00
type Pipe struct{}
func (Pipe) String() string {
return "creating Docker images"
2017-09-12 04:31:00 +02:00
}
// Default sets the pipe defaults
func (Pipe) Default(ctx *context.Context) error {
for i := range ctx.Config.Dockers {
2017-12-17 22:10:38 +02:00
var docker = &ctx.Config.Dockers[i]
if docker.Image != "" {
deprecate.Notice("docker.image")
if len(docker.TagTemplates) != 0 {
deprecate.Notice("docker.tag_templates")
} else {
docker.TagTemplates = []string{"{{ .Version }}"}
}
}
2017-12-17 22:10:38 +02:00
if docker.Goos == "" {
docker.Goos = "linux"
}
if docker.Goarch == "" {
docker.Goarch = "amd64"
}
}
// only set defaults if there is exactly 1 docker setup in the config file.
if len(ctx.Config.Dockers) != 1 {
return nil
}
if ctx.Config.Dockers[0].Binary == "" {
ctx.Config.Dockers[0].Binary = ctx.Config.Builds[0].Binary
}
if ctx.Config.Dockers[0].Dockerfile == "" {
ctx.Config.Dockers[0].Dockerfile = "Dockerfile"
}
return nil
}
// Run the pipe
func (Pipe) Run(ctx *context.Context) error {
if len(ctx.Config.Dockers) == 0 || missingImage(ctx) {
2018-09-12 19:18:01 +02:00
return pipe.Skip("docker section is not configured")
}
_, err := exec.LookPath("docker")
if err != nil {
return ErrNoDocker
}
return doRun(ctx)
}
2018-10-20 18:45:31 +02:00
// Publish the docker images
func (Pipe) Publish(ctx *context.Context) error {
var images = ctx.Artifacts.Filter(artifact.ByType(artifact.PublishableDockerImage)).List()
for _, image := range images {
if err := dockerPush(ctx, image); err != nil {
return err
}
}
return nil
}
func missingImage(ctx *context.Context) bool {
return ctx.Config.Dockers[0].Image == "" && len(ctx.Config.Dockers[0].ImageTemplates) == 0
}
2017-09-15 02:19:56 +02:00
func doRun(ctx *context.Context) error {
var g = semerrgroup.New(ctx.Parallelism)
for _, docker := range ctx.Config.Dockers {
2017-12-26 03:27:06 +02:00
docker := docker
g.Go(func() error {
log.WithField("docker", docker).Debug("looking for binaries matching")
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 {
return a.Extra["Binary"] == docker.Binary
},
),
).List()
if len(binaries) != 1 {
return fmt.Errorf(
"%d binaries match docker definition: %s: %s_%s_%s",
len(binaries),
docker.Binary, docker.Goos, docker.Goarch, docker.Goarm,
)
2017-09-12 04:31:00 +02:00
}
return process(ctx, docker, binaries[0])
2017-12-26 03:27:06 +02:00
})
2017-09-12 04:31:00 +02:00
}
2017-12-26 03:27:06 +02:00
return g.Wait()
2017-09-12 04:31:00 +02:00
}
2018-10-20 18:45:31 +02:00
func process(ctx *context.Context, docker config.Docker, bin artifact.Artifact) error {
tmp, err := ioutil.TempDir(ctx.Config.Dist, "goreleaserdocker")
if err != nil {
return errors.Wrap(err, "failed to create temporary dir")
}
2018-09-04 14:26:45 +02:00
log.Debug("tempdir: " + tmp)
2018-10-20 18:45:31 +02:00
images, err := processImageTemplates(ctx, docker, bin)
if err != nil {
return err
}
if err := os.Link(docker.Dockerfile, filepath.Join(tmp, "Dockerfile")); err != nil {
2017-09-12 12:54:43 +02:00
return errors.Wrap(err, "failed to link dockerfile")
2017-09-12 04:31:00 +02:00
}
for _, file := range docker.Files {
if err := link(file, filepath.Join(tmp, filepath.Base(file))); err != nil {
return errors.Wrapf(err, "failed to link extra file '%s'", file)
}
}
2018-10-20 18:45:31 +02:00
if err := os.Link(bin.Path, filepath.Join(tmp, filepath.Base(bin.Path))); err != nil {
return errors.Wrap(err, "failed to link binary")
}
2018-10-20 18:45:31 +02:00
buildFlags, err := processBuildFlagTemplates(ctx, docker, bin)
if err != nil {
return err
}
if err := dockerBuild(ctx, tmp, images, buildFlags); err != nil {
2017-09-12 04:31:00 +02:00
return err
}
2018-10-20 18:45:31 +02:00
if docker.SkipPush {
// TODO: this should also be better handled
log.Warn(pipe.Skip("skip_push is set").Error())
return nil
}
for _, img := range images {
ctx.Artifacts.Add(artifact.Artifact{
Type: artifact.PublishableDockerImage,
Name: img,
Path: img,
Goarch: docker.Goarch,
Goos: docker.Goos,
Goarm: docker.Goarm,
})
}
return nil
2017-09-26 23:50:00 +02:00
}
func processImageTemplates(ctx *context.Context, docker config.Docker, artifact artifact.Artifact) ([]string, error) {
if len(docker.ImageTemplates) != 0 && docker.Image != "" {
return nil, errors.New("failed to process image, use either image_templates (preferred) or image, not both")
}
// nolint:prealloc
var images []string
for _, imageTemplate := range docker.ImageTemplates {
// TODO: add overrides support to config
image, err := tmpl.New(ctx).
WithArtifact(artifact, map[string]string{}).
Apply(imageTemplate)
if err != nil {
return nil, errors.Wrapf(err, "failed to execute image template '%s'", imageTemplate)
}
images = append(images, image)
}
for _, tagTemplate := range docker.TagTemplates {
imageTemplate := fmt.Sprintf("%s:%s", docker.Image, tagTemplate)
// TODO: add overrides support to config
image, err := tmpl.New(ctx).
WithArtifact(artifact, map[string]string{}).
Apply(imageTemplate)
if err != nil {
return nil, errors.Wrapf(err, "failed to execute tag template '%s'", tagTemplate)
}
images = append(images, image)
}
return images, nil
}
func processBuildFlagTemplates(ctx *context.Context, docker config.Docker, artifact artifact.Artifact) ([]string, error) {
// nolint:prealloc
var buildFlags []string
for _, buildFlagTemplate := range docker.BuildFlagTemplates {
buildFlag, err := tmpl.New(ctx).
WithArtifact(artifact, map[string]string{}).
Apply(buildFlagTemplate)
if err != nil {
return nil, errors.Wrapf(err, "failed to process build flag template '%s'", buildFlagTemplate)
}
buildFlags = append(buildFlags, buildFlag)
}
return buildFlags, nil
}
// walks the src, recreating dirs and hard-linking files
func link(src, dest string) error {
return filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// We have the following:
// - src = "a/b"
// - dest = "dist/linuxamd64/b"
// - path = "a/b/c.txt"
// So we join "a/b" with "c.txt" and use it as the destination.
var dst = filepath.Join(dest, strings.Replace(path, src, "", 1))
log.WithFields(log.Fields{
"src": path,
"dst": dst,
2018-10-05 14:49:47 +02:00
}).Debug("extra file")
if info.IsDir() {
return os.MkdirAll(dst, info.Mode())
}
return os.Link(path, dst)
})
}
func dockerBuild(ctx *context.Context, root string, images, flags []string) error {
log.WithField("image", images[0]).Info("building docker image")
/* #nosec */
var cmd = exec.CommandContext(ctx, "docker", buildCommand(images, flags)...)
cmd.Dir = root
log.WithField("cmd", cmd.Args).WithField("cwd", cmd.Dir).Debug("running")
2017-09-12 04:31:00 +02:00
out, err := cmd.CombinedOutput()
if err != nil {
return errors.Wrapf(err, "failed to build docker image: \n%s", string(out))
}
log.Debugf("docker build output: \n%s", string(out))
return nil
}
func buildCommand(images, flags []string) []string {
base := []string{"build", "."}
for _, image := range images {
base = append(base, "-t", image)
}
base = append(base, flags...)
return base
}
2018-10-20 18:45:31 +02:00
func dockerPush(ctx *context.Context, image artifact.Artifact) error {
2018-10-20 18:59:13 +02:00
log.WithField("image", image.Name).Info("pushing docker image")
/* #nosec */
2018-10-20 18:45:31 +02:00
var cmd = exec.CommandContext(ctx, "docker", "push", image.Name)
log.WithField("cmd", cmd.Args).Debug("running")
2017-09-12 04:31:00 +02:00
out, err := cmd.CombinedOutput()
if err != nil {
return errors.Wrapf(err, "failed to push docker image: \n%s", string(out))
}
log.Debugf("docker push output: \n%s", string(out))
2018-10-20 18:45:31 +02:00
image.Type = artifact.DockerImage
ctx.Artifacts.Add(image)
2017-09-12 04:31:00 +02:00
return nil
}