【问题标题】:In GCP, through Cloudbuild how can I ensure that only those steps get triggered for which changes have happened in file在 GCP 中,通过 Cloudbuild 如何确保仅触发文件中发生更改的那些步骤
【发布时间】:2020-07-15 14:29:14
【问题描述】:

我的问题是,鉴于以下 yaml 文件,如果我在“dir: process/cbd-bu-data”的任何文件中进行更改,Cloud Build 在触发时会依次运行所有步骤。这会浪费时间。

我希望只有该步骤在 cloudbuild 中运行,在该目录的文件中已对其进行了更改。我应该怎么做才能做到这一点?

这是我的cloudbuild.yaml 文件:

steps: 
  - args: 
      - beta
      - functions
      - deploy
      - "--runtime=python37"
      - "--trigger-http"
      - "--entry-point=process_cbd_group_data"
      - process_cbd_group_data
      - "--region=us-central1"
    dir: process/cbd-group-data
    name: gcr.io/cloud-builders/gcloud
  - args: 
      - beta
      - functions
      - deploy
      - "--runtime=python37"
      - "--trigger-http"
      - "--entry-point=process_cbd_bu_data"
      - process_cbd_bu_data
      - "--region=us-central1"
    dir: process/cbd-bu-data
    name: gcr.io/cloud-builders/gcloud
  - args: 
      - beta
      - functions
      - deploy
      - "--runtime=python37"
      - "--trigger-http"
      - "--entry-point=process_cbd_structure_data"
      - process_cbd_structure_data
      - "--region=us-central1"
    dir: process/cbd-structure-data
    name: gcr.io/cloud-builders/gcloud  

【问题讨论】:

  • 这些文件在哪里?触发器的来源是什么?触发器上的更多上下文会更好。虽然似乎对触发器进行分片是最好的方法。
  • 文件的位置存在于每个步骤的标签“dir”中。为云存储库设置任何分支更改的触发器。
  • 对于您的用例,那么最好的方法是使用不同的触发器(在您的用例中为 3 个)来监听不同的标签或分支,这些触发器中的每一个都特定于您想要监听的文件更改到。目前在某些文件更改不可用时执行 Cloud Build 步骤。
  • 嗨,@bhito,您能否添加您的评论作为答案,以便社区可以通过它获得更多可见性而从中受益?
  • @asbovelw 我已经完成了,谢谢!

标签: google-cloud-platform google-cloud-functions google-cloud-build


【解决方案1】:

对于您的用例,最好的方法是使用不同的触发器(在您的用例中为 3 个)来侦听不同的标签或分支,这些触发器中的每一个都特定于您要侦听的文件更改。目前在某些文件更改不可用时执行 Cloud Build 步骤。

【讨论】:

    【解决方案2】:

    您无法通过一个 cloudbuild 执行此操作。您可以使用 --included-files 选项创建三个不同的构建触发器。我认为用分支或标签来完成同样的事情并不方便,就像我在另一个答案中读到的那样。阅读documentation了解更多详情。

    您的 git 存储库布局:

    function_one/
       main.py
       cloudbuild.yaml
    
    function_two/
       main.py
       cloudbuild.yaml
    
    function_three/
       main.py
       cloudbuild.yaml
    
    cloudbuild.yaml
    

    父cloudbuild.yaml的布局:

    steps:
      - name: 'gcr.io/cloud-builders/gcloud'
        entrypoint: 'bash'
        args:
          - '-c'
          - |
            cloud beta builds triggers create github build_one --included-files "function_one/*" --repo-name=XXX --repo-owner=XXX --branch-pattern=$BRANCH_NAME
            cloud beta builds triggers create github build_two --included-files "function_two/*" --repo-name=XXX --repo-owner=XXX --branch-pattern=$BRANCH_NAME
            cloud beta builds triggers create github build_three --included-files "function_three/*" --repo-name=XXX --repo-owner=XXX --branch-pattern=$BRANCH_NAME
    

    子cloudbuild.yaml的布局:

    steps: 
      - args: 
          - functions
          - deploy
          - "--runtime=python37"
          - "--trigger-http"
          - "--entry-point=process_cbd_group_data"
          - process_cbd_group_data
          - "--region=us-central1"
        name: gcr.io/cloud-builders/gcloud
    

    【讨论】:

      【解决方案3】:

      如果您想使用 gcloud CLI 进行操作

      gcloud beta builds triggers create cloud-source-repositories \
      --repo=REPO_NAME \
      --branch-pattern=BRANCH_PATTERN \ # or --tag-pattern=TAG_PATTERN
      --build-config=BUILD_CONFIG_FILE \
      --substitutions=_VARIABLE="VALUE"\ 
      --included-files "DIRECTORY_NAME/**"
      

      注意:-
      --included-files "directory_name/**" 将递归检测所有目录和文件。
      --included-files "directory_name/*" 将只查找该特定目录中的文件。

      例子:-

      gcloud beta builds triggers create cloud-source-repositories \
      --repo=test-repo \
      --branch-pattern=master \
      --build-config=workflows/cloudbuild.yaml \
      --substitutions=_REGION="asia-southeast1"\
      --included-files "src/**"
      

      【讨论】:

        猜你喜欢
        • 2019-12-25
        • 2022-10-17
        • 2021-03-28
        • 2020-12-29
        • 1970-01-01
        • 1970-01-01
        • 2020-05-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多