Skip to content

Commit

Permalink
feat(sync): support build-image option (#5441)
Browse files Browse the repository at this point in the history
* feat(sync): support build-image option

* chore: adding build image option on help option
  • Loading branch information
elvishp2006 authored Jul 6, 2023
1 parent cee2d3d commit 16a1740
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 16 deletions.
23 changes: 23 additions & 0 deletions samcli/commands/_utils/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,29 @@ def use_container_build_option(f):
return use_container_build_click_option()(f)


def build_image_click_option(cls):
return click.option(
"--build-image",
"-bi",
default=None,
multiple=True, # Can pass in multiple build images
required=False,
help="Container image URIs for building functions/layers. "
"You can specify for all functions/layers with just the image URI "
"(--build-image public.ecr.aws/sam/build-nodejs18.x:latest). "
"You can specify for each individual function with "
"(--build-image FunctionLogicalID=public.ecr.aws/sam/build-nodejs18.x:latest). "
"A combination of the two can be used. If a function does not have build image specified or "
"an image URI for all functions, the default SAM CLI build images will be used.",
cls=cls,
)


@parameterized_option
def build_image_option(f, cls):
return build_image_click_option(cls)(f)


def _space_separated_list_func_type(value):
if isinstance(value, str):
return value.split(" ")
Expand Down
17 changes: 2 additions & 15 deletions samcli/commands/build/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
manifest_option,
cached_option,
use_container_build_option,
build_image_option,
hook_name_click_option,
)
from samcli.commands._utils.option_value_processor import process_env_var, process_image_options
Expand Down Expand Up @@ -94,21 +95,7 @@
help="Environment variables json file (e.g., env_vars.json) to be passed to build containers.",
cls=ContainerOptions,
)
@click.option(
"--build-image",
"-bi",
default=None,
multiple=True, # Can pass in multiple build images
required=False,
help="Container image URIs for building functions/layers. "
"You can specify for all functions/layers with just the image URI "
"(--build-image public.ecr.aws/sam/build-nodejs18.x:latest). "
"You can specify for each individual function with "
"(--build-image FunctionLogicalID=public.ecr.aws/sam/build-nodejs18.x:latest). "
"A combination of the two can be used. If a function does not have build image specified or "
"an image URI for all functions, the default SAM CLI build images will be used.",
cls=ContainerOptions,
)
@build_image_option(cls=ContainerOptions)
@click.option(
"--exclude",
"-x",
Expand Down
12 changes: 11 additions & 1 deletion samcli/commands/sync/command.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""CLI command for "sync" command."""
import logging
import os
from typing import TYPE_CHECKING, List, Optional, Set
from typing import TYPE_CHECKING, List, Optional, Set, Tuple

import click

Expand All @@ -18,8 +18,10 @@
DEFAULT_CACHE_DIR,
)
from samcli.commands._utils.custom_options.replace_help_option import ReplaceHelpSummaryOption
from samcli.commands._utils.option_value_processor import process_image_options
from samcli.commands._utils.options import (
base_dir_option,
build_image_option,
capabilities_option,
image_repositories_option,
image_repository_option,
Expand All @@ -35,6 +37,7 @@
template_option_without_build,
use_container_build_option,
)
from samcli.commands.build.click_container import ContainerOptions
from samcli.commands.build.command import _get_mode_value_from_envvar
from samcli.commands.sync.core.command import SyncCommand
from samcli.commands.sync.sync_context import SyncContext
Expand Down Expand Up @@ -155,6 +158,7 @@
@stack_name_option(required=True) # pylint: disable=E1120
@base_dir_option
@use_container_build_option
@build_image_option(cls=ContainerOptions)
@image_repository_option
@image_repositories_option
@s3_bucket_option(disable_callback=True) # pylint: disable=E1120
Expand Down Expand Up @@ -202,6 +206,7 @@ def cli(
use_container: bool,
config_file: str,
config_env: str,
build_image: Optional[Tuple[str]],
) -> None:
"""
`sam sync` command entry point
Expand Down Expand Up @@ -234,6 +239,7 @@ def cli(
tags,
metadata,
use_container,
build_image,
config_file,
config_env,
None, # TODO: replace with build_in_source once it's added as a click option
Expand Down Expand Up @@ -265,6 +271,7 @@ def do_cli(
tags: dict,
metadata: dict,
use_container: bool,
build_image: Optional[Tuple[str]],
config_file: str,
config_env: str,
build_in_source: Optional[bool],
Expand Down Expand Up @@ -303,6 +310,8 @@ def do_cli(
LOG.debug("Using build directory as %s", build_dir)
EventTracker.track_event("UsedFeature", "Accelerate")

processed_build_images = process_image_options(build_image)

with BuildContext(
resource_identifier=None,
template_file=template_file,
Expand All @@ -320,6 +329,7 @@ def do_cli(
print_success_message=False,
locate_layer_nested=True,
build_in_source=build_in_source,
build_images=processed_build_images,
) as build_context:
built_template = os.path.join(build_dir, DEFAULT_TEMPLATE_NAME)

Expand Down
1 change: 1 addition & 0 deletions samcli/commands/sync/core/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"notification_arns",
"tags",
"metadata",
"build_image",
]

CONFIGURATION_OPTION_NAMES: List[str] = ["config_env", "config_file"]
Expand Down
1 change: 1 addition & 0 deletions tests/unit/commands/samconfig/test_samconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,7 @@ def test_sync(
{"a": "tag1", "b": "tag with spaces"},
{"m1": "value1", "m2": "value2"},
True,
(),
"samconfig.toml",
"default",
None,
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/commands/sync/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def setUp(self):
self.clean = True
self.config_env = "mock-default-env"
self.config_file = "mock-default-filename"
self.build_image = None
MOCK_SAM_CONFIG.reset_mock()

@parameterized.expand(
Expand Down Expand Up @@ -141,6 +142,7 @@ def test_infra_must_succeed_sync(
self.tags,
self.metadata,
use_container,
self.build_image,
self.config_file,
self.config_env,
build_in_source=False,
Expand All @@ -167,6 +169,7 @@ def test_infra_must_succeed_sync(
print_success_message=False,
locate_layer_nested=True,
build_in_source=False,
build_images={},
)

PackageContextMock.assert_called_with(
Expand Down Expand Up @@ -298,6 +301,7 @@ def test_watch_must_succeed_sync(
self.tags,
self.metadata,
use_container,
self.build_image,
self.config_file,
self.config_env,
build_in_source=False,
Expand All @@ -320,6 +324,7 @@ def test_watch_must_succeed_sync(
print_success_message=False,
locate_layer_nested=True,
build_in_source=False,
build_images={},
)

PackageContextMock.assert_called_with(
Expand Down Expand Up @@ -443,6 +448,7 @@ def test_code_must_succeed_sync(
self.tags,
self.metadata,
use_container,
self.build_image,
self.config_file,
self.config_env,
build_in_source=None,
Expand Down

0 comments on commit 16a1740

Please sign in to comment.