1

I want to call Cloud Build from a Cloud Function written in Python 3, and pass my steps directly to be executed. I'd rather not have to roll a http request and do all the low level auth myself like here.

Is there some sort of client library for Python I can use to make it easier?

Graham Polley
  • 14,393
  • 4
  • 44
  • 80

2 Answers2

1

There's a new Python client library for Cloud Build that it's in alpha. See here. Under the covers, it's calling this API. You can simply do the following in your Cloud Function to call 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 should include google-cloud-build.

enter image description here

Graham Polley
  • 14,393
  • 4
  • 44
  • 80
1

This code generated an error for me : TypeError: create_build() takes from 1 to 2 positional arguments but 3 were given

Try with this code :

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

Original code found here: https://github.com/googleapis/python-cloudbuild/issues/30