2

How to use python-cloudbuild library to run a build trigger with correctly passing data from SourceRepo?

UPDATE 1:

I have a build trigger set up and I am trying to run that trigger by changing the substitutions and the repo branch

UPDATE 2:

Actual code result:

Traceback (most recent call last): File "/layers/google.python.pip/pip/lib/python3.9/site-packages/google/api_core/grpc_helpers.py", line 67, in error_remapped_callable return callable_(*args, **kwargs) File "/layers/google.python.pip/pip/lib/python3.9/site-packages/grpc/_channel.py", line 946, in call return _end_unary_response_blocking(state, call, False, None) File "/layers/google.python.pip/pip/lib/python3.9/site-packages/grpc/_channel.py", line 849, in _end_unary_response_blocking raise _InactiveRpcError(state) grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with: status = 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
)
  • Hi Damien, just to confirm what you are trying to achieve here. You've already created a build trigger and now want to run it from a python script? I'm asking because – unless configured as manual – build triggers would typically be created precisely to be executed automatically based on git events (push / pull request / tag) happening on the source repo, without you having to run anything. – Thomas Laporte Jul 15 '21 at 13:56
  • yes, i have a build trigger set up and I am trying to run that trigger by changing the substitutions and the repo branch. – Damian Nahmiyas Jul 15 '21 at 14:57
  • I am encountering similar errors when interacting with the [Cloud Build API](https://cloud.google.com/build/docs/api/reference/rest/v1/projects.triggers/run) directly – especially with the projects.triggers endpoints. Might be worth opening an issue on the [IssueTracker](https://issuetracker.google.com/issues?q=componentid:190802%20status:open#) – Thomas Laporte Jul 15 '21 at 19:16
  • Actually, I managed to make it work using the API directly, I was blocked by another issue being that the "branchName" field does not seem to accept regular expressions (see [Trigger Run REST API not accepting regex branchName in payload](https://issuetracker.google.com/issues/189354178)). However, I am still facing the same issue as you using the google-cloud-build python library. See my answer for an alternative implementation using the google-api-python-client. – Thomas Laporte Jul 16 '21 at 08:17

1 Answers1

2

I am facing the same issue when using the Cloud Build Client Library for Python (google-cloud-build). However, it does work properly when calling the REST API directly, so the library seems to be at cause here. As an alternative, you can achieve the same using the Google API Python client library (google-api-python-client):

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()

Make sure that all substitutions are already declared on the existing trigger.

Thomas Laporte
  • 313
  • 2
  • 7