3

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
)
4
  • 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. Commented Jul 15, 2021 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. Commented Jul 15, 2021 at 14:57
  • I am encountering similar errors when interacting with the Cloud Build API directly – especially with the projects.triggers endpoints. Might be worth opening an issue on the IssueTracker Commented Jul 15, 2021 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). 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. Commented Jul 16, 2021 at 8:17

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.