如何使用 python-cloudbuild 来 运行 构建触发器

How to use python-cloudbuild to run a build trigger

如何使用 python-cloudbuild 库 运行 构建触发器并正确传递来自 SourceRepo 的数据?

更新 1:

我设置了构建触发器,我正在尝试 运行 通过更改替换和 repo 分支来触发

更新 2:

实际代码结果:

回溯(最近调用最后):文件“/layers/google.python.pip/pip/lib/python3.9/site-packages/google/api_core/grpc_helpers.py”,第 67 行,在 error_remapped_callable return callable_(*args, **kwargs) 文件“/layers/google.python.pip/pip/lib/python3.9/site-packages/grpc/_channel.py”,第 946 行, 在 call return _end_unary_response_blocking(state, call, False, None) 文件“/layers/google.python. pip/pip/lib/python3.9/site-packages/grpc/_channel.py",第 849 行,在 _end_unary_response_blocking raise _InactiveRpcError(state) grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with: 状态 = StatusCode.INTERNAL

credentials, project_id = google.auth.default()
client = cloudbuild_v1.services.cloud_build.CloudBuildClient()

trigger_id = '2f1erbc4-asdf-1234-qwery-c4bc74d16d62'

repo_source = cloudbuild_v1.RepoSource()
repo_source.branch_name = 'develop'
repo_source.substitutions = {
    "_ENVIRONMENT":"dev",
    "NAMESPACE":"dev"
}

operation = client.run_build_trigger(
    project_id=project_id,
    trigger_id=trigger_id,
    source=repo_source
)

我在为 Python (google-cloud-build) 使用 Cloud Build Client Library 时遇到同样的问题。但是,当直接调用 REST API 时它确实可以正常工作,所以库似乎是这里的原因。作为替代方案,您可以使用 Google API Python 客户端库(google-api-python-客户端)实现相同的目的:

from googleapiclient.discovery import build

project_id = "my-project-id"
trigger_id = "00000000-1111-2222-aaaa-bbbbccccdddd"

with build("cloudbuild", "v1") as cloudbuild:
  run_build_trigger = cloudbuild.projects().triggers().run(
    projectId = project_id,
    triggerId = trigger_id,
    body = {
      "branchName": "dev",
      "substitutions": {
        "_TEST": "FOO"
      }
    }
  )

  run_build_trigger.execute()

确保所有替换都已在现有触发器上声明。