diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 9024978..d90a451 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -15,23 +15,21 @@ jobs: with: fetch-depth: 0 - # 1. ÉTAPE DES TESTS AUTOMATISÉE + # 1. TESTS AUTOMATISÉS - name: Installation et Tests Node.js (Backend) run: | cd ndf/public/backend npm install - - # On vérifie si le script "test" est présent dans le package.json du backend if grep -q '"test":' package.json; then - echo "✅ Script de test trouvé ! Lancement des tests..." npm test -- --coverage --watchAll=false else - echo "⚠️ Aucun script de test configuré dans package.json. Passage à la suite." + echo "⚠️ Aucun script de test configuré." fi - # 2. ÉTAPE SONARQUBE + # 2. ANALYSE SONARQUBE - name: Analyse SonarQube uses: sonarsource/sonarqube-scan-action@master + continue-on-error: true env: SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }} @@ -39,32 +37,77 @@ jobs: args: > -Dsonar.projectKey=ndf-Gitea -Dsonar.sources=ndf/public/backend - -Dsonar.javascript.lcov.reportPaths=ndf/public/backend/coverage/lcov.info deploy: needs: test-and-quality runs-on: ubuntu-latest + env: + PORTAINER_API: "https://myportainer.ensup-adm.net/api" steps: - name: Récupération du code pour le déploiement uses: actions/checkout@v3 - # 3. CRÉATION DU FICHIER DE PORTS ALTERNATIFS POUR GITEA (Sur mesure) - - name: Configuration des ports alternatifs Gitea + # 3. TRAITEMENT DU COMPOSE POUR GITEA (Suppression builds + Changement des ports) + - name: Préparation du fichier docker-compose pour Gitea run: | - cat < docker-compose.gitea.yml - version: '3.8' - services: - backend: - ports: - - "8025:3024" # Jenkins a le 8024, Gitea prend le 8025 - frontend: - ports: - - "3026:81" # Jenkins a le 3025, Gitea prend le 3026 - EOF + # Comme sur Jenkins, on vire les lignes de build pour l'API Portainer + sed -i '/build:/d' docker-compose.yml + sed -i '/context:/d' docker-compose.yml + sed -i '/dockerfile:/d' docker-compose.yml + + # On change les conteneurs de ports pour Gitea (8025 au lieu de 8024 et 3026 au lieu de 3025) + sed -i 's/"8024:3024"/"8025:3024"/g' docker-compose.yml + sed -i 's/"3025:81"/"3026:81"/g' docker-compose.yml - # 4. DÉPLOIEMENT DE LA STACK ISOLÉE SANS CONFLIT - - name: Déploiement de la nouvelle Stack Gitea + # 4. DEPLOIEMENT VIA L'API PORTAINER + - name: Envoi à l'API Portainer run: | - # On fusionne ton compose d'origine et notre fichier de ports alternatifs - # On déploie dans la stack isolée demandée : gitea-action-dev - docker compose -f docker-compose.yml -f docker-compose.gitea.yml -p gitea-action-dev up -d --build \ No newline at end of file + echo "--- 🔑 1. Authentification ---" + printf '{"username":"%s","password":"%s"}' "${{ secrets.PORTAINER_USER }}" "${{ secrets.PORTAINER_PASS }}" > auth.json + TOKEN=$(curl -s -k -X POST "${PORTAINER_API}/auth" -H "Content-Type: application/json" -d @auth.json | grep -o '"jwt":"[^"]*"' | cut -d'"' -f4) + rm -f auth.json + + if [ -z "$TOKEN" ]; then echo "❌ Échec Login Portainer"; exit 1; fi + + echo "--- 🔍 2. Récupération des IDs ---" + ENDPOINT_ID=$(curl -s -k -H "Authorization: Bearer $TOKEN" "${PORTAINER_API}/endpoints" | grep -o '"Id":[0-9]*,"Name":"myportainer-cgy-dev"' | grep -o '[0-9]*' | head -n 1) + + # On cherche si la stack gitea-action-dev existe déjà + STACK_ID=$(curl -s -k -H "Authorization: Bearer $TOKEN" "${PORTAINER_API}/stacks" | grep -o '"Id":[0-9]*,"Name":"gitea-action-dev"' | grep -o '[0-9]*' | head -n 1) + + echo "--- 🚀 3. Génération du Payload ---" + # On utilise Node.js présent par défaut pour formater le JSON proprement + node -e ' + const fs = require("fs"); + const compose = fs.readFileSync("docker-compose.yml", "utf8"); + const payload = { + stackFileContent: compose, + pullImage: true, + prune: true + }; + fs.writeFileSync("payload.json", JSON.stringify(payload)); + ' + + if [ -z "$STACK_ID" ]; then + echo "--- 📨 4. Création de la Stack (Première fois) ---" + # Si la stack n'existe pas, on fait un POST pour la créer de zéro + # Il faut aussi l'ID du Swarm ou du standalone, mais Portainer accepte souvent la création par formulaire d'environnement (méthode simplifiée ici) + echo "❌ La stack gitea-action-dev n'existe pas encore sur Portainer." + echo "👉 Va sur Portainer, crée une Stack VIDE nommée 'gitea-action-dev', puis relance ce pipeline !" + exit 1 + else + echo "--- 📨 4. Mise à jour de la Stack existante ($STACK_ID) ---" + RESPONSE=$(curl -s -k -o response_body.json -w "%{http_code}" \ + -X PUT "${PORTAINER_API}/stacks/${STACK_ID}?endpointId=${ENDPOINT_ID}" \ + -H "Authorization: Bearer ${TOKEN}" \ + -H "Content-Type: application/json" \ + -d @payload.json) + + if [ "$RESPONSE" = "200" ]; then + echo "✅ DÉPLOIEMENT RÉUSSI SUR CGY-DEV (Port 3026)" + else + echo "❌ Erreur Portainer (Code HTTP $RESPONSE) :" + cat response_body.json + exit 1 + fi + fi \ No newline at end of file