Google cloud platform 如何部署使用google cloud build和源存储库新推出的多个云功能?
我有一个项目文件夹,其中包含不同的云功能文件夹,例如Google cloud platform 如何部署使用google cloud build和源存储库新推出的多个云功能?,google-cloud-platform,google-cloud-functions,google-cloud-build,build-triggers,google-cloud-repository,Google Cloud Platform,Google Cloud Functions,Google Cloud Build,Build Triggers,Google Cloud Repository,我有一个项目文件夹,其中包含不同的云功能文件夹,例如 Project_Folder -Cloud-Function-Folder1 -main.py -requirements.txt -cloudbuild.yaml -Cloud-Function-Folder2 -main.py -requirements.txt -cloudbuild.yaml -Cloud
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 Functions文件夹推送到源存储库(每个功能文件夹有单独的repo)。然后它启用了一个触发器,该触发器触发云构建,然后部署该功能。
我拥有的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
现在,我想做的是,我想做一个单源repo,每当我在一个云函数中更改代码并推送它时,只有它得到部署,而rest仍然像以前一样
更新 现在,我也尝试了下面类似的方法,但它也同时部署了所有函数,即使我正在处理单个函数
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
如果您创建一个源repo并将代码更改为一个云函数,那么您必须创建一个源repo。您需要将此单一回购连接到云构建。然后创建一个选择此回购协议作为源。此外,您还需要在任何时候将新代码推送到存储库,您将自动触发构建并在云上部署功能。更复杂的是,您必须处理云构建的限制和约束 我这样做:
- 获取自上次提交以来更新的目录
- 在这个目录上循环并执行我想要的操作
假设1:使用相同的命令部署所有子文件夹 因此,为此,我将
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
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
假设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
- 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
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
请注意此处的错误,因为您可能会触发大量的云构建,而且这需要时间
要手动运行构建,请不要忘记添加$BRANCH\u名称作为替换变量
gcloud builds submit --substitutions=BRANCH_NAME=master
这就是我现在正在做的。我想做的是,我想做一个单一的回购与不同的云功能。如果我更改了例如Cloud-function1,那么它应该只触发此云功能的部署