diff --git a/docs/resources/image.md b/docs/resources/image.md index 18907b27..92791816 100644 --- a/docs/resources/image.md +++ b/docs/resources/image.md @@ -62,6 +62,20 @@ resource "docker_image" "zoo" { } ``` +You can use the `triggers` argument to specify when the image should be rebuild. This is for example helpful when you want to rebuild the docker image whenever the source code changes. + +```terraform +resource "docker_image" "zoo" { + name = "zoo" + build { + path = "." + } + triggers = { + dir_sha1 = sha1(join("", [for f in fileset(path.module, "src/*") : filesha1(f)])) + } +} +``` + ## Schema @@ -76,6 +90,7 @@ resource "docker_image" "zoo" { - `keep_locally` (Boolean) If true, then the Docker image won't be deleted on destroy operation. If this is false, it will delete the image from the docker local storage on destroy operation. - `pull_trigger` (String, Deprecated) A value which cause an image pull when changed - `pull_triggers` (Set of String) List of values which cause an image pull when changed. This is used to store the image digest from the registry when using the [docker_registry_image](../data-sources/registry_image.md). +- `triggers` (Map of String) A map of arbitrary strings that, when changed, will force the `docker_image` resource to be replaced. This can be used to rebuild an image when contents of source code folders change ### Read-Only diff --git a/examples/resources/docker_image/resource-build-triggers.tf b/examples/resources/docker_image/resource-build-triggers.tf new file mode 100644 index 00000000..14d8aeac --- /dev/null +++ b/examples/resources/docker_image/resource-build-triggers.tf @@ -0,0 +1,9 @@ +resource "docker_image" "zoo" { + name = "zoo" + build { + path = "." + } + triggers = { + dir_sha1 = sha1(join("", [for f in fileset(path.module, "src/*") : filesha1(f)])) + } +} \ No newline at end of file diff --git a/internal/provider/data_source_docker_image_test.go b/internal/provider/data_source_docker_image_test.go index 56c36c94..fe0765db 100644 --- a/internal/provider/data_source_docker_image_test.go +++ b/internal/provider/data_source_docker_image_test.go @@ -32,7 +32,7 @@ func TestAccDockerImageDataSource_withSpecificTag(t *testing.T) { Config: loadTestConfiguration(t, DATA_SOURCE, "docker_image", "testAccDockerImageDataSourceWithSpecificTag"), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("data.docker_image.foo", "name", imageName), - resource.TestCheckResourceAttr("data.docker_image.foo", "repo_digest", "busybox@sha256:a3170d3672568b2c6626710db3573f3d92ee31eed933c24f3d7ea978178e21b8"), + resource.TestCheckResourceAttr("data.docker_image.foo", "repo_digest", "busybox@sha256:09439c073bd3eb029a91c72eff2c0d9f12ab9c84f66bdef360fcf3f91a81bf7c"), ), }, }, diff --git a/internal/provider/resource_docker_image.go b/internal/provider/resource_docker_image.go index a7181321..0e229231 100644 --- a/internal/provider/resource_docker_image.go +++ b/internal/provider/resource_docker_image.go @@ -144,6 +144,12 @@ func resourceDockerImage() *schema.Resource { }, }, }, + "triggers": { + Description: "A map of arbitrary strings that, when changed, will force the `docker_image` resource to be replaced. This can be used to rebuild an image when contents of source code folders change", + Type: schema.TypeMap, + Optional: true, + ForceNew: true, + }, }, } } diff --git a/templates/resources/image.md.tmpl b/templates/resources/image.md.tmpl index 36a884e9..cd562f2e 100644 --- a/templates/resources/image.md.tmpl +++ b/templates/resources/image.md.tmpl @@ -33,4 +33,8 @@ In this case the image "zoo" and "zoo:develop" are built. {{tffile "examples/resources/docker_image/resource-build.tf"}} +You can use the `triggers` argument to specify when the image should be rebuild. This is for example helpful when you want to rebuild the docker image whenever the source code changes. + +{{tffile "examples/resources/docker_image/resource-build-triggers.tf"}} + {{ .SchemaMarkdown | trimspace }}