142 lines
3.7 KiB
Python
142 lines
3.7 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Deployment script for dalex-todo-proto to Gitea Package Registry
|
|
Pushes Docker images to https://brokkr.robotico.dev/dalex/-/packages
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import subprocess
|
|
from pathlib import Path
|
|
from dotenv import load_dotenv
|
|
|
|
|
|
def log_info(message: str):
|
|
"""Print info message"""
|
|
print(f"✓ {message}")
|
|
|
|
|
|
def log_error(message: str):
|
|
"""Print error message"""
|
|
print(f"✗ Error: {message}", file=sys.stderr)
|
|
|
|
|
|
def run_command(command: list[str], description: str) -> bool:
|
|
"""Run a shell command and return success status"""
|
|
try:
|
|
log_info(f"{description}...")
|
|
result = subprocess.run(
|
|
command,
|
|
check=True,
|
|
capture_output=True,
|
|
text=True
|
|
)
|
|
return True
|
|
except subprocess.CalledProcessError as e:
|
|
log_error(f"{description} failed")
|
|
if e.stdout:
|
|
print(e.stdout)
|
|
if e.stderr:
|
|
print(e.stderr, file=sys.stderr)
|
|
return False
|
|
|
|
|
|
def main():
|
|
"""Main deployment function"""
|
|
# Load environment variables from .env file
|
|
env_file = Path(".env")
|
|
if not env_file.exists():
|
|
log_error(".env file not found!")
|
|
log_error("Please create .env file with PUBLISH_TOKEN variable.")
|
|
log_error("See .env.example for template.")
|
|
log_error("See ENV_SETUP.md for detailed instructions.")
|
|
sys.exit(1)
|
|
|
|
load_dotenv()
|
|
|
|
publish_token = os.getenv("PUBLISH_TOKEN")
|
|
if not publish_token:
|
|
log_error("PUBLISH_TOKEN not set in .env file")
|
|
sys.exit(1)
|
|
|
|
# Configuration
|
|
gitea_server = "https://brokkr.robotico.dev"
|
|
gitea_owner = "dalex"
|
|
registry = f"{gitea_server}/{gitea_owner}"
|
|
|
|
# Image names
|
|
backend_local = "todolist-proto-backend:latest"
|
|
frontend_local = "todolist-proto-frontend:latest"
|
|
backend_remote = f"{registry}/dalex-todo-backend:latest"
|
|
frontend_remote = f"{registry}/dalex-todo-frontend:latest"
|
|
|
|
log_info("Starting deployment process...")
|
|
print(f"Registry: {registry}")
|
|
print()
|
|
|
|
# Build images
|
|
if not run_command(
|
|
["docker-compose", "build"],
|
|
"Building Docker images"
|
|
):
|
|
sys.exit(1)
|
|
|
|
# Tag images for Gitea registry
|
|
if not run_command(
|
|
["docker", "tag", backend_local, backend_remote],
|
|
"Tagging backend image"
|
|
):
|
|
sys.exit(1)
|
|
|
|
if not run_command(
|
|
["docker", "tag", frontend_local, frontend_remote],
|
|
"Tagging frontend image"
|
|
):
|
|
sys.exit(1)
|
|
|
|
# Login to Gitea registry
|
|
log_info("Logging in to Gitea registry...")
|
|
login_process = subprocess.Popen(
|
|
["docker", "login", gitea_server, "-u", gitea_owner, "--password-stdin"],
|
|
stdin=subprocess.PIPE,
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.PIPE,
|
|
text=True
|
|
)
|
|
stdout, stderr = login_process.communicate(input=publish_token)
|
|
|
|
if login_process.returncode != 0:
|
|
log_error("Login to Gitea registry failed")
|
|
print(stderr, file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
log_info("Successfully logged in to Gitea registry")
|
|
|
|
# Push backend image
|
|
if not run_command(
|
|
["docker", "push", backend_remote],
|
|
"Pushing backend image"
|
|
):
|
|
sys.exit(1)
|
|
|
|
# Push frontend image
|
|
if not run_command(
|
|
["docker", "push", frontend_remote],
|
|
"Pushing frontend image"
|
|
):
|
|
sys.exit(1)
|
|
|
|
# Success
|
|
print()
|
|
log_info("Deployment complete!")
|
|
print()
|
|
print("Images published to:")
|
|
print(f" • {backend_remote}")
|
|
print(f" • {frontend_remote}")
|
|
print()
|
|
print(f"View packages at: {gitea_server}/{gitea_owner}/-/packages")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|