Gcloud, Cloud build: 一次性更新所有 Cloud Run 服务的新镜像

huangapple go评论67阅读模式
英文:

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

问题

我有一个Cloud Build触发器,当在主分支上推送新的提交时,会将一个镜像推送到容器注册表。

当这种情况发生时,我想要使用新创建的镜像更新所有我的Cloud Run作业。

- 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

问题在于我有类似N个作业ID(job-id1; job-id2 .... job-idN)。是否有一种方法可以迭代我作为替代变量提供的数组,以一次更新所有作业?

英文:

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 ?

答案1

得分: 1

      - 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: 部署作业
        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          
英文:

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

huangapple
  • 本文由 发表于 2023年2月9日 00:12:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75388626.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定