2017-09-12 04:31:00 +02:00
|
|
|
// Package docker provides a Pipe that creates and pushes a Docker image
|
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-09-12 12:54:43 +02:00
|
|
|
"os"
|
2017-09-12 04:31:00 +02:00
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/goreleaser/goreleaser/config"
|
|
|
|
"github.com/goreleaser/goreleaser/context"
|
|
|
|
"github.com/goreleaser/goreleaser/pipeline"
|
2017-09-13 01:58:02 +02:00
|
|
|
|
|
|
|
"github.com/apex/log"
|
|
|
|
|
2017-09-12 04:50:57 +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{}
|
|
|
|
|
|
|
|
// Description of the pipe
|
|
|
|
func (Pipe) Description() string {
|
|
|
|
return "Creating Docker images"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the pipe
|
2017-09-12 05:22:02 +02:00
|
|
|
func (Pipe) Run(ctx *context.Context) error {
|
|
|
|
if len(ctx.Config.Dockers) == 0 || ctx.Config.Dockers[0].Image == "" {
|
2017-09-12 04:31:00 +02:00
|
|
|
return pipeline.Skip("docker section is not configured")
|
|
|
|
}
|
2017-09-12 05:22:02 +02:00
|
|
|
_, err := exec.LookPath("docker")
|
|
|
|
if err != nil {
|
|
|
|
return ErrNoDocker
|
|
|
|
}
|
2017-09-12 04:42:36 +02:00
|
|
|
for _, docker := range ctx.Config.Dockers {
|
2017-09-12 04:31:00 +02:00
|
|
|
var imagePlatform = docker.Goos + docker.Goarch + docker.Goarm
|
|
|
|
for platform, groups := range ctx.Binaries {
|
|
|
|
if platform != imagePlatform {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for folder, binaries := range groups {
|
|
|
|
for _, binary := range binaries {
|
|
|
|
if binary.Name != docker.Binary {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var err = doRun(ctx, folder, docker, binary)
|
|
|
|
if err != nil && !pipeline.IsSkip(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func doRun(ctx *context.Context, folder string, docker config.Docker, binary context.Binary) error {
|
|
|
|
var root = filepath.Join(ctx.Config.Dist, folder)
|
2017-09-12 15:04:56 +02:00
|
|
|
var dockerfile = filepath.Join(root, filepath.Base(docker.Dockerfile))
|
2017-09-12 04:42:36 +02:00
|
|
|
var image = fmt.Sprintf("%s:%s", docker.Image, ctx.Git.CurrentTag)
|
2017-09-12 04:31:00 +02:00
|
|
|
|
2017-09-12 12:54:43 +02:00
|
|
|
if err := os.Link(docker.Dockerfile, dockerfile); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to link dockerfile")
|
2017-09-12 04:31:00 +02:00
|
|
|
}
|
2017-09-12 15:04:56 +02:00
|
|
|
if err := dockerBuild(root, dockerfile, image); err != nil {
|
2017-09-12 04:31:00 +02:00
|
|
|
return err
|
|
|
|
}
|
2017-09-12 12:54:43 +02:00
|
|
|
|
2017-09-12 04:59:43 +02:00
|
|
|
// TODO: improve this so it can log into to stdout
|
2017-09-12 04:31:00 +02:00
|
|
|
if !ctx.Publish {
|
|
|
|
return pipeline.Skip("--skip-publish is set")
|
|
|
|
}
|
2017-09-12 05:29:12 +02:00
|
|
|
if ctx.Config.Release.Draft {
|
|
|
|
return pipeline.Skip("release is marked as draft")
|
|
|
|
}
|
2017-09-12 04:31:00 +02:00
|
|
|
if err := dockerPush(image); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-12 15:04:56 +02:00
|
|
|
func dockerBuild(root, dockerfile, image string) error {
|
2017-09-12 04:42:36 +02:00
|
|
|
log.WithField("image", image).Info("building docker image")
|
2017-09-12 15:04:56 +02:00
|
|
|
var cmd = exec.Command("docker", "build", "-f", dockerfile, "-t", image, root)
|
2017-09-12 04:31:00 +02:00
|
|
|
log.WithField("cmd", cmd).Debug("executing")
|
|
|
|
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 dockerPush(image string) error {
|
2017-09-12 04:42:36 +02:00
|
|
|
log.WithField("image", image).Info("pushing docker image")
|
2017-09-12 04:31:00 +02:00
|
|
|
var cmd = exec.Command("docker", "push", image)
|
|
|
|
log.WithField("cmd", cmd).Debug("executing")
|
|
|
|
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))
|
|
|
|
return nil
|
|
|
|
}
|