Search code examples
google-cloud-platformgoogle-cloud-rungoogle-container-registry

Deploy multiple Cloudrun service with same dockerimage


There are 25+ Cloudrun services that use the same docker image(from GCR) but are configured with different variables. What is an easy and reliable method to deploy all the services with the latest container image from any kind of incoming events? Currently using below CLI command to execute one by one manually. Is there any automated way to implement auto deployment for all the service one after another or in parallel.

gcloud run deploy SERVICE --image IMAGE_URL

Addn: Labels are been used to mark the 25 containers which have the same container images. Not required to build docker image everytime from source. The same image can be used.


Solution

  • In case Terraform is an option for you, you can automate all Cloud Run services deployment using either with the count or for_each meta-arguments:

    count if you need the same service name with indexes

    provider "google" {
        project = "MY-PROJECT-ID"
    }
    
    resource "google_cloud_run_service" "default" {
        count    = 25
        name     = "MY-SERVICE-${count.index}"
        location = "MY-REGION"
    
        metadata {
          annotations = {
            "run.googleapis.com/client-name" = "terraform"
          }
        }
    
        template {
          spec {
            containers {
              image = "IMAGE_URL"
            }
          }
        }
     }
    
     data "google_iam_policy" "noauth" {
       binding {
         role = "roles/run.invoker"
         members = ["allUsers"]
       }
     }
    
     resource "google_cloud_run_service_iam_policy" "noauth" {
       for_each    = google_cloud_run_service.default
       location    = each.value.location
       project     = each.value.project
       service     = each.value.name
    
       policy_data = data.google_iam_policy.noauth.policy_data
     }
    

    where MY-PROJECT-ID and MY-REGION needs to be replaced with your project specific values.

    for_each if you need different service names

    provider "google" {
        project = "MY-PROJECT-ID"
    }
    
    resource "google_cloud_run_service" "default" {
        for_each = toset( ["Service 1", "Service 2", "Service 25"] )
        name     = each.key
        location = "MY-REGION"
    
        metadata {
          annotations = {
            "run.googleapis.com/client-name" = "terraform"
          }
        }
    
        template {
          spec {
            containers {
              image = "IMAGE_URL"
            }
          }
        }
     }
    
     data "google_iam_policy" "noauth" {
       binding {
         role = "roles/run.invoker"
         members = ["allUsers"]
       }
     }
    
     resource "google_cloud_run_service_iam_policy" "noauth" {
       for_each    = google_cloud_run_service.default
       location    = each.value.location
       project     = each.value.project
       service     = each.value.name
    
       policy_data = data.google_iam_policy.noauth.policy_data
     }
    

    where MY-PROJECT-ID and MY-REGION needs to be replaced with your project specific values as well.

    You can refer to the official GCP Cloud Run documentation for further details on Terraform usage.