I deploy my service on cloud run, in several regions.
My cloudbuild.yaml
does this:
- Build a new image
- Push new image to Artifact Registry
- TODO: Stop service in all regions
- Run database migrations
- Deploy service in europe-west9
- Deploy service in europe-west8
During step 3, I want to make sure my service is stopped in all regions it is currently deployed, because I will perform migrations on the databases.
The command to delete a service is gcloud run services delete <service-name> --platform=managed --region=europe-west9
. (Documentation)
And inside my cloudbuild.yaml it looks like that:
- id: "Stop all services"
name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
entrypoint: gcloud
args: [
'run', 'services', 'delete', "${_SERVICE_NAME}", '--platform=managed', "--region=europe-west9",
]
I need a way to delete the service in all regions.
I thought of doing:
- get all regions with
gcloud run regions list
(Documentation). - iterate on result and run
gcloud run services delete .....--region=...
on each region.
But I have 2 issues:
gcloud run regions list
will return all regions available in Cloud Run. So I need to filter on my${_SERVICE_NAME}
, but I don’t understand how to use--filter
for that purpose.- I have no idea how to “iterate” in my
cloudbuild.yaml
.
Full (simplified) cloudbuild.yaml:
steps:
# - Build new image
- id: "Build"
name: "gcr.io/cloud-builders/docker"
entrypoint: 'bash'
args: [
'-c',
"docker build -t ${_ARTIFACT_REGISTRY_REPOSITORY}/${_SERVICE_NAME}:$SHORT_SHA ."
]
# - Push new image to artifact registry
- id: "Push"
name: "gcr.io/cloud-builders/docker"
args: ["push", "${_ARTIFACT_REGISTRY_REPOSITORY}/${_SERVICE_NAME}:$SHORT_SHA"]
# - Stop all existing Cloud Run services (Paris, Milan, annd maybe more)
- id: "Stop all services"
name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
entrypoint: gcloud
args: [
'run', 'services', 'delete', "${_SERVICE_NAME}", '--platform=managed', "--region=europe-west9",
]
# - Apply database migrations
- id: "apply migrations"
name: "gcr.io/google-appengine/exec-wrapper"
entrypoint: "bash"
args:
[
"-c",
"/buildstep/execute.sh -i ${_ARTIFACT_REGISTRY_REPOSITORY}/${_SERVICE_NAME}:$SHORT_SHA -s ${_CLOUD_SQL_CONNECTION_NAME} -- bundle exec rails db:migrate"
]
# - Launch paris instance
- id: "Run Paris"
name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
entrypoint: gcloud
args: [
'run', 'deploy', "${_SERVICE_NAME}",
'--image', '${_ARTIFACT_REGISTRY_REPOSITORY}/${_SERVICE_NAME}:$SHORT_SHA',
'--region', 'europe-west9',
]
# - Launch Milan instance
- id: "Run Milan"
name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
entrypoint: gcloud
args: [
'run', 'deploy', "${_SERVICE_NAME}",
'--image', '${_ARTIFACT_REGISTRY_REPOSITORY}/${_SERVICE_NAME}:$SHORT_SHA',
'--region', 'europe-west8',
]
options:
logging: CLOUD_LOGGING_ONLY
images:
- "${_ARTIFACT_REGISTRY_REPOSITORY}/${_SERVICE_NAME}:$SHORT_SHA"