Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用云构建从monorepo部署多个功能,但一次只能部署一个_Python_Google Cloud Platform_Google Cloud Functions_Gcloud_Google Cloud Build - Fatal编程技术网

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

Python 如何使用云构建从monorepo部署多个功能,但一次只能部署一个,python,google-cloud-platform,google-cloud-functions,gcloud,google-cloud-build,Python,Google Cloud Platform,Google Cloud Functions,Gcloud,Google Cloud Build,我正在尝试使用Python编写的多个云函数建立monorepo。我目前使用的云构建和结构如下: . ├── deployment │ └── cloudbuild.yaml ├── main.py └── requirements.txt 使用此云构建YAML代码可以很好地部署: steps: - name: 'gcr.io/cloud-builders/gcloud' args: [ 'functions', 'deploy', '$_FUNCTION_NAME'

我正在尝试使用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
.
├── cloudbuild.yaml
├── first_function
│   ├── main.py
│   └── requirements.txt
└── second_function
    ├── main.py
    └── requirements.txt
设置触发器以监视相应子文件夹中的更改,将函数名作为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
的所有安排都会不断地给我错误,例如:

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

尝试以下值时会发生此错误:

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

当未定义任何
--source
时,我得到的错误是:

错误:(gcloud.functions.deploy)操作错误:代码=3,消息=生成失败:生成错误详细信息不可用


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

我无法用云函数+云构建重现您的问题。结构如下:

.
├── first_function
│   ├── main.py
│   └── requirements.txt
├── second_function
│   ├── main.py
│   └── requirements.txt
└── cloudbuild.yaml
.
├── 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
?@DustinIngram您是对的,我忘了输入-谢谢提醒。我现在编辑了这个问题来提供这些信息。为什么要使用terraform来触发CloudBuild?为什么不直接触发云构建?或者为什么不使用Terraform部署功能?我遗漏了一些东西。@guillaumeblaquiere TF用于配置所有其他infra(VPC、GKE、VM等)。云构建的配置包含在CI/CD模块中。该函数是使用cloudbuild.yaml部署的。我相信这并不是从monorepo部署多个功能的实际问题的一个重要细节,但如果你认为是,我很乐意听你说。@guillaumeblaquiere抱歉,我可能误解了-TF只定义了云构建触发器,它不会触发它。云构建在代码推送时自动触发。这与我通过
gcloud
或UI控制台定义它一样。听起来可能很疯狂。但我无法让你的代码工作。然后…我把
--source
作为最后一个参数,它就工作了。您是否介意尝试将
--source
放在
--trigger http
之前,以查看是否出现相同的错误?或者我在文档中遗漏了一些明确说明,如果顺序不同,它将不起作用的内容吗?顺序应该不重要。对我来说,改变顺序并没有导致失败。很有趣。好的,谢谢你的帮助-我再次翻转了订单,但我确实失败了,因此如果有兴趣了解原因(出于内部GCP目的),我很乐意协助日志等-我处于官方GCP松弛状态,因此我很乐意跟进。我将你的答案标记为已接受,以便在这里结束!我能想到的唯一一件事是,可能您的源字符串中有一个换行符?我对此表示怀疑-我将其作为
$\u FUNCTION\u NAME
传递,这是Cloud Build中的一个变量。正如您在我的TF文件中看到的,它作为
\u FUNCTION\u NAME=“first\u FUNCTION”
传递。看起来不像是一条新的路线。当然,除非terraform添加了它(我想不太可能吧?)。