feat: Implement triggers attribute for docker_image. (#425)

* feat: Implement triggers attribute for docker_image.

* fix: Update repo_digest value in test.
This commit is contained in:
Martin 2022-08-10 13:57:14 +02:00 committed by GitHub
parent 6db80ccbb0
commit 59fcfcc269
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 1 deletions

View file

@ -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 generated by tfplugindocs -->
## 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

View file

@ -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)]))
}
}

View file

@ -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"),
),
},
},

View file

@ -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,
},
},
}
}

View file

@ -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 }}