project5 / .github /workflows /cicd.yml
GitHub Action
Deploy to HuggingFace Spaces from main branch
66a0674
name: Project5 CI/CD
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main, develop ]
workflow_dispatch:
inputs:
environment:
description: 'Environnement'
type: choice
options: ['dev', 'production']
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.11"]
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: latest
virtualenvs-create: true
virtualenvs-in-project: true
installer-parallel: true
- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v3
with:
path: .venv
key: venv-${{ runner.os }}-${{ matrix.python-version }}-${{ hashFiles('**/poetry.lock') }}
- name: Install dependencies
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: poetry install --no-interaction --no-root
- name: Install project
run: poetry install --no-interaction
- name: Run tests with pytest
run: |
DATABASE_URL="sqlite:///:memory:" poetry run pytest tests/ --cov=src/project5 --cov-report=xml --cov-report=html --cov-report=term-missing --cov-fail-under=80 -v
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage.xml
flags: unittests
name: codecov-umbrella
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.11"
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: latest
virtualenvs-create: true
virtualenvs-in-project: true
- name: Install dependencies
run: poetry install --no-interaction
- name: Run Black
run: poetry run black --check ./src
- name: Run isort
run: poetry run isort --check-only .
- name: Run flake8
run: poetry run flake8 ./src
# - name: Run mypy
# run: poetry run mypy ./src
security:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.11"
- name: Install Poetry
uses: snok/install-poetry@v1
- name: Install dependencies
run: poetry install --no-interaction
- name: Run Bandit security linter
run: poetry run bandit -r ./src -f json
- name: Run Safety check
run: poetry run safety check --output json
deploy:
runs-on: ubuntu-latest
needs: [test, lint, security]
if: github.ref == 'refs/heads/main' && github.event.inputs.environment == 'production'
environment: production # Nécessite approbation dans Settings > Environments
steps:
- name: Setup Git LFS
run: |
git lfs install
- uses: actions/checkout@v4
with:
fetch-depth: 0
lfs: true
- name: Pull LFS files
run: git lfs pull
- name: Push to Hugging Face Space
env:
HF_TOKEN: ${{ secrets.HF_TOKEN }}
HF_SPACE_NAME: ${{ secrets.HF_SPACE_NAME }}
run: |
echo "Configuration Git pour HF"
git config --global user.email "action@github.com"
git config --global user.name "GitHub Action"
echo "Ajouter remote HF si pas déjà présent"
if ! git remote | grep -q huggingface; then
git remote add huggingface https://huggingface.co/spaces/$HF_SPACE_NAME
fi
echo "Créer une branche orpheline pour éviter l'historique"
git checkout --orphan deploy-hf
echo "Configuration Git LFS pour HuggingFace"
git lfs track "*.pkl"
echo "Copier le Dockerfile pour HF (renommer)"
cp Dockerfile_app Dockerfile
git rm -f README.md
echo "Créer README.md pour HF Spaces si absent"
if [ ! -f README.md ]; then
cat > README.md << EOF
---
title: Building Energy Prediction API
emoji: 🏢
colorFrom: blue
colorTo: green
sdk: docker
app_port: 7860
pinned: false
license: mit
---
# Building Energy Prediction API
API FastAPI pour la prédiction de consommation énergétique des bâtiments.
## Fonctionnalités
- 🏢 Gestion des quartiers, bâtiments et propriétés
- 🤖 Prédictions ML avec RandomForest
- 📊 API REST complète avec documentation Swagger
EOF
fi
echo "Echo suppression des données non nécessaires en production"
git rm -f *.pdf
git rm -rf docs
git rm -rf sql
git rm -rf tests
echo "Ajouter tous les fichiers nécessaires (y compris LFS)"
git add -A
git commit -m "Deploy to HuggingFace Spaces from main branch"
echo "Push vers HF avec authentification (LFS supporté)"
git push https://oauth2:$HF_TOKEN@huggingface.co/spaces/$HF_SPACE_NAME deploy-hf:main --force