gpt4 book ai didi

google-cloud-platform - 如何部署使用google cloud build和Source Repository新推送的多个云函数?

转载 作者:行者123 更新时间:2023-12-05 08:11:20 27 4
gpt4 key购买 nike

我有一个包含不同云功能文件夹的项目文件夹,例如

Project_Folder
-Cloud-Function-Folder1
-main.py
-requirements.txt
-cloudbuild.yaml
-Cloud-Function-Folder2
-main.py
-requirements.txt
-cloudbuild.yaml
-Cloud-Function-Folder3
-main.py
-requirements.txt
-cloudbuild.yaml
--------- and so on!

现在我所拥有的是。我将代码从Cloud Fucntions文件夹到Source Repository(每个函数文件夹的单独Repos)一个一个地推送到Source Repository。然后它启用了触发云构建然后部署功能的触发器。我拥有的 cloudbuild.yaml 文件如下所示。

 steps:

- name: 'python:3.7'
entrypoint: 'bash'
args:
- '-c'
- |
pip3 install -r requirements.txt
pytest

- name: 'gcr.io/cloud-builders/gcloud'
args:
- functions
- deploy
- Function
- --runtime=python37
- --source=.
- --entry-point=function_main
- --trigger-topic=Function
- --region=europe-west3

现在,我想做的是创建一个单一的源代码库,每当我更改一个云函数中的代码并推送它时,只有它得到部署,其余部分仍然像以前一样。


更新

现在我也尝试了下面类似的方法,但它也会同时部署所有功能,即使我正在处理一个功能。

Project_Folder
-Cloud-Function-Folder1
-main.py
-requirements.txt
-Cloud-Function-Folder2
-main.py
-requirements.txt
-Cloud-Function-Folder3
-main.py
-requirements.txt
-cloudbuild.yaml
-requirements.txt

cloudbuild.yaml 文件如下所示

 steps:

- name: 'python:3.7'
entrypoint: 'bash'
args:
- '-c'
- |
pip3 install -r requirements.txt
pytest

- name: 'gcr.io/cloud-builders/gcloud'
args:
- functions
- deploy
- Function1
- --runtime=python37
- --source=./Cloud-Function-Folder1
- --entry-point=function1_main
- --trigger-topic=Function1
- --region=europe-west3

- name: 'gcr.io/cloud-builders/gcloud'
args:
- functions
- deploy
- Function2
- --runtime=python37
- --source=./Cloud-Function-Folder2
- --entry-point=function2_main
- --trigger-topic=Function2
- --region=europe-west3

最佳答案

它更复杂,您必须玩弄 Cloud Build 的限制和约束。

我这样做:

  • 获取自上次提交后更新的目录
  • 在这个目录上循环并做我想做的事

假设一:所有的子文件夹都使用相同的命令部署

因此,为此,我将 cloudbuild.yaml 放在目录的根目录中,而不是子文件夹中

steps:
- name: 'gcr.io/cloud-builders/git'
entrypoint: /bin/bash
args:
- -c
- |
# Cloud Build doesn't recover the .git file. Thus checkout the repo for this
git clone --branch $BRANCH_NAME https://github.com/guillaumeblaquiere/cloudbuildtest.git /tmp/repo ;
# Copy only the .git file
mv /tmp/repo/.git .
# Make a diff between this version and the previous one and store the result into a file
git diff --name-only --diff-filter=AMDR @~..@ | grep "/" | cut -d"/" -f1 | uniq > /workspace/diff

# Do what you want, by performing a loop in to the directory
- name: 'python:3.7'
entrypoint: /bin/bash
args:
- -c
- |
for i in $$(cat /workspace/diff); do
cd $$i
# No strong isolation between each function, take care of conflicts!!
pip3 install -r requirements.txt
pytest
cd ..
done

- name: 'gcr.io/cloud-builders/gcloud'
entrypoint: /bin/bash
args:
- -c
- |
for i in $$(cat /workspace/diff); do
cd $$i
gcloud functions deploy .........
cd ..
done

假设 2:部署特定于子文件夹

因此,为此我将一个 cloudbuild.yaml 放在我的目录的根目录下,另一个放在子文件夹中

steps:
- name: 'gcr.io/cloud-builders/git'
entrypoint: /bin/bash
args:
- -c
- |
# Cloud Build doesn't recover the .git file. Thus checkout the repo for this
git clone --branch $BRANCH_NAME https://github.com/guillaumeblaquiere/cloudbuildtest.git /tmp/repo ;
# Copy only the .git file
mv /tmp/repo/.git .
# Make a diff between this version and the previous one and store the result into a file
git diff --name-only --diff-filter=AMDR @~..@ | grep "/" | cut -d"/" -f1 | uniq > /workspace/diff

# Do what you want, by performing a loop in to the directory. Here launch a cloud build
- name: 'gcr.io/cloud-builders/gcloud'
entrypoint: /bin/bash
args:
- -c
- |
for i in $$(cat /workspace/diff); do
cd $$i
gcloud builds submit
cd ..
done

注意timeout在这里,因为您可以触发大量 Cloud Build,这需要时间。


想要手动运行你的构建,不要忘记添加 $BRANCH_NAME 作为替换变量

gcloud builds submit --substitutions=BRANCH_NAME=master

关于google-cloud-platform - 如何部署使用google cloud build和Source Repository新推送的多个云函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60543957/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com