Skip to content

Commit

Permalink
Add CloudBuild build id log (#30516)
Browse files Browse the repository at this point in the history
  • Loading branch information
tnk-ysk authored May 16, 2023
1 parent 492acc2 commit d9f70df
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
20 changes: 14 additions & 6 deletions airflow/providers/google/cloud/hooks/cloud_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"""Hook for Google Cloud Build service."""
from __future__ import annotations

import warnings
from typing import Sequence

from google.api_core.client_options import ClientOptions
Expand All @@ -28,7 +29,7 @@
from google.cloud.devtools.cloudbuild_v1 import CloudBuildAsyncClient, CloudBuildClient, GetBuildRequest
from google.cloud.devtools.cloudbuild_v1.types import Build, BuildTrigger, RepoSource

from airflow.exceptions import AirflowException
from airflow.exceptions import AirflowException, AirflowProviderDeprecationWarning
from airflow.providers.google.common.consts import CLIENT_INFO
from airflow.providers.google.common.hooks.base_google import PROVIDE_PROJECT_ID, GoogleBaseHook

Expand Down Expand Up @@ -180,6 +181,8 @@ def create_build_without_waiting_for_result(
metadata=metadata,
)
id_ = self._get_build_id_from_operation(operation)
self.log.info("Build has been created: %s.", id_)

return operation, id_

@GoogleBaseHook.fallback_to_default_project_id
Expand Down Expand Up @@ -207,6 +210,11 @@ def create_build(
:param metadata: Optional, additional metadata that is provided to the method.
"""
warnings.warn(
"This method is deprecated. Please use `create_build_without_waiting_for_result`.",
AirflowProviderDeprecationWarning,
stacklevel=2,
)
client = self.get_conn()

self.log.info("Start creating build...")
Expand Down Expand Up @@ -520,13 +528,12 @@ def retry_build(
)

id_ = self._get_build_id_from_operation(operation)
self.log.info("Build has been retried: %s.", id_)

if not wait:
return self.get_build(id_=id_, project_id=project_id, location=location)

operation.result()

self.log.info("Build has been retried: %s.", id_)
self.wait_for_operation(operation, timeout)

return self.get_build(id_=id_, project_id=project_id, location=location)

Expand Down Expand Up @@ -567,14 +574,15 @@ def run_build_trigger(
timeout=timeout,
metadata=metadata,
)
self.log.info("Build trigger has been run: %s.", trigger_id)

id_ = self._get_build_id_from_operation(operation)
self.log.info("Build has been created: %s.", id_)

if not wait:
return self.get_build(id_=id_, project_id=project_id, location=location)
operation.result()

self.log.info("Build trigger has been run: %s.", trigger_id)
self.wait_for_operation(operation, timeout)

return self.get_build(id_=id_, project_id=project_id, location=location)

Expand Down
35 changes: 30 additions & 5 deletions tests/providers/google/cloud/hooks/test_cloud_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from google.api_core.gapic_v1.method import DEFAULT
from google.cloud.devtools.cloudbuild_v1 import CloudBuildAsyncClient, GetBuildRequest

from airflow import AirflowException
from airflow.exceptions import AirflowException, AirflowProviderDeprecationWarning
from airflow.providers.google.cloud.hooks.cloud_build import CloudBuildAsyncHook, CloudBuildHook
from airflow.providers.google.common.consts import CLIENT_INFO
from tests.providers.google.cloud.utils.base_gcp_mock import mock_base_gcp_hook_no_default_project_id
Expand Down Expand Up @@ -102,7 +102,8 @@ def test_create_build_with_wait(self, get_conn, wait_time, mock_get_id_from_oper

wait_time.return_value = 0

self.hook.create_build(build=BUILD, project_id=PROJECT_ID)
with pytest.warns(AirflowProviderDeprecationWarning, match="This method is deprecated"):
self.hook.create_build(build=BUILD, project_id=PROJECT_ID)

get_conn.return_value.create_build.assert_called_once_with(
request={"project_id": PROJECT_ID, "build": BUILD}, retry=DEFAULT, timeout=None, metadata=()
Expand All @@ -122,7 +123,8 @@ def test_create_build_without_wait(self, get_conn, mock_get_id_from_operation):
get_conn.return_value.run_build_trigger.return_value = mock.MagicMock()
mock_get_id_from_operation.return_value = BUILD_ID

self.hook.create_build(build=BUILD, project_id=PROJECT_ID, wait=False)
with pytest.warns(AirflowProviderDeprecationWarning, match="This method is deprecated"):
self.hook.create_build(build=BUILD, project_id=PROJECT_ID, wait=False)

mock_operation = get_conn.return_value.create_build

Expand All @@ -136,6 +138,29 @@ def test_create_build_without_wait(self, get_conn, mock_get_id_from_operation):

mock_get_id_from_operation.assert_called_once_with(mock_operation())

@mock.patch(
"airflow.providers.google.cloud.hooks.cloud_build.CloudBuildHook._get_build_id_from_operation"
)
@mock.patch("airflow.providers.google.cloud.hooks.cloud_build.CloudBuildHook.get_conn")
def test_create_build_without_waiting_for_result(self, get_conn, mock_get_id_from_operation):
get_conn.return_value.run_build_trigger.return_value = mock.MagicMock()
mock_get_id_from_operation.return_value = BUILD_ID

self.hook.create_build_without_waiting_for_result(
build=BUILD, project_id=PROJECT_ID, location=LOCATION
)

mock_operation = get_conn.return_value.create_build

mock_operation.assert_called_once_with(
request={"parent": PARENT, "project_id": PROJECT_ID, "build": BUILD},
retry=DEFAULT,
timeout=None,
metadata=(),
)

mock_get_id_from_operation.assert_called_once_with(mock_operation())

@mock.patch("airflow.providers.google.cloud.hooks.cloud_build.CloudBuildHook.get_conn")
def test_create_build_trigger(self, get_conn):
self.hook.create_build_trigger(trigger=BUILD_TRIGGER, project_id=PROJECT_ID)
Expand Down Expand Up @@ -224,7 +249,7 @@ def test_retry_build_with_wait(self, get_conn, wait_time, mock_get_id_from_opera
request={"project_id": PROJECT_ID, "id": BUILD_ID}, retry=DEFAULT, timeout=None, metadata=()
)

get_conn.return_value.retry_build.return_value.result.assert_called_once_with()
get_conn.return_value.retry_build.return_value.result.assert_called_once_with(timeout=None)

get_conn.return_value.get_build.assert_called_once_with(
request={"project_id": PROJECT_ID, "id": BUILD_ID}, retry=DEFAULT, timeout=None, metadata=()
Expand Down Expand Up @@ -278,7 +303,7 @@ def test_run_build_trigger_with_wait(self, get_conn, wait_time, mock_get_id_from
metadata=(),
)

get_conn.return_value.run_build_trigger.return_value.result.assert_called_once_with()
get_conn.return_value.run_build_trigger.return_value.result.assert_called_once_with(timeout=None)

get_conn.return_value.get_build.assert_called_once_with(
request={"project_id": PROJECT_ID, "id": BUILD_ID}, retry=DEFAULT, timeout=None, metadata=()
Expand Down

0 comments on commit d9f70df

Please sign in to comment.