Search code examples
google-cloud-platformyamlgoogle-cloud-rungoogle-cloud-build

Gcloud, Cloud build: Update all cloud run services with new image at once


I have a Cloud Build trigger that push an image to container registry when a new commit is pushed on master branch.

When this occurs, I would like to update all my cloud run jobs with newly created image.

  - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:slim'
    entrypoint: gcloud
    args:
      - beta
      - run
      - jobs
      - update
      - $_JOB_ID_1
      - '--platform=managed'
      - '--image=$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA'
      - >-
        --labels=managed-by=gcp-cloud-build-deploy-cloud-run,commit-sha=$COMMIT_SHA,gcb-build-id=$BUILD_ID,gcb-trigger-id=$_TRIGGER_ID
      - '--region=$_DEPLOY_REGION'
      - '--quiet'
    id: Deploy

The problem is that I have like N jobs ids (job-id1; job-id2 .... job-idN). Is there a way to iterate over an array I would give as substitution variable to update all jobs at once ?


Solution

  • I could do it like this:

      - name: gcr.io/google.com/cloudsdktool/cloud-sdk
        env:
          - HOST=$_GCR_HOSTNAME
          - PROJECT=$PROJECT_ID
          - REPO=$REPO_NAME
          - SERVICE=$_SERVICE_NAME
          - COMMIT=$COMMIT_SHA
        id: Deploy Jobs
        script: |
          #!/usr/bin/env bash
          jobs=$(gcloud beta run jobs list --format="value(name)")
          for job in $jobs; do
            if [[ $job == *"prod"* ]]; then
              gcloud beta run jobs update $job --image=$HOST/$PROJECT/$REPO/$SERVICE:$COMMIT --region=europe-southwest1
            fi
          done