diff --git a/.vscode/tasks.json b/.vscode/tasks.json index d19140c..b80453f 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -24,11 +24,11 @@ "group": "build" }, { - "label": "Build Docker Image", + "label": "Build & Push Docker", "type": "shell", "command": "${workspaceFolder}\\.venv\\Scripts\\python.exe", "args": [ - "${workspaceFolder}/docker/build_image.py" + "${workspaceFolder}/docker/build_and_push_image.py" ], "problemMatcher": [] } diff --git a/docker-compose.traefik.yml b/docker-compose.traefik.yml new file mode 100644 index 0000000..ede1bd1 --- /dev/null +++ b/docker-compose.traefik.yml @@ -0,0 +1,15 @@ +services: + astrain: + image: git.beging.de/troogs/astrain:latest + restart: unless-stopped + container_name: astrain + networks: + - proxy + labels: + - traefik.enable=true + - traefik.http.routers.fs-onboarding-si.rule=Host(`astrain.melvin.beging.de`) + - traefik.http.services.fs-onboarding-si.loadbalancer.server.port=8080 + +networks: + proxy: + external: true diff --git a/docker/build_and_push_image.py b/docker/build_and_push_image.py new file mode 100644 index 0000000..67bdb09 --- /dev/null +++ b/docker/build_and_push_image.py @@ -0,0 +1,26 @@ +from __future__ import annotations + +import subprocess +import sys +from pathlib import Path + + +def main() -> int: + script_dir = Path(__file__).resolve().parent + scripts = ["build_image.py", "push_image.py"] + + for script in scripts: + script_path = script_dir / script + print(f"Executing {script}...") + try: + subprocess.run([sys.executable, str(script_path)], check=True) + print(f"{script} executed successfully.\n") + except subprocess.CalledProcessError as error: + print(f"Error: {script} failed with exit code {error.returncode}.") + return error.returncode + + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/docker/push_image.py b/docker/push_image.py new file mode 100644 index 0000000..117e245 --- /dev/null +++ b/docker/push_image.py @@ -0,0 +1,57 @@ +from __future__ import annotations + +import datetime as dt +import subprocess +from pathlib import Path + + +def get_timestamp_tag(today: dt.date) -> str: + year_suffix = today.year % 100 + day_of_year = today.timetuple().tm_yday + return f"{year_suffix}.{day_of_year}" + + +def run_command(command: list[str], repo_root: Path) -> int: + print(" ".join(command)) + result = subprocess.run(command, cwd=str(repo_root)) + return result.returncode + + +def main() -> int: + repo_root = Path(__file__).resolve().parents[1] + + today = dt.date.today() + timestamp_tag = get_timestamp_tag(today) + + local_image = "troogs/astrain" + registry_image = "git.beging.de/troogs/astrain" + tags = ["latest", timestamp_tag] + + for tag in tags: + tag_cmd = [ + "docker", + "tag", + f"{local_image}:{tag}", + f"{registry_image}:{tag}", + ] + print(f"Tagging {local_image}:{tag} as {registry_image}:{tag}") + exit_code = run_command(tag_cmd, repo_root) + if exit_code != 0: + return exit_code + + for tag in tags: + push_cmd = [ + "docker", + "push", + f"{registry_image}:{tag}", + ] + print(f"Pushing {registry_image}:{tag}") + exit_code = run_command(push_cmd, repo_root) + if exit_code != 0: + return exit_code + + return 0 + + +if __name__ == "__main__": + raise SystemExit(main())