You've already forked goreleaser
mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-09-16 09:26:52 +02:00
feat: use top level environment variables within env section in docker (#2596)
* feat: add global env to the docker builds * chore: remove output * feat: updates according to reviews Signed-off-by: Batuhan Apaydın <batuhan.apaydin@trendyol.com> * feat: add test Signed-off-by: Batuhan Apaydın <batuhan.apaydin@trendyol.com> * feat: updates according to code review Signed-off-by: Batuhan Apaydın <batuhan.apaydin@trendyol.com> Co-authored-by: Batuhan Apaydın <batuhan.apaydin@trendyol.com>
This commit is contained in:
committed by
GitHub
parent
e53b4940de
commit
2bdb39e06a
@@ -2,7 +2,6 @@ package docker
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os/exec"
|
||||
@@ -11,6 +10,7 @@ import (
|
||||
"github.com/apex/log"
|
||||
"github.com/goreleaser/goreleaser/internal/gio"
|
||||
"github.com/goreleaser/goreleaser/internal/logext"
|
||||
"github.com/goreleaser/goreleaser/pkg/context"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -33,18 +33,18 @@ func registerImager(use string, impl imager) {
|
||||
|
||||
// imager is something that can build and push docker images.
|
||||
type imager interface {
|
||||
Build(ctx context.Context, root string, images, flags []string) error
|
||||
Push(ctx context.Context, image string, flags []string) error
|
||||
Build(ctx *context.Context, root string, images, flags []string) error
|
||||
Push(ctx *context.Context, image string, flags []string) error
|
||||
}
|
||||
|
||||
// manifester is something that can create and push docker manifests.
|
||||
type manifester interface {
|
||||
Create(ctx context.Context, manifest string, images, flags []string) error
|
||||
Push(ctx context.Context, manifest string, flags []string) error
|
||||
Create(ctx *context.Context, manifest string, images, flags []string) error
|
||||
Push(ctx *context.Context, manifest string, flags []string) error
|
||||
}
|
||||
|
||||
// nolint: unparam
|
||||
func runCommand(ctx context.Context, dir, binary string, args ...string) error {
|
||||
func runCommand(ctx *context.Context, dir, binary string, args ...string) error {
|
||||
fields := log.Fields{
|
||||
"cmd": append([]string{binary}, args[0]),
|
||||
"cwd": dir,
|
||||
@@ -53,6 +53,7 @@ func runCommand(ctx context.Context, dir, binary string, args ...string) error {
|
||||
/* #nosec */
|
||||
cmd := exec.CommandContext(ctx, binary, args...)
|
||||
cmd.Dir = dir
|
||||
cmd.Env = ctx.Env.Strings()
|
||||
|
||||
var b bytes.Buffer
|
||||
w := gio.Safe(&b)
|
||||
|
@@ -1,18 +1,19 @@
|
||||
package docker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/goreleaser/goreleaser/pkg/context"
|
||||
)
|
||||
|
||||
type buildPackImager struct{}
|
||||
|
||||
func (i buildPackImager) Push(ctx context.Context, image string, flags []string) error {
|
||||
func (i buildPackImager) Push(ctx *context.Context, image string, flags []string) error {
|
||||
return dockerImager{}.Push(ctx, image, flags)
|
||||
}
|
||||
|
||||
func (i buildPackImager) Build(ctx context.Context, root string, images, flags []string) error {
|
||||
func (i buildPackImager) Build(ctx *context.Context, root string, images, flags []string) error {
|
||||
if err := runCommand(ctx, "", "pack", i.buildCommand(images, flags)...); err != nil {
|
||||
return fmt.Errorf("failed to build %s: %w", images[0], err)
|
||||
}
|
||||
|
@@ -1,8 +1,9 @@
|
||||
package docker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/goreleaser/goreleaser/pkg/context"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -17,7 +18,7 @@ func init() {
|
||||
|
||||
type dockerManifester struct{}
|
||||
|
||||
func (m dockerManifester) Create(ctx context.Context, manifest string, images, flags []string) error {
|
||||
func (m dockerManifester) Create(ctx *context.Context, manifest string, images, flags []string) error {
|
||||
_ = runCommand(ctx, ".", "docker", "manifest", "rm", manifest)
|
||||
|
||||
args := []string{"manifest", "create", manifest}
|
||||
@@ -30,7 +31,7 @@ func (m dockerManifester) Create(ctx context.Context, manifest string, images, f
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m dockerManifester) Push(ctx context.Context, manifest string, flags []string) error {
|
||||
func (m dockerManifester) Push(ctx *context.Context, manifest string, flags []string) error {
|
||||
args := []string{"manifest", "push", manifest}
|
||||
args = append(args, flags...)
|
||||
if err := runCommand(ctx, ".", "docker", args...); err != nil {
|
||||
@@ -43,14 +44,14 @@ type dockerImager struct {
|
||||
buildx bool
|
||||
}
|
||||
|
||||
func (i dockerImager) Push(ctx context.Context, image string, flags []string) error {
|
||||
func (i dockerImager) Push(ctx *context.Context, image string, flags []string) error {
|
||||
if err := runCommand(ctx, ".", "docker", "push", image); err != nil {
|
||||
return fmt.Errorf("failed to push %s: %w", image, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i dockerImager) Build(ctx context.Context, root string, images, flags []string) error {
|
||||
func (i dockerImager) Build(ctx *context.Context, root string, images, flags []string) error {
|
||||
if err := runCommand(ctx, root, "docker", i.buildCommand(images, flags)...); err != nil {
|
||||
return fmt.Errorf("failed to build %s: %w", images[0], err)
|
||||
}
|
||||
|
@@ -167,6 +167,7 @@ func process(ctx *context.Context, docker config.Docker, artifacts []*artifact.A
|
||||
}
|
||||
|
||||
log.Info("building docker image")
|
||||
|
||||
if err := imagers[docker.Use].Build(ctx, tmp, images, buildFlags); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@@ -66,7 +66,6 @@ func killAndRm(t *testing.T) {
|
||||
|
||||
// TODO: this test is too big... split in smaller tests? Mainly the manifest ones...
|
||||
func TestRunPipe(t *testing.T) {
|
||||
testlib.CheckPath(t, "docker")
|
||||
type errChecker func(*testing.T, error)
|
||||
shouldErr := func(msg string) errChecker {
|
||||
return func(t *testing.T, err error) {
|
||||
@@ -1048,8 +1047,6 @@ func TestRunPipe(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRunPipeWhileUsingBuildpacks(t *testing.T) {
|
||||
testlib.CheckPath(t, "packs")
|
||||
|
||||
type errChecker func(*testing.T, error)
|
||||
shouldNotErr := func(t *testing.T, err error) {
|
||||
t.Helper()
|
||||
@@ -1101,8 +1098,9 @@ func TestRunPipeWhileUsingBuildpacks(t *testing.T) {
|
||||
Dist: dist,
|
||||
Dockers: docker.dockers,
|
||||
})
|
||||
|
||||
ctx.Parallelism = 1
|
||||
ctx.Env = docker.env
|
||||
// ctx.Env = docker.env
|
||||
ctx.Version = "1.0.0"
|
||||
ctx.Git = context.GitInfo{
|
||||
CurrentTag: "v1.0.0",
|
||||
|
Reference in New Issue
Block a user