Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 8a2081e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
3 changes: 2 additions & 1 deletion pkg/buildah/buildah.go
Expand Up @@ -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 {
Expand Down
12 changes: 11 additions & 1 deletion pkg/buildah/docker_with_fuse_buildah.go
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pkg/buildah/native_rootless_buildah_linux.go
Expand Up @@ -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{}
Expand Down Expand Up @@ -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{}
Expand Down
11 changes: 11 additions & 0 deletions pkg/container_runtime/buildah_runtime.go
Expand Up @@ -3,6 +3,7 @@ package container_runtime
import (
"context"
"fmt"
"strings"

"github.com/werf/logboek"
"github.com/werf/werf/pkg/buildah"
Expand Down Expand Up @@ -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,
})
}

Expand Down

0 comments on commit 8a2081e

Please sign in to comment.