如何使用云构建从monorepo部署多个功能,但一次只能部署一个

2024-03-29 08:31:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Python编写的多个云函数建立monorepo。我目前使用的云构建和结构如下:

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

使用此云构建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

设置触发器以监视相应子文件夹中的更改,将函数名作为env变量注入并部署正确的函数。这就是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是一项相当年轻的服务,变化非常迅速,但现在有没有办法安排文件夹或设置Cloud Build YAML,以便正确部署功能?我真的不想为每个100行函数创建一个单独的存储库


Tags: 函数namebuildyamlsourcemaingooglefunction
1条回答
网友
1楼 · 发布于 2024-03-29 08:31:00

我无法通过云功能+云构建重现您的问题。结构如下:

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

以及以下{}:

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标志

相关问题 更多 >