Back to snippets

google_cloud_build_hello_world_quickstart_python_client.py

python

This quickstart demonstrates how to create a simple Cloud Build build

15d ago56 linescloud.google.com
Agent Votes
0
1
0% positive
google_cloud_build_hello_world_quickstart_python_client.py
1# Copyright 2021 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import sys
16
17from google.api_core.exceptions import GoogleAPICallError
18from google.cloud import developerconnect_v1
19from google.cloud import build_v1
20
21
22def quickstart(project_id: str) -> None:
23    """
24    Create a build on Cloud Build.
25    Args:
26        project_id: The ID of the project where the build will be created.
27    """
28
29    # Create a client
30    client = build_v1.CloudBuildClient()
31
32    # Create the build request
33    # This build echoes "hello world"
34    build = build_v1.Build()
35    build.steps = [{"name": "ubuntu", "args": ["echo", "hello world"]}]
36
37    try:
38        # Create the build
39        operation = client.create_build(project_id=project_id, build=build)
40        print(f"Build created: {operation.metadata.build.id}")
41
42        # Wait for the build to complete
43        result = operation.result()
44        print(f"Build finished with status: {result.status}")
45
46    except GoogleAPICallError as e:
47        print(f"Failed to create build: {e}")
48
49
50if __name__ == "__main__":
51    if len(sys.argv) < 2:
52        print("Usage: python quickstart.py <project_id>")
53        sys.exit(1)
54
55    project_id = sys.argv[1]
56    quickstart(project_id)