From 914a8072d96e3b2420a5c025829098ef73ec56bf Mon Sep 17 00:00:00 2001 From: Taylor Silva Date: Wed, 14 Jul 2021 15:56:27 -0400 Subject: [PATCH 1/2] Add IMAGE_PLATFORM param Allows users to build images for other platforms like linux/arm64 Signed-off-by: Taylor Silva --- README.md | 5 +++++ task.go | 6 ++++++ task_test.go | 8 ++++++++ types.go | 2 ++ 4 files changed, 21 insertions(+) diff --git a/README.md b/README.md index 1ad3000..1f3bbd6 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,11 @@ Next, any of the following optional parameters may be specified: name. For example, `IMAGE_ARG_base_image=ubuntu/image.tar` will set `base_image` to a local image reference for using `ubuntu/image.tar`. +* `IMAGE_PLATFORM`: Specify the target platform to build the image for. For + example `IMAGE_PLATFORM=linux/arm64` will build the image for the Linux OS + and `arm64` architecture. By default, images will be built for the current + worker's platform that the task is running on. + * `$LABEL_*`: params prefixed with `LABEL_` will be set as image labels. For example `LABEL_foo=bar`, will set the `foo` label to `bar`. diff --git a/task.go b/task.go index 2e5affd..1fe3d40 100644 --- a/task.go +++ b/task.go @@ -148,6 +148,12 @@ func Build(buildkitd *Buildkitd, outputsDir string, req Request) (Response, erro ) } + if req.Config.ImagePlatform != "" { + buildctlArgs = append(buildctlArgs, + "--opt", "platform="+req.Config.ImagePlatform, + ) + } + builds = append(builds, buildctlArgs) targets = append(targets, "") diff --git a/task_test.go b/task_test.go index 4cab4ec..6397e75 100644 --- a/task_test.go +++ b/task_test.go @@ -531,6 +531,14 @@ func (s *TaskSuite) TestAddHosts() { s.NoError(err) } +func (s *TaskSuite) TestImagePlatform() { + s.req.Config.ContextDir = "testdata/basic" + s.req.Config.ImagePlatform = "linux/arm64" + + _, err := s.build() + s.NoError(err) +} + func (s *TaskSuite) build() (task.Response, error) { return task.Build(s.buildkitd, s.outputsDir, s.req) } diff --git a/types.go b/types.go index 38960d3..2e4dc58 100644 --- a/types.go +++ b/types.go @@ -77,6 +77,8 @@ type Config struct { ImageArgs []string `json:"image_args" envconfig:"optional"` AddHosts string `json:"add_hosts" envconfig:"BUILDKIT_ADD_HOSTS,optional"` + + ImagePlatform string `json:"image_platform" envconfig:"optional"` } // ImageMetadata is the schema written to manifest.json when producing the From e0ea63e63d6cccea6a3c7c8af83bb7f5106c49f3 Mon Sep 17 00:00:00 2001 From: Taylor Silva Date: Thu, 15 Jul 2021 16:22:50 -0400 Subject: [PATCH 2/2] Update platform test to verify image platform Signed-off-by: Taylor Silva --- task_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/task_test.go b/task_test.go index 6397e75..36c0208 100644 --- a/task_test.go +++ b/task_test.go @@ -537,6 +537,15 @@ func (s *TaskSuite) TestImagePlatform() { _, err := s.build() s.NoError(err) + + image, err := tarball.ImageFromPath(s.imagePath("image.tar"), nil) + s.NoError(err) + + configFile, err := image.ConfigFile() + s.NoError(err) + + s.Equal("linux", configFile.OS) + s.Equal("arm64", configFile.Architecture) } func (s *TaskSuite) build() (task.Response, error) {