Sfoglia il codice sorgente

[TG-125] Fix app.py git log command and rebuild container on deploy

Lange François 1 mese fa
parent
commit
784c57396c
2 ha cambiato i file con 49 aggiunte e 1 eliminazioni
  1. 1 1
      app.py
  2. 48 0
      scripts/deploy_to_server.py

+ 1 - 1
app.py

@@ -283,7 +283,7 @@ def render_version():
     st.caption(f"🚀 Version: {git_version}")
     
     try:
-        git_id = subprocess.check_output(['git', 'log', '-1', '--format=%cd %h']).decode('utf-8').strip()
+        git_id = subprocess.check_output(['git', 'log', '-1', '--format=%cd %h', 'app.py']).decode('utf-8').strip()
     except Exception:
         git_id = "Unknown"
     st.caption(f"📅 Git ID: {git_id}")

+ 48 - 0
scripts/deploy_to_server.py

@@ -0,0 +1,48 @@
+#!/usr/bin/env python3
+#ident "@(#)$Format:LocalFoodAI:deploy_to_server.py:%an:%ae:%ad:%cn:%ce:%cd:%H:%D:%N$"
+import os
+import paramiko
+from dotenv import load_dotenv
+
+def deploy():
+    # Load .env
+    repo_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+    env_path = os.path.join(repo_root, ".env")
+    load_dotenv(dotenv_path=env_path)
+
+    host = os.environ.get('SERVER_HOST')
+    user = os.environ.get('SERVER_USER')
+    password = os.environ.get('SERVER_PASS')
+
+    if not all([host, user, password]):
+        print("Error: Server credentials not found in .env file.")
+        return
+
+    print(f"Connecting to {user}@{host}...")
+    
+    ssh = paramiko.SSHClient()
+    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
+    
+    try:
+        ssh.connect(host, username=user, password=password, timeout=10)
+        print("Connected successfully!")
+        
+        command = "cd food_project && git pull && docker compose up -d --build"
+        print(f"Executing: {command}")
+        
+        stdin, stdout, stderr = ssh.exec_command(command)
+        
+        out = stdout.read().decode('utf-8')
+        err = stderr.read().decode('utf-8')
+        
+        if out: print(f"Output:\n{out}")
+        if err: print(f"Errors:\n{err}")
+        
+    except Exception as e:
+        print(f"Failed to connect or execute command: {e}")
+    finally:
+        ssh.close()
+        print("Connection closed.")
+
+if __name__ == "__main__":
+    deploy()