From bca332b59834cb01ade589c2ae31ffdf2114ccbe Mon Sep 17 00:00:00 2001 From: VPhung24 Date: Mon, 7 Oct 2024 18:27:10 +0000 Subject: [PATCH 1/2] run dockerfile if exists --- stack_orchestrator/build/build_webapp.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/stack_orchestrator/build/build_webapp.py b/stack_orchestrator/build/build_webapp.py index 1021f4bf..5b349366 100644 --- a/stack_orchestrator/build/build_webapp.py +++ b/stack_orchestrator/build/build_webapp.py @@ -88,9 +88,17 @@ def command(ctx, base_container, source_repo, force_rebuild, extra_build_args, t # Now build the target webapp. We use the same build script, but with a different Dockerfile and work dir. container_build_env["CERC_WEBAPP_BUILD_RUNNING"] = "true" container_build_env["CERC_CONTAINER_BUILD_WORK_DIR"] = os.path.abspath(source_repo) - container_build_env["CERC_CONTAINER_BUILD_DOCKERFILE"] = os.path.join(container_build_dir, - base_container.replace("/", "-"), - "Dockerfile.webapp") + # Check if Dockerfile exists in the repository + repo_dockerfile = os.path.join(container_build_env["CERC_CONTAINER_BUILD_WORK_DIR"], "Dockerfile") + default_dockerfile = os.path.join(container_build_dir, + base_container.replace("/", "-"), + "Dockerfile.webapp") + + if os.path.isfile(repo_dockerfile): + container_build_env["CERC_CONTAINER_BUILD_DOCKERFILE"] = repo_dockerfile + else: + container_build_env["CERC_CONTAINER_BUILD_DOCKERFILE"] = default_dockerfile + if not tag: webapp_name = os.path.abspath(source_repo).split(os.path.sep)[-1] tag = f"cerc/{webapp_name}:local" -- 2.45.2 From dceda936b72211bd900f17754489f806ed54da66 Mon Sep 17 00:00:00 2001 From: VPhung24 Date: Mon, 7 Oct 2024 18:55:42 +0000 Subject: [PATCH 2/2] env --- stack_orchestrator/build/build_webapp.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/stack_orchestrator/build/build_webapp.py b/stack_orchestrator/build/build_webapp.py index 5b349366..29991e0e 100644 --- a/stack_orchestrator/build/build_webapp.py +++ b/stack_orchestrator/build/build_webapp.py @@ -30,6 +30,12 @@ from stack_orchestrator.build import build_containers from stack_orchestrator.deploy.webapp.util import determine_base_container, TimedLogger from stack_orchestrator.build.build_types import BuildContext +def create_env_file(env_vars, repo_root): + env_file_path = os.path.join(repo_root, '.env') + with open(env_file_path, 'w') as env_file: + for key, value in env_vars.items(): + env_file.write(f"{key}={value}\n") + return env_file_path @click.command() @click.option('--base-container') @@ -37,8 +43,9 @@ from stack_orchestrator.build.build_types import BuildContext @click.option("--force-rebuild", is_flag=True, default=False, help="Override dependency checking -- always rebuild") @click.option("--extra-build-args", help="Supply extra arguments to build") @click.option("--tag", help="Container tag (default: cerc/:local)") +@click.option("--env", help="Environment variables for webapp (format: KEY1=VALUE1,KEY2=VALUE2)", default="") @click.pass_context -def command(ctx, base_container, source_repo, force_rebuild, extra_build_args, tag): +def command(ctx, base_container, source_repo, force_rebuild, extra_build_args, tag, env): '''build the specified webapp container''' logger = TimedLogger() @@ -88,6 +95,7 @@ def command(ctx, base_container, source_repo, force_rebuild, extra_build_args, t # Now build the target webapp. We use the same build script, but with a different Dockerfile and work dir. container_build_env["CERC_WEBAPP_BUILD_RUNNING"] = "true" container_build_env["CERC_CONTAINER_BUILD_WORK_DIR"] = os.path.abspath(source_repo) + # Check if Dockerfile exists in the repository repo_dockerfile = os.path.join(container_build_env["CERC_CONTAINER_BUILD_WORK_DIR"], "Dockerfile") default_dockerfile = os.path.join(container_build_dir, @@ -95,7 +103,17 @@ def command(ctx, base_container, source_repo, force_rebuild, extra_build_args, t "Dockerfile.webapp") if os.path.isfile(repo_dockerfile): + env_vars = {} + if env: + for pair in env.split(','): + key, value = pair.split('=') + env_vars[key.strip()] = value.strip() + container_build_env["CERC_CONTAINER_BUILD_DOCKERFILE"] = repo_dockerfile + + # Create .env file with environment variables + env_file_path = create_env_file(env_vars, container_build_env["CERC_CONTAINER_BUILD_WORK_DIR"]) + container_build_env["CERC_CONTAINER_BUILD_ENV_FILE"] = env_file_path else: container_build_env["CERC_CONTAINER_BUILD_DOCKERFILE"] = default_dockerfile -- 2.45.2