Skip to content

Commit 8547d1b

Browse files
committed
Make compose watch a separate command
1 parent 84b97af commit 8547d1b

File tree

8 files changed

+63
-13
lines changed

8 files changed

+63
-13
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
cache-from: type=gha
3838
cache-to: type=gha,mode=max
3939
- name: Start Docker containers
40-
run: uv run ./cli.py compose-up -- --no-build
40+
run: uv run ./cli.py compose-up -- --detach --no-build
4141
- name: Run linting
4242
# https://github.com/actions/runner/issues/241#issuecomment-745902718
4343
shell: 'script -q -e -c "bash {0}"'

adit_radis_shared/cli/commands.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,29 @@ def compose_build(profile: list[str], extra_args: list[str], **kwargs):
2727
print("Build finished.")
2828

2929

30+
def compose_watch(profile: list[str], extra_args: list[str], **kwargs):
31+
helper = CommandHelper()
32+
helper.prepare_environment()
33+
34+
if helper.is_production():
35+
sys.exit(
36+
"Starting containers with compose-watch can only be used in development. "
37+
"Check ENVIRONMENT setting in .env file."
38+
)
39+
40+
cmd = f"{helper.build_compose_cmd(profile, watch_mode=True)} up --watch"
41+
if extra_args:
42+
cmd += " " + " ".join(extra_args)
43+
44+
helper.execute_cmd(
45+
cmd,
46+
env={
47+
"COMPOSE_BAKE": "true",
48+
"PROJECT_VERSION": helper.get_local_project_version(),
49+
},
50+
)
51+
52+
3053
def compose_up(profile: list[str], extra_args: list[str], **kwargs):
3154
helper = CommandHelper()
3255
helper.prepare_environment()
@@ -37,7 +60,7 @@ def compose_up(profile: list[str], extra_args: list[str], **kwargs):
3760
"Check ENVIRONMENT setting in .env file."
3861
)
3962

40-
cmd = f"{helper.build_compose_cmd(profile)} up --watch"
63+
cmd = f"{helper.build_compose_cmd(profile)} up"
4164
if extra_args:
4265
cmd += " " + " ".join(extra_args)
4366

adit_radis_shared/cli/helper.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ def get_compose_env_file(self):
135135
return self.root_path / "docker-compose.prod.yml"
136136
return self.root_path / "docker-compose.dev.yml"
137137

138+
def get_compose_watch_file(self):
139+
return self.root_path / "docker-compose.watch.yml"
140+
138141
def get_stack_name(self) -> str:
139142
config = self.load_config_from_env_file()
140143
if stack_name := config.get("STACK_NAME", ""):
@@ -144,10 +147,12 @@ def get_stack_name(self) -> str:
144147
return f"{self.project_id}_prod"
145148
return f"{self.project_id}_dev"
146149

147-
def build_compose_cmd(self, profiles: list[str] | None = None):
150+
def build_compose_cmd(self, profiles: list[str] | None = None, watch_mode: bool = False) -> str:
148151
cmd = "docker compose"
149152
cmd += f" -f {self.get_compose_base_file()}"
150153
cmd += f" -f {self.get_compose_env_file()}"
154+
if watch_mode:
155+
cmd += f" -f {self.get_compose_watch_file()}"
151156
cmd += f" -p {self.get_stack_name()}"
152157

153158
if profiles:

adit_radis_shared/cli/parsers.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ def register_compose_build(subparsers: argparse._SubParsersAction, func: Callabl
1313
parser.set_defaults(func=func or commands.compose_build)
1414

1515

16+
def register_compose_watch(subparsers: argparse._SubParsersAction, func: Callable | None = None):
17+
info = "Start stack with docker compose and watch for changes"
18+
parser = subparsers.add_parser("compose-watch", help=info, description=info)
19+
parser.add_argument(
20+
"--profile", action="append", default=[], help="Docker compose profile(s) to use"
21+
)
22+
parser.set_defaults(func=func or commands.compose_watch)
23+
24+
1625
def register_compose_up(subparsers: argparse._SubParsersAction, func: Callable | None = None):
1726
info = "Start stack with docker compose"
1827
parser = subparsers.add_parser("compose-up", help=info, description=info)

cli.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def copy_file(file: str, filename: str | None = None):
4444
subparsers = root_parser.add_subparsers(dest="command")
4545

4646
parsers.register_compose_build(subparsers)
47+
parsers.register_compose_watch(subparsers)
4748
parsers.register_compose_up(subparsers)
4849
parsers.register_compose_down(subparsers)
4950
parsers.register_db_backup(subparsers)

docker-compose.base.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ x-app: &default-app
44
depends_on:
55
- postgres
66
environment:
7-
BACKUP_DIR: ${BACKUP_DIR:?}
87
DATABASE_URL: postgres://postgres:postgres@postgres.local:5432/postgres
98
DBBACKUP_STORAGE_LOCATION: /backups
109
DJANGO_ADMIN_EMAIL: ${DJANGO_ADMIN_EMAIL:?}

docker-compose.dev.yml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,15 @@ x-app: &default-app
22
image: example_project_dev:latest
33
build:
44
target: development
5+
volumes:
6+
- .:/app
7+
- /app/.venv
58
environment:
69
DJANGO_INTERNAL_IPS: ${DJANGO_INTERNAL_IPS:?}
710
DJANGO_SETTINGS_MODULE: example_project.settings.development
811
FORCE_DEBUG_TOOLBAR: ${FORCE_DEBUG_TOOLBAR:-true}
912
REMOTE_DEBUGGING_ENABLED: ${REMOTE_DEBUGGING_ENABLED:-false}
1013
REMOTE_DEBUGGING_PORT: ${REMOTE_DEBUGGING_PORT:-5678}
11-
develop:
12-
watch:
13-
- action: sync
14-
path: .
15-
target: /app
16-
ignore:
17-
- .venv/
18-
- action: rebuild
19-
path: ./pyproject.toml
2014

2115
services:
2216
init:

docker-compose.watch.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
x-app: &default-app
2+
volumes: !override
3+
- ${BACKUP_DIR:?}:/backups
4+
develop:
5+
watch:
6+
- action: sync
7+
path: .
8+
target: /app
9+
ignore:
10+
- .venv/
11+
- action: rebuild
12+
path: ./pyproject.toml
13+
14+
services:
15+
web:
16+
<<: *default-app
17+
18+
worker:
19+
<<: *default-app

0 commit comments

Comments
 (0)