diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0a27357..ecb9a31 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,18 +1,44 @@ -name: CI +name: CI — NexaCloud API on: push: - branches: [ "main" ] + branches: [main] pull_request: - branches: [ "main" ] + branches: [main] jobs: - build: + lint: runs-on: ubuntu-latest steps: - - name: Checkout code + - name: Checkout uses: actions/checkout@v4 - - name: Example step - run: echo "Add your build/test steps here!" + - name: Setup Python + uses: actions/setup-python@v5 + with: + python-version: "3.11" + + - name: Installer flake8 + run: pip install flake8 + + - name: Lint avec flake8 + run: flake8 ressources/ --config ressources/.flake8 + + test: + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup Python + uses: actions/setup-python@v5 + with: + python-version: "3.11" + + - name: Installer les dépendances + run: pip install -r ressources/requirements.txt + + - name: Lancer les tests + run: pytest ressources/ -v diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml new file mode 100644 index 0000000..2b9db71 --- /dev/null +++ b/.github/workflows/cicd.yml @@ -0,0 +1,52 @@ +name: CI/CD — NexaCloud API + +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + # ── Job 1 : Qualité ──────────────────────────────────────────────── + qualite: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-python@v5 + with: + python-version: "3.11" + + - run: pip install -r ressources/requirements.txt + + - name: Lint + run: flake8 ressources/ --config ressources/.flake8 + + - name: Tests + run: pytest ressources/ -v --cov=ressources + + # ── Job 2 : Staging ─────────────────────────────────────────────── + staging: + runs-on: ubuntu-latest + needs: qualite # attend que le job qualite réussisse + environment: staging + if: github.ref_name == 'main' # uniquement sur la branche main + + steps: + - uses: actions/checkout@v4 + + # TODO: ajouter le step de déploiement sur Azure App Service + # (remplacez app-name par votre nom d'application) + + # ── Job 3 : Production ──────────────────────────────────────────── + production: + runs-on: ubuntu-latest + needs: staging + environment: production + if: github.ref_name == 'main' + + steps: + - uses: actions/checkout@v4 + + # TODO: même chose pour la production diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..d37deff --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,32 @@ +name: Deploy + +on: + workflow_dispatch: + inputs: + environment: + description: "Environnement cible" + required: true + default: "staging" + type: choice + options: + - staging + - production + +jobs: + deploy: + runs-on: ubuntu-latest + environment: ${{ inputs.environment }} # bloqué si production nécessite une approbation + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Déployer sur ${{ inputs.environment }} + run: | + if [ "${{ inputs.environment }}" = "production" ]; then + echo "🚀 Déploiement en PRODUCTION" + echo "URL : https://nexacloud.example.com" + else + echo "✅ Déploiement en STAGING" + echo "URL : https://staging.nexacloud.example.com" + fi diff --git a/.github/workflows/hello.yml b/.github/workflows/hello.yml new file mode 100644 index 0000000..f35681c --- /dev/null +++ b/.github/workflows/hello.yml @@ -0,0 +1,24 @@ +name: Hello NexaCloud + +on: + push: + branches: [main] + workflow_dispatch: # permet de déclencher manuellement depuis l'interface GitHub + +jobs: + salutation: + runs-on: ubuntu-latest + + steps: + - name: Checkout du code + uses: actions/checkout@v4 + + - name: Informations sur l'environnement + run: | + echo "Repo : ${{ github.repository }}" + echo "Branche : ${{ github.ref_name }}" + echo "Commit : ${{ github.sha }}" + echo "Acteur : ${{ github.actor }}" + + - name: Lister les fichiers du repo + run: ls -la diff --git a/.github/workflows/secrets.yml b/.github/workflows/secrets.yml new file mode 100644 index 0000000..236d950 --- /dev/null +++ b/.github/workflows/secrets.yml @@ -0,0 +1,15 @@ +name: Demo Secrets + +on: + workflow_dispatch: + +jobs: + demo: + runs-on: ubuntu-latest + + steps: + - name: Utiliser le secret + run: | + echo "La clé existe : ${{ secrets.MDPFRANCK != '' }}" + # ⚠️ Cette ligne sera masquée dans les logs : + echo "Valeur : ${{ secrets.MDPFRANCK }}" diff --git a/.gitignore b/.gitignore index 78e7733..66ee655 100644 --- a/.gitignore +++ b/.gitignore @@ -41,4 +41,4 @@ terraform.rc # Optional: ignore plan files saved before destroying Terraform configuration # Uncomment the line below if you want to ignore planout files. -# planout \ No newline at end of file +# planout diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..5efc02b --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,15 @@ +repos: + - repo: https://github.com/pycqa/flake8 + rev: 7.0.0 + hooks: + - id: flake8 + args: [--config, ressources/.flake8] + files: ressources/.*\.py$ + + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.6.0 + hooks: + - id: trailing-whitespace # supprime les espaces en fin de ligne + - id: end-of-file-fixer # ajoute un saut de ligne en fin de fichier + - id: check-yaml # valide les fichiers YAML (vos workflows !) + - id: check-merge-conflict # bloque si des marqueurs de conflit Git traînent diff --git a/README.md b/README.md index 4abe121..c264a77 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # TP GitHub Actions — CI/CD et automatisation -**Durée estimée :** 6h -**Prérequis :** Git, GitHub, bases Bash ou PowerShell (TPs précédents) +**Durée estimée :** 6h +**Prérequis :** Git, GitHub, bases Bash ou PowerShell (TPs précédents) **Environnement :** tout OS avec Git installé et un compte GitHub actif --- @@ -242,7 +242,7 @@ Commitez et pushez. Observez l'exécution dans l'onglet **Actions**. > ✏️ **À vous** > -> Ajoutez un step qui affiche la date et l'heure du runner avec `date`. +> Ajoutez un step qui affiche la date et l'heure du runner avec `date`. > Puis déclenchez le workflow **manuellement** depuis l'interface GitHub (bouton "Run workflow").
diff --git a/notes.md b/notes.md new file mode 100644 index 0000000..2c821cd --- /dev/null +++ b/notes.md @@ -0,0 +1 @@ +# Mon TP GitHub Actions diff --git a/ressources/__pycache__/app.cpython-314.pyc b/ressources/__pycache__/app.cpython-314.pyc new file mode 100644 index 0000000..c272a30 Binary files /dev/null and b/ressources/__pycache__/app.cpython-314.pyc differ diff --git a/ressources/__pycache__/test_app.cpython-314-pytest-8.2.0.pyc b/ressources/__pycache__/test_app.cpython-314-pytest-8.2.0.pyc new file mode 100644 index 0000000..5f83d09 Binary files /dev/null and b/ressources/__pycache__/test_app.cpython-314-pytest-8.2.0.pyc differ diff --git a/ressources/app.py b/ressources/app.py index d7e75f2..eee358d 100644 --- a/ressources/app.py +++ b/ressources/app.py @@ -38,5 +38,14 @@ def logs_critical(): return jsonify({"critical_count": seuil, "alerte": alerte}) +@app.route("/logs/stats") +def logs_stats(): + total = sum(LOG_SUMMARY.values()) + return jsonify({ + "total": total, + "breakdown": LOG_SUMMARY + }) + + if __name__ == "__main__": app.run(debug=True, port=5001) diff --git a/ressources/test_app.py b/ressources/test_app.py index ce56a43..d5f9dce 100644 --- a/ressources/test_app.py +++ b/ressources/test_app.py @@ -57,3 +57,13 @@ def test_logs_critical_alerte(client): assert "critical_count" in data assert "alerte" in data assert data["alerte"] is True + + +def test_logs_stats(client): + """La route /logs/stats retourne le total et le détail.""" + response = client.get("/logs/stats") + assert response.status_code == 200 + data = response.get_json() + assert "total" in data + assert "breakdown" in data + assert data["total"] == 185