Actualiser .gitea/workflows/ci.yml
CI-CD NDF Complet / test-and-quality (push) Successful in 1m8s
CI-CD NDF Complet / deploy (push) Successful in 29s

This commit is contained in:
2026-05-19 14:09:54 +02:00
parent 970d07413c
commit 3d5923c49a
+61 -49
View File
@@ -15,18 +15,17 @@ jobs:
with: with:
fetch-depth: 0 fetch-depth: 0
# 1. TESTS AUTOMATISÉS
- name: Installation et Tests Node.js (Backend) - name: Installation et Tests Node.js (Backend)
run: | run: |
cd ndf/public/backend cd ndf/public/backend
npm install npm install
if grep -q '"test":' package.json; then if grep -q '"test":' package.json; then
echo "Script de test trouvé - Lancement des tests..."
npm test -- --coverage --watchAll=false npm test -- --coverage --watchAll=false
else else
echo "⚠️ Aucun script de test configuré." echo "Aucun script de test configuré. Passage à la suite."
fi fi
# 2. ANALYSE SONARQUBE
- name: Analyse SonarQube - name: Analyse SonarQube
uses: sonarsource/sonarqube-scan-action@master uses: sonarsource/sonarqube-scan-action@master
continue-on-error: true continue-on-error: true
@@ -37,6 +36,7 @@ jobs:
args: > args: >
-Dsonar.projectKey=ndf-Gitea -Dsonar.projectKey=ndf-Gitea
-Dsonar.sources=ndf/public/backend -Dsonar.sources=ndf/public/backend
-Dsonar.javascript.lcov.reportPaths=ndf/public/backend/coverage/lcov.info
deploy: deploy:
needs: test-and-quality needs: test-and-quality
@@ -47,67 +47,79 @@ jobs:
- name: Récupération du code pour le déploiement - name: Récupération du code pour le déploiement
uses: actions/checkout@v3 uses: actions/checkout@v3
# 3. TRAITEMENT DU COMPOSE POUR GITEA (Suppression builds + Changement des ports)
- name: Préparation du fichier docker-compose pour Gitea - name: Préparation du fichier docker-compose pour Gitea
run: | run: |
# Comme sur Jenkins, on vire les lignes de build pour l'API Portainer
sed -i '/build:/d' docker-compose.yml sed -i '/build:/d' docker-compose.yml
sed -i '/context:/d' docker-compose.yml sed -i '/context:/d' docker-compose.yml
sed -i '/dockerfile:/d' docker-compose.yml sed -i '/dockerfile:/d' docker-compose.yml
sed -i 's/8024:3024/8025:3024/g' 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/3025:81/3026:81/g' docker-compose.yml
sed -i 's/"8024:3024"/"8025:3024"/g' docker-compose.yml sed -i 's/container_name: ndf-backend/container_name: gitea-ndf-backend/g' docker-compose.yml
sed -i 's/"3025:81"/"3026:81"/g' docker-compose.yml sed -i 's/container_name: ndf-frontend/container_name: gitea-ndf-frontend/g' docker-compose.yml
echo "Contenu du compose après modification :"
cat docker-compose.yml
# 4. DEPLOIEMENT VIA L'API PORTAINER
- name: Envoi à l'API Portainer - name: Envoi à l'API Portainer
run: | run: |
echo "--- 🔑 1. Authentification ---" echo "--- 1. Authentification ---"
printf '{"username":"%s","password":"%s"}' "${{ secrets.PORTAINER_USER }}" "${{ secrets.PORTAINER_PASS }}" > auth.json 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) 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 rm -f auth.json
if [ -z "$TOKEN" ]; then echo "❌ Échec Login Portainer"; exit 1; fi if [ -z "$TOKEN" ]; then
echo "Echec Login Portainer"
echo "--- 🔍 2. Récupération des IDs ---" exit 1
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) fi
echo "Authentification réussie"
# 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 "--- 2. Récupération des IDs ---"
ENDPOINT_ID=$(curl -s -k \
echo "--- 🚀 3. Génération du Payload ---" -H "Authorization: Bearer $TOKEN" \
# On utilise Node.js présent par défaut pour formater le JSON proprement "${PORTAINER_API}/endpoints" \
| grep -o '"Id":[0-9]*,"Name":"myportainer-cgy-dev"' \
| grep -o '[0-9]*' | head -n 1)
echo "Endpoint ID : $ENDPOINT_ID"
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 "Stack ID : $STACK_ID"
echo "--- 3. Génération du Payload ---"
node -e ' node -e '
const fs = require("fs"); const fs = require("fs");
const compose = fs.readFileSync("docker-compose.yml", "utf8"); const compose = fs.readFileSync("docker-compose.yml", "utf8");
const payload = { const payload = {
stackFileContent: compose, stackFileContent: compose,
pullImage: true, pullImage: true,
prune: true prune: true
}; };
fs.writeFileSync("payload.json", JSON.stringify(payload)); fs.writeFileSync("payload.json", JSON.stringify(payload));
console.log("Payload size:", JSON.stringify(payload).length, "bytes");
' '
if [ -z "$STACK_ID" ]; then echo "--- 4. Envoi à Portainer ---"
echo "--- 📨 4. Création de la Stack (Première fois) ---" RESPONSE=$(curl -s -k \
# Si la stack n'existe pas, on fait un POST pour la créer de zéro -o response_body.json \
# 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) -w "%{http_code}" \
echo "❌ La stack gitea-action-dev n'existe pas encore sur Portainer." -X PUT "${PORTAINER_API}/stacks/${STACK_ID}?endpointId=${ENDPOINT_ID}" \
echo "👉 Va sur Portainer, crée une Stack VIDE nommée 'gitea-action-dev', puis relance ce pipeline !" -H "Authorization: Bearer ${TOKEN}" \
exit 1 -H "Content-Type: application/json" \
-d @payload.json)
echo "HTTP Code : $RESPONSE"
echo "Réponse Portainer :"
cat response_body.json
if [ "$RESPONSE" = "200" ]; then
echo "DEPLOIEMENT REUSSI - Frontend: 3026 / Backend: 8025"
else else
echo "--- 📨 4. Mise à jour de la Stack existante ($STACK_ID) ---" echo "Erreur Portainer (Code HTTP $RESPONSE)"
RESPONSE=$(curl -s -k -o response_body.json -w "%{http_code}" \ exit 1
-X PUT "${PORTAINER_API}/stacks/${STACK_ID}?endpointId=${ENDPOINT_ID}" \ fi
-H "Authorization: Bearer ${TOKEN}" \
-H "Content-Type: application/json" \ rm -f payload.json response_body.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