diff --git a/.forgejo/workflows/ci.yml b/.forgejo/workflows/ci.yml index 6d1560d..fb32764 100644 --- a/.forgejo/workflows/ci.yml +++ b/.forgejo/workflows/ci.yml @@ -1,4 +1,4 @@ -name: CI +name: CI/CD on: push: @@ -17,8 +17,7 @@ jobs: python-version: "3.12" - name: Install dependencies - run: | - pip install -r requirements.txt -r requirements-dev.txt + run: pip install -r requirements.txt -r requirements-dev.txt - name: Ruff lint run: ruff check . @@ -36,11 +35,39 @@ jobs: python-version: "3.12" - name: Install dependencies - run: | - pip install -r requirements.txt -r requirements-dev.txt + run: pip install -r requirements.txt -r requirements-dev.txt - name: Syntax check - run: python -m py_compile bot.py && python -m py_compile vision.py && python -m py_compile config.py + run: | + python -m py_compile bot.py + python -m py_compile vision.py + python -m py_compile config.py - name: Run tests run: pytest tests/ -v || true + + deploy: + needs: [lint, test] + if: github.ref == 'refs/heads/main' && github.event_name == 'push' + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Deploy to production server + uses: appleboy/ssh-action@v1 + with: + host: ${{ secrets.PROD_HOST }} + username: ${{ secrets.PROD_USER }} + password: ${{ secrets.PROD_PASSWORD }} + script: | + cd /home/discord/Bots/VisionApp + git pull origin main --rebase + if command -v docker &>/dev/null; then + docker compose build --no-cache vision + docker compose up -d vision + else + pkill -f "python.*vision\.py" 2>/dev/null || true + source venv/bin/activate + pip install -r requirements.txt + nohup python vision.py > /dev/null 2>&1 & + fi diff --git a/.gitignore b/.gitignore index eaf9bc6..960af7e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ # --- SÉCURITÉ --- .env +.env.dev +.env.prod # --- PYTHON --- __pycache__/ diff --git a/bot.py b/bot.py index 1dfa008..09cba96 100644 --- a/bot.py +++ b/bot.py @@ -1,4 +1,5 @@ import logging +import os import discord from discord.ext import commands @@ -6,6 +7,8 @@ import config logger = logging.getLogger("vision") +DEV_GUILD_IDS = [int(x) for x in os.getenv("DEV_GUILD_IDS", "").split(",") if x.strip()] + class VisionBot(commands.Bot): def __init__(self): @@ -22,13 +25,22 @@ class VisionBot(commands.Bot): async def setup_hook(self): logger.info("Chargement des cogs...") await self.load_extension("commandes.hello") - logger.info("Sync des slash commands...") - await self.tree.sync() - logger.info("Slash commands synchronisées") + + if config.DEV and DEV_GUILD_IDS: + logger.info(f"Mode DEV — sync des commands sur les guilds: {DEV_GUILD_IDS}") + for guild_id in DEV_GUILD_IDS: + self.tree.copy_global_to(guild=guild_id) + await self.tree.sync(guild=guild_id) + logger.info(f" ✓ Sync guild {guild_id}") + else: + logger.info("Mode PROD — sync global des slash commands...") + await self.tree.sync() + logger.info("Slash commands synchronisées (global)") async def on_ready(self): - logger.info(f"Connecté en tant que {self.user} (ID: {self.user.id})") - logger.info(f"Serveurs : {len(self.guilds)}") + instance = "DEV" if config.DEV else "PROD" + logger.info(f"[{instance}] Connecté en tant que {self.user} (ID: {self.user.id})") + logger.info(f"[{instance}] Serveurs : {len(self.guilds)}") await self.change_presence( activity=discord.Activity( type=discord.ActivityType.watching, diff --git a/vision.py b/vision.py index 442fea5..234c26f 100644 --- a/vision.py +++ b/vision.py @@ -4,8 +4,6 @@ import sys from dotenv import load_dotenv -load_dotenv() - import config from bot import VisionBot @@ -26,7 +24,8 @@ async def main(): logger.critical("DISCORD_TOKEN manquant dans .env") sys.exit(1) - logger.info("Lancement de Vision Studio...") + instance = "DEV" if config.DEV else "PROD" + logger.info(f"Lancement de Vision Studio [{instance}]...") bot = VisionBot() try: