Skip to content
Permalink
Browse files
fix(buildah): support --build-arg arguments defined in the werf.yaml
Fixes #3858
  • Loading branch information
distorhead committed Nov 8, 2021
1 parent 1643aaa commit 8a2081ef4499bba507fd66198370353460f04992
Showing with 26 additions and 3 deletions.
  1. +2 −1 pkg/buildah/buildah.go
  2. +11 −1 pkg/buildah/docker_with_fuse_buildah.go
  3. +2 −1 pkg/buildah/native_rootless_buildah_linux.go
  4. +11 −0 pkg/container_runtime/buildah_runtime.go
@@ -25,11 +25,12 @@ type CommonOpts struct {
type BuildFromDockerfileOpts struct {
CommonOpts
ContextTar io.Reader
BuildArgs map[string]string
}

type RunCommandOpts struct {
CommonOpts
BuildArgs []string
Args []string
}

type FromCommandOpts struct {
@@ -62,13 +62,23 @@ func (b *DockerWithFuseBuildah) BuildFromDockerfile(ctx context.Context, dockerf
}
}()

var buildArgs []string
for k, v := range opts.BuildArgs {
buildArgs = append(buildArgs, "--build-arg", fmt.Sprintf("%s=%s", k, v))
}

// NOTE: it is principal to use cli option --tls-verify=true|false form with equality sign, instead of separate arguments (--tls-verify true|false), because --tls-verify is by itself a boolean argument
budArgs := []string{"bud", "--format", "docker", fmt.Sprintf("--tls-verify=%s", strconv.FormatBool(!b.Insecure))}
budArgs = append(budArgs, buildArgs...)
budArgs = append(budArgs, "-f", "/.werf/buildah/tmp/Dockerfile")

output, _, err := b.runBuildah(
ctx,
[]string{
"--volume", fmt.Sprintf("%s:/.werf/buildah/tmp", sessionTmpDir),
"--workdir", "/.werf/buildah/tmp/context",
},
[]string{"bud", "--format=docker", fmt.Sprintf("--tls-verify=%s", strconv.FormatBool(!b.Insecure)), "-f", "/.werf/buildah/tmp/Dockerfile"}, opts.LogWriter,
budArgs, opts.LogWriter,
)
if err != nil {
return "", err
@@ -153,6 +153,7 @@ func (b *NativeRootlessBuildah) BuildFromDockerfile(ctx context.Context, dockerf
DockerInsecureSkipTLSVerify: imgtypes.NewOptionalBool(b.Insecure),
DockerDaemonInsecureSkipTLSVerify: b.Insecure,
},
Args: opts.BuildArgs,
}

errLog := &bytes.Buffer{}
@@ -184,7 +185,7 @@ func (b *NativeRootlessBuildah) BuildFromDockerfile(ctx context.Context, dockerf

func (b *NativeRootlessBuildah) RunCommand(ctx context.Context, container string, command []string, opts RunCommandOpts) error {
runOpts := buildah.RunOptions{
Args: opts.BuildArgs,
Args: opts.Args,
}

stderr := &bytes.Buffer{}
@@ -3,6 +3,7 @@ package container_runtime
import (
"context"
"fmt"
"strings"

"github.com/werf/logboek"
"github.com/werf/werf/pkg/buildah"
@@ -81,11 +82,21 @@ func (runtime *BuildahRuntime) Push(ctx context.Context, ref string, opts PushOp
}

func (runtime *BuildahRuntime) BuildDockerfile(ctx context.Context, dockerfile []byte, opts BuildDockerfileOpts) (string, error) {
buildArgs := make(map[string]string)
for _, argStr := range opts.BuildArgs {
argParts := strings.SplitN(argStr, "=", 2)
if len(argParts) < 2 {
return "", fmt.Errorf("invalid build argument %q given, expected string in the key=value format", argStr)
}
buildArgs[argParts[0]] = argParts[1]
}

return runtime.buildah.BuildFromDockerfile(ctx, dockerfile, buildah.BuildFromDockerfileOpts{
CommonOpts: buildah.CommonOpts{
LogWriter: logboek.Context(ctx).OutStream(),
},
ContextTar: opts.ContextTar,
BuildArgs: buildArgs,
})
}

0 comments on commit 8a2081e

Please sign in to comment.