我如何从 Python 云功能 trigger/call 云构建
How do I trigger/call Cloud Build from a Python Cloud Function
我想从 Python 3 中编写的 Cloud Function 调用 Cloud Build,并直接将我的步骤传递给执行。我宁愿不必滚动 http 请求并像 .
那样自己完成所有低级身份验证
是否有某种 Python 的客户端库可以让我使用起来更容易?
Cloud Build 有一个新的 Python 客户端库,它在 alpha 中。参见 here. Under the covers, it's calling this API。您只需在 Cloud Function 中执行以下操作即可调用 Cloud Build:
from google.cloud.devtools import cloudbuild_v1
def trigger_cloud_build(request):
client = cloudbuild_v1.CloudBuildClient()
project_id = 'YOUR_PROJECT_ID'
build = {'steps': [{'name': 'gcr.io/cloud-builders/docker',
'args': ['version'], 'id': 'Docker Version'}]}
response = client.create_build(project_id, build)
print(response)
requirements.txt 应包括 google-cloud-build
.
此代码为我生成了一个错误:TypeError: create_build() takes from 1 to 2 positional arguments but 3 were given
尝试使用此代码:
# from datetime import timedelta
from google.cloud.devtools import cloudbuild_v1
# build = {"steps": [{"name": "ubuntu","args": ["sleep", "100"],}],}
build = {'steps': [{'name': 'gcr.io/cloud-builders/docker','args': ['version'], 'id': 'Docker Version'}],}
client = cloudbuild_v1.CloudBuildClient()
response = client.create_build(project_id="YOUR-GCP-PROJECT-ID",build=cloudbuild_v1.Build(build),)
print(response)
原始代码在这里找到:https://github.com/googleapis/python-cloudbuild/issues/30
我想从 Python 3 中编写的 Cloud Function 调用 Cloud Build,并直接将我的步骤传递给执行。我宁愿不必滚动 http 请求并像
是否有某种 Python 的客户端库可以让我使用起来更容易?
Cloud Build 有一个新的 Python 客户端库,它在 alpha 中。参见 here. Under the covers, it's calling this API。您只需在 Cloud Function 中执行以下操作即可调用 Cloud Build:
from google.cloud.devtools import cloudbuild_v1
def trigger_cloud_build(request):
client = cloudbuild_v1.CloudBuildClient()
project_id = 'YOUR_PROJECT_ID'
build = {'steps': [{'name': 'gcr.io/cloud-builders/docker',
'args': ['version'], 'id': 'Docker Version'}]}
response = client.create_build(project_id, build)
print(response)
requirements.txt 应包括 google-cloud-build
.
此代码为我生成了一个错误:TypeError: create_build() takes from 1 to 2 positional arguments but 3 were given
尝试使用此代码:
# from datetime import timedelta
from google.cloud.devtools import cloudbuild_v1
# build = {"steps": [{"name": "ubuntu","args": ["sleep", "100"],}],}
build = {'steps': [{'name': 'gcr.io/cloud-builders/docker','args': ['version'], 'id': 'Docker Version'}],}
client = cloudbuild_v1.CloudBuildClient()
response = client.create_build(project_id="YOUR-GCP-PROJECT-ID",build=cloudbuild_v1.Build(build),)
print(response)
原始代码在这里找到:https://github.com/googleapis/python-cloudbuild/issues/30