feat: Add build option for additional contexts (#798)

This commit is contained in:
Martin 2025-09-30 22:55:12 +02:00 committed by GitHub
parent 587fa20b96
commit 974fe956aa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 54 additions and 0 deletions

View file

@ -121,6 +121,7 @@ Required:
Optional: Optional:
- `additional_contexts` (List of String) A list of additional build contexts. Only supported when using a buildx builder. Example: `["name=path", "src = https://example.org"}`. Please see https://docs.docker.com/reference/cli/docker/buildx/build/#build-context for more information.
- `auth_config` (Block List) The configuration for the authentication (see [below for nested schema](#nestedblock--build--auth_config)) - `auth_config` (Block List) The configuration for the authentication (see [below for nested schema](#nestedblock--build--auth_config))
- `build_args` (Map of String) Pairs for build-time variables in the form of `ENDPOINT : "https://example.com"` - `build_args` (Map of String) Pairs for build-time variables in the form of `ENDPOINT : "https://example.com"`
- `build_id` (String) BuildID is an optional identifier that can be passed together with the build request. The same identifier can be used to gracefully cancel the build with the cancel request. - `build_id` (String) BuildID is an optional identifier that can be passed together with the build request. The same identifier can be used to gracefully cancel the build with the cancel request.

View file

@ -227,6 +227,10 @@ func mapBuildAttributesToBuildOptions(buildAttributes map[string]interface{}, im
options.tags = append(options.tags, t.(string)) options.tags = append(options.tags, t.(string))
} }
if contexts, ok := buildAttributes["additional_contexts"].([]interface{}); ok {
options.contexts = interfaceArrayToStringArray(contexts)
}
if builder, ok := buildAttributes["builder"].(string); ok { if builder, ok := buildAttributes["builder"].(string); ok {
options.builder = builder options.builder = builder
} }

View file

@ -328,6 +328,16 @@ func resourceDockerImage() *schema.Resource {
Required: true, Required: true,
ForceNew: true, ForceNew: true,
}, },
"additional_contexts": {
Type: schema.TypeList,
Description: "A list of additional build contexts. Only supported when using a buildx builder. Example: `[\"name=path\", \"src = https://example.org\"}`. Please see https://docs.docker.com/reference/cli/docker/buildx/build/#build-context for more information.",
Optional: true,
ForceNew: true,
Elem: &schema.Schema{
Type: schema.TypeString,
Description: "An additional context in the form of `key=value`",
},
},
"labels": { "labels": {
Type: schema.TypeMap, Type: schema.TypeMap,
Description: "User-defined key/value metadata", Description: "User-defined key/value metadata",

View file

@ -609,6 +609,25 @@ func TestAccDockerImageResource_buildxCacheFromCacheTo(t *testing.T) {
}) })
} }
func TestAccDockerImageResource_buildxAdditionalContexts(t *testing.T) {
wd, _ := os.Getwd()
context := strings.ReplaceAll((filepath.Join(wd, "..", "..", "scripts", "testing", "docker_registry_image_context")), "\\", "\\\\")
additional_context := strings.ReplaceAll((filepath.Join(wd, "..", "..", "scripts", "testing", "buildx_additional_context")), "\\", "\\\\")
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(loadTestConfiguration(t, RESOURCE, "docker_image", "testAccDockerImageAdditionalContexts"), context, context, additional_context),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("docker_image.test_additional_contexts", "image_id"),
),
},
},
})
}
// Test for https://github.com/kreuzwerker/terraform-provider-docker/issues/249 // Test for https://github.com/kreuzwerker/terraform-provider-docker/issues/249
func TestAccDockerImageResource_whitelistDockerignore(t *testing.T) { func TestAccDockerImageResource_whitelistDockerignore(t *testing.T) {
name := "tftest-dockerregistryimage-whitelistdockerignore:1.0" name := "tftest-dockerregistryimage-whitelistdockerignore:1.0"

View file

@ -0,0 +1,20 @@
resource "docker_buildx_builder" "foo" {
name = "foo"
docker_container {
image = "moby/buildkit:v0.22.0"
}
use = true
bootstrap = true
}
resource "docker_image" "test_additional_contexts" {
name = "alpine:latest"
build {
context = "%s"
dockerfile = "%s/Dockerfile"
force_remove = true
builder = docker_buildx_builder.foo.name
additional_contexts = ["second=%s"]
}
}