|
| 1 | +// The below example illustrates the behavior of the envbuilder_cached_image |
| 2 | +// resource. |
| 3 | +// 1) Run a local registry: |
| 4 | +// |
| 5 | +// ```shell |
| 6 | +// docker run -d -p 5000:5000 --name test-registry registry:2 |
| 7 | +// ``` |
| 8 | +// |
| 9 | +// 2) Running a `terraform plan` should result in the following outputs: |
| 10 | +// |
| 11 | +// ``` |
| 12 | +// + builder_image = "ghcr.io/coder/envbuilder-preview:latest" |
| 13 | +// + exists = (known after apply) |
| 14 | +// + id = (known after apply) |
| 15 | +// + image = (known after apply) |
| 16 | +// ``` |
| 17 | +// |
| 18 | +// 3) Running `terraform apply` should result in outputs similar to the below: |
| 19 | +// |
| 20 | +// ``` |
| 21 | +// builder_image = "ghcr.io/coder/envbuilder-preview:latest" |
| 22 | +// exists = false |
| 23 | +// id = "00000000-0000-0000-0000-000000000000" |
| 24 | +// image = "ghcr.io/coder/envbuilder-preview:latest" |
| 25 | +// ``` |
| 26 | +// |
| 27 | +// 4) Populate the cache by running Envbuilder and pushing the built image to |
| 28 | +// the local registry: |
| 29 | +// |
| 30 | +// ```shell |
| 31 | +// docker run -it --rm \ |
| 32 | +// -e ENVBUILDER_CACHE_REPO=localhost:5000/test \ |
| 33 | +// -e ENVBUILDER_GIT_URL=https://github.com/coder/envbuilder-starter-devcontainer \ |
| 34 | +// -e ENVBUILDER_PUSH_IMAGE=true \ |
| 35 | +// -e ENVBUILDER_INIT_SCRIPT=exit \ |
| 36 | +// --net=host \ |
| 37 | +// ghcr.io/coder/envbuilder-preview:latest |
| 38 | +// ``` |
| 39 | +// |
| 40 | +// 5) Run `terraform plan` once more. Now, the cached image will be detected: |
| 41 | +// |
| 42 | +// ``` |
| 43 | +// Note: Objects have changed outside of Terraform |
| 44 | +// |
| 45 | +// Terraform detected the following changes made outside of Terraform since the last "terraform apply" which may have affected this plan: |
| 46 | +// envbuilder_cached_image.example has been deleted |
| 47 | +// - resource "envbuilder_cached_image" "example" { |
| 48 | +// - exists = false -> null |
| 49 | +// - id = "00000000-0000-0000-0000-000000000000" -> null |
| 50 | +// - image = "ghcr.io/coder/envbuilder-preview:latest" -> null |
| 51 | +// # (5 unchanged attributes hidden) |
| 52 | +// ``` |
| 53 | +// |
| 54 | +// 6) Run `terraform apply` and the newly pushed image will be saved in the Terraform state: |
| 55 | +// ```shell |
| 56 | +// builder_image = "ghcr.io/coder/envbuilder-preview:latest" |
| 57 | +// exists = true |
| 58 | +// id = "sha256:xxx..." |
| 59 | +// image = "localhost:5000/test@sha256:xxx..." |
| 60 | +// ``` |
| 61 | + |
| 62 | +terraform { |
| 63 | + required_providers { |
| 64 | + envbuilder = { |
| 65 | + source = "coder/envbuilder" |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +resource "envbuilder_cached_image" "example" { |
| 71 | + builder_image = "ghcr.io/coder/envbuilder-preview:latest" |
| 72 | + git_url = "https://github.com/coder/envbuilder-starter-devcontainer" |
| 73 | + cache_repo = "localhost:5000/test" |
| 74 | + extra_env = { |
| 75 | + "ENVBUILDER_VERBOSE" : "true" |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +output "builder_image" { |
| 80 | + value = envbuilder_cached_image.example.builder_image |
| 81 | +} |
| 82 | + |
| 83 | +output "exists" { |
| 84 | + value = envbuilder_cached_image.example.exists |
| 85 | +} |
| 86 | + |
| 87 | +output "id" { |
| 88 | + value = envbuilder_cached_image.example.id |
| 89 | +} |
| 90 | + |
| 91 | +output "image" { |
| 92 | + value = envbuilder_cached_image.example.image |
| 93 | +} |
0 commit comments