name: Build and Test Flask App on: push: branches: [ main, master ] pull_request: branches: [ main, master ] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Set up Python via apt run: | apt update apt install -y python3 python3-pip python3-venv python3-dev python3-full - name: Create and activate virtual environment run: | python3 -m venv venv . venv/bin/activate pip install --upgrade pip echo "VIRTUAL_ENV=$PWD/venv" >> $GITHUB_ENV echo "$PWD/venv/bin" >> $GITHUB_PATH - name: Install dependencies run: | if [ -f dependencies.txt ]; then pip install --no-cache-dir -r dependencies.txt else echo "dependencies.txt not found, installing Flask only" pip install flask fi pip list - name: Debug build - check imports run: | export FLASK_APP=app.py export FLASK_ENV=development flask run --host 0.0.0.0 --debug & - name: Wait for Flask app to start run: | echo "Waiting for Flask app to start..." for i in {1..10}; do if curl -f http://localhost:80/health 2>/dev/null; then echo "✅ Flask app is running" exit 0 fi echo "Attempt $i: Flask app not ready yet..." sleep 2 done echo "❌ Flask app failed to start" exit 1 - name: Install test dependencies run: | . venv/bin/activate pip install requests pytest pytest-cov - name: Run API tests with coverage run: | . venv/bin/activate echo "Running API tests..." python test_api.py echo "✅ API tests completed" - name: Run additional pytest tests (if exist) run: | . venv/bin/activate if [ -d tests ] && [ "$(ls -A tests)" ]; then pytest tests/ -v --tb=short else echo "No pytest tests found, skipping" fi - name: Install release dependencies run: | . venv/bin/activate pip install gunicorn - name: Release build (simulated) run: | . venv/bin/activate gunicorn --check-config app:app echo "✅ Gunicorn configuration is valid" - name: Build Docker image run: | if [ -f Dockerfile ]; then echo "🐳 Building Docker image..." docker build -t PVH:${{ github.sha }} -t PVH:latest . docker images | grep PVH echo "✅ Docker image built successfully" # Проверяем, что образ создался docker run --rm PVH:latest python -c "import sys; print('Python version:', sys.version)" || true fi - name: Test Docker image run: | echo "Testing Docker image..." docker run -d -p 5001:5000 --name flask-test-container flask-app:latest sleep 5 curl -f http://localhost:5001/health || (docker logs flask-test-container && exit 1) docker stop flask-test-container docker rm flask-test-container echo "✅ Docker image test passed" - name: Stop Flask app if: always() run: | pkill -f "flask run" || pkill -f "python app.py" || true - name: Cleanup Docker if: always() run: | docker system prune -f || true