如何在云构建yaml文件中指定默认云运行修订版本url

问题描述 投票:0回答:2

我有一个 GCP 项目,我使用云构建触发器不断将对 GitHub 存储库所做的更改 (PR) 部署到云运行服务
我一开始设置的方式是使用 GCP GUI enter image description here

这会导致云构建中的触发器\ 云构建触发器具有如下所示的 yaml 文件

  - name: gcr.io/cloud-builders/docker
    args:
      - build
      - '--no-cache'
      - '-t'
      - '$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA'
      - .
      - '-f'
      - Dockerfile
    id: Build
  - name: gcr.io/cloud-builders/docker
    args:
      - push
      - '$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA'
    id: Push
  - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:slim'
    args:
      - run
      - services
      - update
      - $_SERVICE_NAME
      - '--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,$_LABELS
      - '--region=$_DEPLOY_REGION'
      - '--quiet'
    id: Deploy
    entrypoint: gcloud
images:
  - '$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA'
options:
  substitutionOption: ALLOW_LOOSE
substitutions:
  _PLATFORM: managed
  _SERVICE_NAME: bordereau
  _DEPLOY_REGION: europe-west1
  _LABELS: gcb-trigger-id=((a long random id goes here))
  _TRIGGER_ID: ((an other long random id goes here))
  _GCR_HOSTNAME: eu.gcr.io
tags:
  - gcp-cloud-build-deploy-cloud-run
  - gcp-cloud-build-deploy-cloud-run-managed
  - bordereau

每当运行此触发器时,就会创建一个新的云运行修订版,如下所示 enter image description here

然后我可以创建一个指向特定网址的网址,如下所示 enter image description here

enter image description here

enter image description here

这可以帮助我使用其唯一的 URL 访问每个修订版本
我尝试了多种方法来编辑云构建 YAML 文件,以自动为每个修订版提供唯一的 URL(而不是通过 GCP GUI 手动),但我似乎找不到方法!我尝试了很多关键字,并阅读了文档,但这也没有帮助!
非常感谢任何帮助。
如果修订 URL(标签)是独特且简短的内容(如提交 SHA 的第一个字符或 PR 编号),那就太好了

google-cloud-platform continuous-deployment google-cloud-run google-cloud-build cloudbuild.yaml
2个回答
1
投票

通常你可以这样做(参见步骤id:

tag

  - name: gcr.io/cloud-builders/docker
    args:
      - build
      - '--no-cache'
      - '-t'
      - '$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA'
      - .
      - '-f'
      - Dockerfile
    id: Build
  - name: gcr.io/cloud-builders/docker
    args:
      - push
      - '$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA'
    id: Push
  - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:slim'
    args:
      - run
      - services
      - update
      - $_SERVICE_NAME
      - '--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,$_LABELS
      - '--region=$_DEPLOY_REGION'
      - '--quiet'
    id: Deploy
    entrypoint: gcloud
  - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:slim'
    args:
      - -c
      - |
         export sha=$COMMIT_SHA
         export CUSTOM_TAG=${sha:0:8}
         export CURRENT_REV=$(gcloud alpha run services describe $_SERVICE_NAME --region=$_DEPLOY_REGION --platform=managed --format='value(status.traffic[0].revisionName)')
         gcloud run services update-traffic $_SERVICE_NAME --set-tags=$$CUSTOM_TAG=$$CURRENT_REV --region=$_DEPLOY_REGION --platform=managed
    id: tag
    entrypoint: bash
images:
  - '$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA'
options:
  substitutionOption: ALLOW_LOOSE
substitutions:
  _PLATFORM: managed
  _SERVICE_NAME: bordereau
  _DEPLOY_REGION: europe-west1
  _LABELS: gcb-trigger-id=((a long random id goes here))
  _TRIGGER_ID: ((an other long random id goes here))
  _GCR_HOSTNAME: eu.gcr.io
tags:
  - gcp-cloud-build-deploy-cloud-run
  - gcp-cloud-build-deploy-cloud-run-managed
  - bordereau

在该自定义标签中,我放置了提交 SHA 的第 8 个字符

您可以注意到奇怪的环境变量 COMMIT_SHA 复制到本地环境变量。 CloudBuild 是一件奇怪的事情。


0
投票

我认为最简单的方法是在部署到 GCR 期间添加幂等修订标签。 确保 PR 的 REVISION_TAG 是幂等的,无论 PR 中的提交如何。这样你就是金色的。

steps:
  # ... Other steps here

  # Deploy to Cloud Run with a unique revision for PR or regular branch
  - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:slim'
    args:
      - run
      - services
      - update
      - ${_SERVICE_NAME}
      - '--platform=managed'
      - >-
        --image=$_AR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:${_TAG}
      - >-
        --labels=commit-sha=$COMMIT_SHA,gcb-build-id=$BUILD_ID,gcb-trigger-id=$_TRIGGER_ID
      - '--region=$_DEPLOY_REGION'
      - '--tag=${_REVISION_TAG}' # This could make the next step redundant
      - '--quiet'
    id: Deploy to Cloud Run
    entrypoint: gcloud

    # ... Other steps here

substitutions:
  _TAG: '${_PR_NUMBER:=${COMMIT_SHA}}' # If PR_NUMBER is empty, use COMMIT_SHA
  _REVISION_TAG: 'rev-${_PR_NUMBER:=${COMMIT_SHA:5}}' # Starting tag with number gives error

注意,我保留图像标签和修订标签有点不同。

之前(查看

rev-134
标签并忽略
pr-134
标签) Before a commit to #134After a commit to #134

© www.soinside.com 2019 - 2024. All rights reserved.