mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-03-31 21:55:34 +02:00
also supporting latest docker tag
This commit is contained in:
parent
f4c79826da
commit
2175bf1e80
@ -167,6 +167,7 @@ type Docker struct {
|
|||||||
Goarm string `yaml:",omitempty"`
|
Goarm string `yaml:",omitempty"`
|
||||||
Image string `yaml:",omitempty"`
|
Image string `yaml:",omitempty"`
|
||||||
Dockerfile string `yaml:",omitempty"`
|
Dockerfile string `yaml:",omitempty"`
|
||||||
|
Latest bool `yaml:",omitempty"`
|
||||||
|
|
||||||
// Capture all undefined fields and should be empty after loading
|
// Capture all undefined fields and should be empty after loading
|
||||||
XXX map[string]interface{} `yaml:",inline"`
|
XXX map[string]interface{} `yaml:",inline"`
|
||||||
|
@ -50,6 +50,8 @@ dockers:
|
|||||||
image: myuser/myimage
|
image: myuser/myimage
|
||||||
# Path to the Dockerfile (from the project root)
|
# Path to the Dockerfile (from the project root)
|
||||||
dockerfile: Dockerfile
|
dockerfile: Dockerfile
|
||||||
|
# Also tag and push myuser/myimage:latest
|
||||||
|
latest: true
|
||||||
```
|
```
|
||||||
|
|
||||||
These settings should allow you to generate multiple docker images, using
|
These settings should allow you to generate multiple docker images, using
|
||||||
|
@ -62,6 +62,7 @@ func doRun(ctx *context.Context, folder string, docker config.Docker, binary con
|
|||||||
var root = filepath.Join(ctx.Config.Dist, folder)
|
var root = filepath.Join(ctx.Config.Dist, folder)
|
||||||
var dockerfile = filepath.Join(root, filepath.Base(docker.Dockerfile))
|
var dockerfile = filepath.Join(root, filepath.Base(docker.Dockerfile))
|
||||||
var image = fmt.Sprintf("%s:%s", docker.Image, ctx.Version)
|
var image = fmt.Sprintf("%s:%s", docker.Image, ctx.Version)
|
||||||
|
var latest = fmt.Sprintf("%s:latest", docker.Image)
|
||||||
|
|
||||||
if err := os.Link(docker.Dockerfile, dockerfile); err != nil {
|
if err := os.Link(docker.Dockerfile, dockerfile); err != nil {
|
||||||
return errors.Wrap(err, "failed to link dockerfile")
|
return errors.Wrap(err, "failed to link dockerfile")
|
||||||
@ -69,6 +70,11 @@ func doRun(ctx *context.Context, folder string, docker config.Docker, binary con
|
|||||||
if err := dockerBuild(root, dockerfile, image); err != nil {
|
if err := dockerBuild(root, dockerfile, image); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if docker.Latest {
|
||||||
|
if err := dockerTag(image, latest); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: improve this so it can log into to stdout
|
// TODO: improve this so it can log into to stdout
|
||||||
if !ctx.Publish {
|
if !ctx.Publish {
|
||||||
@ -80,6 +86,11 @@ func doRun(ctx *context.Context, folder string, docker config.Docker, binary con
|
|||||||
if err := dockerPush(image); err != nil {
|
if err := dockerPush(image); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if docker.Latest {
|
||||||
|
if err := dockerTag(image, latest); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,6 +106,18 @@ func dockerBuild(root, dockerfile, image string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func dockerTag(image, tag string) error {
|
||||||
|
log.WithField("image", image).WithField("tag", tag).Info("building docker image")
|
||||||
|
var cmd = exec.Command("docker", "tag", image, tag)
|
||||||
|
log.WithField("cmd", cmd).Debug("executing")
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "failed to tag docker image: \n%s", string(out))
|
||||||
|
}
|
||||||
|
log.Debugf("docker tag output: \n%s", string(out))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func dockerPush(image string) error {
|
func dockerPush(image string) error {
|
||||||
log.WithField("image", image).Info("pushing docker image")
|
log.WithField("image", image).Info("pushing docker image")
|
||||||
var cmd = exec.Command("docker", "push", image)
|
var cmd = exec.Command("docker", "push", image)
|
||||||
|
@ -24,8 +24,14 @@ func TestRunPipe(t *testing.T) {
|
|||||||
var binPath = filepath.Join(dist, "mybin", "mybin")
|
var binPath = filepath.Join(dist, "mybin", "mybin")
|
||||||
_, err = os.Create(binPath)
|
_, err = os.Create(binPath)
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
|
var images = []string{
|
||||||
|
"goreleaser/test_run_pipe:1.0.0",
|
||||||
|
"goreleaser/test_run_pipe:latest",
|
||||||
|
}
|
||||||
// this might fail as the image doesnt exist yet, so lets ignore the error
|
// this might fail as the image doesnt exist yet, so lets ignore the error
|
||||||
_ = exec.Command("docker", "rmi", "goreleaser/test_run_pipe:v1.0.0").Run()
|
for _, img := range images {
|
||||||
|
_ = exec.Command("docker", "rmi", img).Run()
|
||||||
|
}
|
||||||
var ctx = &context.Context{
|
var ctx = &context.Context{
|
||||||
Version: "1.0.0",
|
Version: "1.0.0",
|
||||||
Publish: true,
|
Publish: true,
|
||||||
@ -39,6 +45,7 @@ func TestRunPipe(t *testing.T) {
|
|||||||
Goarch: "amd64",
|
Goarch: "amd64",
|
||||||
Dockerfile: "testdata/Dockerfile",
|
Dockerfile: "testdata/Dockerfile",
|
||||||
Binary: "mybin",
|
Binary: "mybin",
|
||||||
|
Latest: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Image: "goreleaser/test_run_pipe_nope",
|
Image: "goreleaser/test_run_pipe_nope",
|
||||||
@ -54,11 +61,13 @@ func TestRunPipe(t *testing.T) {
|
|||||||
ctx.AddBinary(plat, "mybin", "mybin", binPath)
|
ctx.AddBinary(plat, "mybin", "mybin", binPath)
|
||||||
}
|
}
|
||||||
assert.NoError(Pipe{}.Run(ctx))
|
assert.NoError(Pipe{}.Run(ctx))
|
||||||
|
|
||||||
// this might should not fail as the image should have been created when
|
// this might should not fail as the image should have been created when
|
||||||
// the step ran
|
// the step ran
|
||||||
assert.NoError(
|
for _, img := range images {
|
||||||
exec.Command("docker", "rmi", "goreleaser/test_run_pipe:1.0.0").Run(),
|
assert.NoError(exec.Command("docker", "rmi", img).Run())
|
||||||
)
|
}
|
||||||
|
|
||||||
// the test_run_pipe_nope image should not have been created, so deleting
|
// the test_run_pipe_nope image should not have been created, so deleting
|
||||||
// it should fail
|
// it should fail
|
||||||
assert.Error(
|
assert.Error(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user