-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[airbyte-ci] Implement pre/post build hooks (#31282)
Co-authored-by: Ben Church <[email protected]>
- Loading branch information
1 parent
e30cbed
commit 33c683b
Showing
10 changed files
with
210 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
airbyte-ci/connectors/pipelines/pipelines/builds/build_customization.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# | ||
# Copyright (c) 2023 Airbyte, Inc., all rights reserved. | ||
# | ||
import importlib | ||
from logging import Logger | ||
from types import ModuleType | ||
from typing import List, Optional | ||
|
||
from connector_ops.utils import Connector | ||
from dagger import Container | ||
|
||
BUILD_CUSTOMIZATION_MODULE_NAME = "build_customization" | ||
BUILD_CUSTOMIZATION_SPEC_NAME = f"{BUILD_CUSTOMIZATION_MODULE_NAME}.py" | ||
DEFAULT_MAIN_FILE_NAME = "main.py" | ||
|
||
|
||
def get_build_customization_module(connector: Connector) -> Optional[ModuleType]: | ||
"""Import the build_customization.py file from the connector directory if it exists. | ||
Returns: | ||
Optional[ModuleType]: The build_customization.py module if it exists, None otherwise. | ||
""" | ||
build_customization_spec_path = connector.code_directory / BUILD_CUSTOMIZATION_SPEC_NAME | ||
if not build_customization_spec_path.exists(): | ||
return None | ||
build_customization_spec = importlib.util.spec_from_file_location( | ||
f"{connector.code_directory.name}_{BUILD_CUSTOMIZATION_MODULE_NAME}", build_customization_spec_path | ||
) | ||
build_customization_module = importlib.util.module_from_spec(build_customization_spec) | ||
build_customization_spec.loader.exec_module(build_customization_module) | ||
return build_customization_module | ||
|
||
|
||
def get_main_file_name(connector: Connector) -> str: | ||
"""Get the main file name from the build_customization.py module if it exists, DEFAULT_MAIN_FILE_NAME otherwise. | ||
Args: | ||
connector (Connector): The connector to build. | ||
Returns: | ||
str: The main file name. | ||
""" | ||
build_customization_module = get_build_customization_module(connector) | ||
if hasattr(build_customization_module, "MAIN_FILE_NAME"): | ||
return build_customization_module.MAIN_FILE_NAME | ||
return DEFAULT_MAIN_FILE_NAME | ||
|
||
|
||
def get_entrypoint(connector: Connector) -> List[str]: | ||
main_file_name = get_main_file_name(connector) | ||
return ["python", f"/airbyte/integration_code/{main_file_name}"] | ||
|
||
|
||
async def pre_install_hooks(connector: Connector, base_container: Container, logger: Logger) -> Container: | ||
"""Run the pre_connector_install hook if it exists in the build_customization.py module. | ||
It will mutate the base_container and return it. | ||
Args: | ||
connector (Connector): The connector to build. | ||
base_container (Container): The base container to mutate. | ||
logger (Logger): The logger to use. | ||
Returns: | ||
Container: The mutated base_container. | ||
""" | ||
build_customization_module = get_build_customization_module(connector) | ||
if hasattr(build_customization_module, "pre_connector_install"): | ||
base_container = await build_customization_module.pre_connector_install(base_container) | ||
logger.info(f"Connector {connector.technical_name} pre install hook executed.") | ||
return base_container | ||
|
||
|
||
async def post_install_hooks(connector: Connector, connector_container: Container, logger: Logger) -> Container: | ||
"""Run the post_connector_install hook if it exists in the build_customization.py module. | ||
It will mutate the connector_container and return it. | ||
Args: | ||
connector (Connector): The connector to build. | ||
connector_container (Container): The connector container to mutate. | ||
logger (Logger): The logger to use. | ||
Returns: | ||
Container: The mutated connector_container. | ||
""" | ||
build_customization_module = get_build_customization_module(connector) | ||
if hasattr(build_customization_module, "post_connector_install"): | ||
connector_container = await build_customization_module.post_connector_install(connector_container) | ||
logger.info(f"Connector {connector.technical_name} post install hook executed.") | ||
return connector_container |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" | |
|
||
[tool.poetry] | ||
name = "pipelines" | ||
version = "1.6.0" | ||
version = "1.7.0" | ||
description = "Packaged maintained by the connector operations team to perform CI for connectors' pipelines" | ||
authors = ["Airbyte <[email protected]>"] | ||
|
||
|
35 changes: 35 additions & 0 deletions
35
airbyte-ci/connectors/pipelines/tests/test_builds/dummy_build_customization.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# | ||
# Copyright (c) 2023 Airbyte, Inc., all rights reserved. | ||
# | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from dagger import Container | ||
|
||
|
||
async def pre_connector_install(base_image_container: Container) -> Container: | ||
"""This function will run before the connector installation. | ||
It can mutate the base image container. | ||
Args: | ||
base_image_container (Container): The base image container to mutate. | ||
Returns: | ||
Container: The mutated base image container. | ||
""" | ||
return base_image_container.with_env_variable("MY_PRE_BUILD_ENV_VAR", "my_pre_build_env_var_value") | ||
|
||
|
||
async def post_connector_install(connector_container: Container) -> Container: | ||
"""This function will run after the connector installation during the build process. | ||
It can mutate the connector container. | ||
Args: | ||
connector_container (Container): The connector container to mutate. | ||
Returns: | ||
Container: The mutated connector container. | ||
""" | ||
return connector_container.with_env_variable("MY_POST_BUILD_ENV_VAR", "my_post_build_env_var_value") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 0 additions & 20 deletions
20
airbyte-integrations/connectors/source-zendesk-chat/Dockerfile
This file was deleted.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
airbyte-integrations/connectors/source-zendesk-chat/build_customization.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# | ||
# Copyright (c) 2023 Airbyte, Inc., all rights reserved. | ||
# | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from dagger import Container | ||
|
||
MAIN_FILE_NAME = "main_dev.py" | ||
|
||
|
||
async def pre_connector_install(base_image_container: Container) -> Container: | ||
"""This function will run before the connector installation. | ||
We set these environment variable to match what was originally in the Dockerfile. | ||
Disclaimer: I have no idea if these env vars are actually needed. | ||
""" | ||
return base_image_container.with_env_variable("AIRBYTE_IMPL_MODULE", "source_zendesk_chat").with_env_variable( | ||
"AIRBYTE_IMPL_PATH", "SourceZendeskChat" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters