在GCP中,如何通过Cloudbuild确保仅触发文件中已发生更改的那些步骤

时间:2020-04-03 12:06:34

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

我的问题是,给定以下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  

3 个答案:

答案 0 :(得分:1)

您不能从一个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

答案 1 :(得分:0)

对于您的用例而言,最好的方法是让不同的触发器(在您的用例中为3个)监听不同的标记或分支,每个触发器都特定于您要监听的文件更改。当前,当某些文件更改不可用时,执行Cloud Build步骤。

答案 2 :(得分:0)

如果你想用 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/**"