Skip to content

Commit

Permalink
feat: use top level environment variables within env section in docker (
Browse files Browse the repository at this point in the history
#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>
  • Loading branch information
pmareke and developer-guy committed Nov 24, 2021
1 parent e53b494 commit 2bdb39e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 18 deletions.
13 changes: 7 additions & 6 deletions internal/pipe/docker/api.go
Expand Up @@ -2,7 +2,6 @@ package docker

import (
"bytes"
"context"
"fmt"
"io"
"os/exec"
Expand All @@ -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 (
Expand All @@ -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,
Expand All @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions internal/pipe/docker/api_buildpack.go
@@ -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)
}
Expand Down
11 changes: 6 additions & 5 deletions internal/pipe/docker/api_docker.go
@@ -1,8 +1,9 @@
package docker

import (
"context"
"fmt"

"github.com/goreleaser/goreleaser/pkg/context"
)

func init() {
Expand All @@ -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}
Expand All @@ -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 {
Expand All @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions internal/pipe/docker/docker.go
Expand Up @@ -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
}
Expand Down
6 changes: 2 additions & 4 deletions internal/pipe/docker/docker_test.go
Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 2bdb39e

Please sign in to comment.