diff --git a/README.md b/README.md index b556ecb35dd9634e0004cdbad13cbbd53a19fb48..bf44158aaa4ce6dbf0bc0254656a97407bb73163 100644 --- a/README.md +++ b/README.md @@ -171,18 +171,5 @@ module "webapp" { Originally, the module did not deploy images except on the very first use (using `gcr.io/cloudrun/hello:latest`). -Currently, deploy the image from `image_name` if specified, otherwise keep the +Currently, the module deploys the image from `image_name` if specified, otherwise keep the current image, if that doesn't exist fall-back to the "hello" image. - -The name of the currently deployed image needs to be sourced outside of -this module and supplied as `existing_image_name` to prevent a cyclic dependency -problem. This can be extracted from an existing deployment and included in the -root module with: - -```tf -existing_image_name = ( - data.google_cloud_run_service.webapp.template != null ? - data.google_cloud_run_service.webapp.template[0].spec[0].containers[0].image : - "" -) -``` diff --git a/locals.tf b/locals.tf index 8b5966323b050e90dddcc52d0fabed6f7da34e02..5578dd3926c02d040e2eb0d286ef45e00355a7f4 100644 --- a/locals.tf +++ b/locals.tf @@ -23,4 +23,11 @@ locals { [trimsuffix(trimprefix(google_cloud_run_service.webapp.status[0].url, "https://"), "/")], local.can_monitor_custom_dns ? [var.dns_name] : [] ) + + # If a cloud run revision is present, get the full URL of the deployed image. + existing_image_name = ( + data.google_cloud_run_service.webapp.template != null ? + data.google_cloud_run_service.webapp.template[0].spec[0].containers[0].image : + "" + ) } diff --git a/main.tf b/main.tf index cd9dad2e485a429293283ff2f7d5af850d263676..0194526e2a4d2610434fc7e48d32be4d342e9e0d 100644 --- a/main.tf +++ b/main.tf @@ -113,7 +113,7 @@ resource "google_cloud_run_service" "webapp" { containers { image = coalesce( var.image_name, - var.existing_image_name, + local.existing_image_name, "gcr.io/cloudrun/hello:latest", ) @@ -275,3 +275,11 @@ module "uptime_monitoring" { google = google.stackdriver } } + +# This extracts information about any currently running Cloud Run revision before +# starting the plan walk. This is current behaviour, but may change in future see +# https://github.com/hashicorp/terraform/issues/17034. +data "google_cloud_run_service" "webapp" { + name = "webapp" + location = var.cloud_run_region +} diff --git a/variables.tf b/variables.tf index 4165abc1ba5cd0cd7ad1d5b24e82364a9549c098..6544989282227243b8cffd97c212378035af2b23 100644 --- a/variables.tf +++ b/variables.tf @@ -243,16 +243,7 @@ variable "image_name" { type = string default = "" description = <<-EOL - The docker image that will be deployed. If unset, the value of existing_image_name - is preferred. -EOL -} - -variable "existing_image_name" { - type = string - default = "" - description = <<-EOL - The image currently deployed, which needs to be passed into this module to - avoid a cyclic dependency. If unset, the hello container will be deployed. + Optional. The docker image that will be deployed. If unset, the value of + existing_image_name is preferred. EOL }