def deploy()

in solutions_builder/cli/cli.py [0:0]


def deploy(
        profile: Annotated[str, typer.Option(
          "--profile", "-p")] = DEFAULT_DEPLOY_PROFILE,
        component: Annotated[str, typer.Option(
          "--component", "-c", "-m")] = None,
        namespace: Annotated[str, typer.Option("--namespace", "-n")] = None,
        dev: Optional[bool] = False,
        dev_cleanup: Optional[bool] = False,
        solution_path: Annotated[Optional[str],
                                 typer.Argument()] = ".",
        skaffold_args: Optional[str] = "",
        yes: Optional[bool] = False):
  """
  Build and deploy services.
  """
  validate_solution_folder(solution_path)

  sb_yaml = read_yaml(f"{solution_path}/sb.yaml")
  global_variables = sb_yaml.get("global_variables", {})

  # Get global vars from sb.yaml.
  project_id = global_variables.get("project_id", None)
  region = global_variables.get("region", None)
  assert project_id, "project_id is not set in 'global_variables' in sb.yaml."
  assert region, "region is not set in 'global_variables' in sb.yaml."

  # Check default deploy method.
  if not profile:
    profile = global_variables.get("deploy_method", "cloudrun")

  # Check namespace
  deploy_config = sb_yaml.get("deploy", {})
  if deploy_config.get("require_namespace") not in [None, False, ""] \
          and not namespace:
    assert namespace, "Please set namespace with --namespace or -n"

  if project_id in PLACEHOLDER_VALUES:
    project_id = None
    while not project_id:
      project_id = input("Please set the GCP project ID: ")
    print()
    set_project_id(project_id)

    # Reload sb.yaml
    sb_yaml = read_yaml(f"{solution_path}/sb.yaml")
    global_variables = sb_yaml.get("global_variables", {})

  # Set gcloud to project_id
  set_gcloud_project(project_id)
  create_default_artifact_repo(project_id, "default", "us")

  commands = []
  component_flag = f" -m {component} " if component else ""
  no_prune_flag = " --no-prune " if not dev_cleanup else ""

  port_forwarding_flag = ""
  if dev:
    skaffold_command = "skaffold dev"
    port_forwarding_flag = "--port-forward"
  else:
    skaffold_command = "skaffold run"

  # Get terraform_gke component settings.
  terraform_gke = sb_yaml["components"].get("terraform_gke")
  if terraform_gke:
    cluster_name = terraform_gke["cluster_name"]
    region = terraform_gke["region"]
    commands.append(
        f"gcloud container clusters get-credentials {cluster_name} --region {region} --project {project_id}"
    )

  # Set Skaffold namespace
  namespace_flag = f"-n {namespace}" if namespace else ""

  # Set default repo to Artifact Registry
  artifact_region = "us"  # TODO: Add support to other multi-regions.
  default_repo = f"\"{artifact_region}-docker.pkg.dev/{project_id}/default\""

  # Add skaffold command.
  command_str = \
      f"{skaffold_command} -p {profile} {component_flag} {namespace_flag}" \
      f" --default-repo={default_repo}" \
      f" {skaffold_args} {port_forwarding_flag} {no_prune_flag}"
  commands.append(re.sub(" +", " ", command_str.strip()))
  print("This will build and deploy all services using the command "
        "and variables below:")
  for command in commands:
    print_success(f"- {command}")

  namespace_str = namespace or "default"
  print("\nnamespace:")
  print_success(f"- {namespace_str}")

  # print("\nenvironment variables:")
  # env_vars = {
  #   "PROJECT_ID": project_id,
  # }
  # env_var_str = ""
  # for key, value in env_vars.items():
  #   print_success(f"- {key}={value}")
  #   env_var_str += f"{key}={value} "

  print("\nglobal_variables in sb.yaml:")
  for key, value in sb_yaml.get("global_variables", {}).items():
    print_success(f"- {key}: {value}")

  print()
  confirm("This may take a few minutes. Continue?", skip=yes)
  set_gcloud_project(project_id)
  exec_shell("gcloud auth configure-docker us-docker.pkg.dev",
             working_dir=solution_path)

  for command in commands:
    exec_shell(command, working_dir=solution_path)