Add support for build-args with --build local (#1256)

At the moment build-args can be specified in the compose configuration,
but they're used only when creating a build-config and not for local
builds.
This commit is contained in:
Christian Fetzer 2020-03-12 15:16:36 +01:00 committed by GitHub
parent d105a77e07
commit ca75c31df8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 2 deletions

View File

@ -24,6 +24,7 @@ import (
"path" "path"
"strings" "strings"
dockerlib "github.com/fsouza/go-dockerclient"
"github.com/kubernetes/kompose/pkg/kobject" "github.com/kubernetes/kompose/pkg/kobject"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -268,6 +269,17 @@ func BuildDockerImage(service kobject.ServiceConfig, name string) error {
imageName = service.Image imageName = service.Image
} }
buildargs := []dockerlib.BuildArg{}
for envName, envValue := range service.BuildArgs {
var value string
if envValue == nil {
value = os.Getenv(envName)
} else {
value = *envValue
}
buildargs = append(buildargs, dockerlib.BuildArg{Name: envName, Value: value})
}
// Connect to the Docker client // Connect to the Docker client
client, err := docker.Client() client, err := docker.Client()
if err != nil { if err != nil {
@ -277,7 +289,7 @@ func BuildDockerImage(service kobject.ServiceConfig, name string) error {
// Use the build struct function to build the image // Use the build struct function to build the image
// Build the image! // Build the image!
build := docker.Build{Client: *client} build := docker.Build{Client: *client}
err = build.BuildImage(imagePath, imageName, service.Dockerfile) err = build.BuildImage(imagePath, imageName, service.Dockerfile, buildargs)
if err != nil { if err != nil {
return err return err

View File

@ -38,7 +38,7 @@ BuildImage builds a Docker image via the Docker API. Takes the source directory
and image name and then builds the appropriate image. Tarball is utilized and image name and then builds the appropriate image. Tarball is utilized
in order to make building easier. in order to make building easier.
*/ */
func (c *Build) BuildImage(source string, image string, dockerfile string) error { func (c *Build) BuildImage(source string, image string, dockerfile string, buildargs []dockerlib.BuildArg) error {
log.Infof("Building image '%s' from directory '%s'", image, path.Base(source)) log.Infof("Building image '%s' from directory '%s'", image, path.Base(source))
@ -68,6 +68,7 @@ func (c *Build) BuildImage(source string, image string, dockerfile string) error
InputStream: tarballSource, InputStream: tarballSource,
OutputStream: outputBuffer, OutputStream: outputBuffer,
Dockerfile: dockerfile, Dockerfile: dockerfile,
BuildArgs: buildargs,
} }
// Build it! // Build it!