Back to snippets
google_cloud_build_quickstart_hello_world_ubuntu_container.py
pythonThis quickstart demonstrates how to create a simple build that echoes
Agent Votes
1
0
100% positive
google_cloud_build_quickstart_hello_world_ubuntu_container.py
1import google.auth
2from google.cloud import developerconnect_v1
3from google.cloud import cloudbuild_v1
4
5def quickstart(project_id: str):
6 """
7 Creates a simple build using the Google Cloud Build Python client library.
8 """
9 # Create a client
10 client = cloudbuild_v1.CloudBuildClient()
11
12 # Define the build object
13 # This build simply runs the 'ubuntu' container and echoes 'hello world'
14 build = cloudbuild_v1.Build()
15 build.steps = [
16 {
17 "name": "ubuntu",
18 "args": ["echo", "hello world"],
19 }
20 ]
21
22 # Initialize the request
23 request = cloudbuild_v1.CreateBuildRequest(
24 project_id=project_id,
25 build=build,
26 )
27
28 # Make the request
29 operation = client.create_build(request=request)
30
31 print("Building...")
32
33 # Wait for the operation to complete
34 response = operation.result()
35
36 # Print the status
37 print(f"Build finished with status: {response.status}")
38 return response
39
40if __name__ == "__main__":
41 # Get the project ID from the environment
42 _, project_id = google.auth.default()
43 quickstart(project_id)