如何使用 Cloud Build 从 monorepo 部署多个功能,但一次只能部署一个

How to deploy multiple functions from a monorepo using Cloud Build but only one at a time

我正在尝试使用 Python 中编写的多个云函数来设置一个 monorepo。我目前正在使用 Cloud Build 和这样的结构:

.
├── deployment
│   └── cloudbuild.yaml
├── main.py
└── requirements.txt

使用此 Cloud Build YAML 代码可以很好地部署:

steps:
  - name: 'gcr.io/cloud-builders/gcloud'
    args: [
      'functions', 'deploy', '$_FUNCTION_NAME',
      '--trigger-resource', '$_TRIGGER_RESOURCE',
      '--trigger-event', '$_TRIGGER_EVENT',
      '--runtime', 'python37',
      '--memory', '1024MB',
      '--region', 'europe-west1'
    ]

现在我的意图是转向这个结构:

.
├── first_function
│   ├── main.py
│   └── requirements.txt
├── second_function
│   ├── main.py
│   └── requirements.txt
└── cloudbuild.yaml

设置触发器以观察各个子文件夹内的变化,将函数名称作为环境变量注入并部署正确的函数。这是TF的设置思路:

resource "google_cloudbuild_trigger" "first_function_trigger" {
  project = google_project.my_project.name
  name = "trigger-first-function"
  description = "Trigger for deploying first function"

  trigger_template {
    repo_name = google_sourcerepo_repository.functions.name
    branch_name = "master"
    dir = "first_function/**"
  }

  substitutions = {
    _TRIGGER_RESOURCE = google_storage_bucket.my_bucket.name
    _TRIGGER_EVENT = "google.storage.object.finalize"
    _FUNCTION_NAME = "first_function"
  }

  filename = "cloudbuild.yaml"
}

然而,问题在于:

所有安排,在gcloud functions deploy命令中指定--source,只是不断给我错误,例如:

ERROR: (gcloud.functions.deploy) argument --source: Provided directory does not exist

当我尝试这些值时出现此错误:

1. --source=.
2. --source=./first_function
3. --source=./first_function/

当从根文件夹调用 gcloud functions deploy 时,第三个在本地工作。我阅读了有关在 GCP 中指定存储库的方法 - 但这是一个额外的数据加载操作,不是吗?源代码已经存在 - 这是存储库中更改的触发器。

当没有定义 --source 时,这是我得到的错误:

ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: Build error details not available

我知道 Cloud Build 是一项相当年轻的服务并且变化非常快,但是现在有没有办法安排文件夹或设置云构建 YAML 以便正确部署功能?我真的不想为每个 100 行函数创建一个单独的存储库。

我无法仅使用 Cloud Functions + Cloud Build 重现您的问题。具有以下结构:

.
├── cloudbuild.yaml
├── first_function
│   ├── main.py
│   └── requirements.txt
└── second_function
    ├── main.py
    └── requirements.txt

以及以下cloudbuild.yaml

steps:
  - name: 'gcr.io/cloud-builders/gcloud'
    args: [
      'functions', 'deploy', 'first_function',
      '--trigger-http',
      '--runtime', 'python37',
      '--region', 'us-central1',
      '--source', 'first_function'
    ]
  - name: 'gcr.io/cloud-builders/gcloud'
    args: [
      'functions', 'deploy', 'second_function',
      '--trigger-http',
      '--runtime', 'python37',
      '--region', 'us-central1',
      '--source', 'second_function'
    ]

我能够部署这两个功能。

是否有可能 source 标志设置不正确?