diff --git a/.github/workflows/ci_test.yml b/.github/workflows/ci_test.yml new file mode 100644 index 0000000..5abd233 --- /dev/null +++ b/.github/workflows/ci_test.yml @@ -0,0 +1,99 @@ +name: Django CI + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + test: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: [3.11, 3.12] + + services: + mysql: + image: mysql:8 + env: + MYSQL_ROOT_PASSWORD: root + MYSQL_DATABASE: test_db + MYSQL_USER: ci_user + MYSQL_PASSWORD: ci_pass + ports: + - 3306:3306 + options: >- + --health-cmd="mysqladmin ping -h localhost -proot" + --health-interval=10s --health-timeout=5s --health-retries=10 + + env: + SECRET_KEY: test-secret-key + DEBUG: "False" + DB_NAME: test_db + DB_USER: ci_user + DB_PASSWORD: ci_pass + DB_HOST: 127.0.0.1 + DB_PORT: "3306" + DJANGO_SETTINGS_MODULE: lms_api.settings + PYTHONUNBUFFERED: 1 + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + cache: 'pip' + + - name: Install system dependencies + run: | + sudo apt-get update + sudo apt-get install -y default-libmysqlclient-dev build-essential + + - name: Install Python dependencies + working-directory: django_api/lms_api + run: | + python -m pip install --upgrade pip wheel + if [ -f requirements.txt ]; then + pip install -r requirements.txt + else + pip install django djangorestframework django-filter drf-yasg python-decouple mysqlclient pytest pytest-django pytest-cov coverage factory_boy faker + fi + + - name: Wait for MySQL to be ready + run: | + echo "Waiting for MySQL to be ready..." + for i in {1..30}; do + mysqladmin ping -h 127.0.0.1 -uroot -proot >/dev/null 2>&1 && break + echo "waiting for mysql..." + sleep 2 + done + + - name: Create .env file + working-directory: django_api/lms_api + run: | + echo "SECRET_KEY=test-secret-key" > .env + echo "DEBUG=False" >> .env + echo "DB_NAME=test_db" >> .env + echo "DB_USER=ci_user" >> .env + echo "DB_PASSWORD=ci_pass" >> .env + echo "DB_HOST=127.0.0.1" >> .env + echo "DB_PORT=3306" >> .env + + - name: Run migrations + working-directory: django_api/lms_api + run: python manage.py migrate --noinput + + - name: Run tests with coverage + working-directory: django_api/lms_api + run: pytest --cov=library --cov-report=xml --cov-report=term -v + + - name: Upload coverage report + if: always() + uses: actions/upload-artifact@v4 + with: + name: coverage-report-${{ matrix.python-version }} + path: django_api/lms_api/coverage.xml