|
| 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 | + |
| 15 | + |
| 16 | +# [START cloudbuild_quickstart] |
| 17 | +import google.auth |
| 18 | +from google.cloud.devtools import cloudbuild_v1 |
| 19 | + |
| 20 | + |
| 21 | +def quickstart() -> None: |
| 22 | + """Create and execute a simple Google Cloud Build configuration, |
| 23 | + print the in-progress status and print the completed status.""" |
| 24 | + |
| 25 | + # Authorize the client with Google defaults |
| 26 | + credentials, project_id = google.auth.default() |
| 27 | + client = cloudbuild_v1.services.cloud_build.CloudBuildClient() |
| 28 | + |
| 29 | + # If you're using Private Pools or a non-global default pool, add a regional |
| 30 | + # `api_endpoint` to `CloudBuildClient()` |
| 31 | + # For example, '<YOUR_POOL_REGION>-cloudbuild.googleapis.com' |
| 32 | + # |
| 33 | + # from google.api_core import client_options |
| 34 | + # client_options = client_options.ClientOptions( |
| 35 | + # api_endpoint="us-central1-cloudbuild.googleapis.com" |
| 36 | + # ) |
| 37 | + # client = cloudbuild_v1.services.cloud_build.CloudBuildClient(client_options=client_options) |
| 38 | + |
| 39 | + build = cloudbuild_v1.Build() |
| 40 | + |
| 41 | + # The following build steps will output "hello world" |
| 42 | + # For more information on build configuration, see |
| 43 | + # https://cloud.google.com/build/docs/configuring-builds/create-basic-configuration |
| 44 | + build.steps = [{"name": "ubuntu", |
| 45 | + "entrypoint": "bash", |
| 46 | + "args": ["-c", "echo hello world"]}] |
| 47 | + |
| 48 | + operation = client.create_build(project_id=project_id, build=build) |
| 49 | + # Print the in-progress operation |
| 50 | + print("IN PROGRESS:") |
| 51 | + print(operation.metadata) |
| 52 | + |
| 53 | + result = operation.result() |
| 54 | + # Print the completed status |
| 55 | + print("RESULT:", result.status) |
| 56 | +# [END cloudbuild_quickstart] |
| 57 | + |
| 58 | + |
| 59 | +if __name__ == "__main__": |
| 60 | + quickstart() |
0 commit comments