【问题标题】:How to deploy multiple functions from a monorepo using Cloud Build but only one at a time如何使用 Cloud Build 从 monorepo 部署多个功能,但一次只能部署一个
【发布时间】:2020-03-04 21:36:52
【问题描述】:

我正在尝试建立一个带有多个用 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 的 setup 思路:

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,只是不断给我错误,例如:

错误:(gcloud.functions.deploy)参数--source:提供的目录不存在

当我尝试这些值时会发生此错误:

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

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

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

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

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

【问题讨论】:

  • 你传递的--source是什么?
  • @DustinIngram 你是对的,我忘了把它放进去——谢谢你的提醒。我现在编辑了问题以提供该信息。
  • 为什么使用 terraform 来触发 CloudBuild?为什么不直接触发 Cloud Build?或者您为什么不使用 Terraform 部署您的功能?我错过了一些东西。
  • @guillaumeblaquiere TF 用于配置所有其他基础设施(VPC、GKE、Vms 等)。 Cloud Build 的配置包含在 CI/CD 模块中。该函数使用 cloudbuild.yaml 部署。我相信这对于从 monorepo 部署多个功能的实际问题来说并不是一个重要的细节,但如果你认为是,我很乐意倾听。
  • @guillaumeblaquiere 抱歉,我可能误解了 - TF 只定义了 Cloud Build 触发器,它没有触发它。 Cloud Build 在代码推送时自动触发。就像我通过gcloud 或 UI 控制台定义的一样。

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


【解决方案1】:

我无法仅使用 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 标志是否设置不正确?

【讨论】:

  • 这听起来很疯狂。但我无法让你的代码工作。然后...我将--source 作为最后一个参数,它起作用了。您介意尝试将--source 放在--trigger-http 之前,看看是否会遇到同样的错误吗?或者我是否遗漏了文档中明确指出如果订单不同它将不起作用的内容?
  • 顺序无关紧要。更改订单并没有导致我失败。
  • 有趣。好吧,谢谢您的帮助-我再次取消了订单,但它确实失败了,所以如果有兴趣了解原因(出于内部 GCP 目的),我很乐意协助处理日志等 - 我在官方 GCP Slack,所以我很乐意跟进。我将您的答案标记为已接受,以便在此处结束!
  • 我唯一能想到的,也许你的源字符串中有换行符?
  • 我对此表示怀疑 - 我将其作为 $_FUNCTION_NAME 传递,这是 Cloud Build 中的一个变量。正如您在我的 TF 文件中看到的那样,它以_FUNCTION_NAME = "first_function" 传递。那里似乎没有新线。当然,除非 terraform 添加它(我猜不太可能?)。
猜你喜欢
  • 1970-01-01
  • 2021-03-29
  • 1970-01-01
  • 2010-12-08
  • 1970-01-01
  • 2019-03-14
  • 2018-08-20
  • 2020-09-23
  • 1970-01-01
相关资源
最近更新 更多