Enhancing the Meme Management #2200
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Docker Build & Test | |
| on: | |
| push: | |
| branches: [ main ] | |
| paths: | |
| - 'Dockerfile' | |
| - '.dockerignore' | |
| - 'pyproject.toml' | |
| - 'poetry.lock' | |
| - '**.py' | |
| pull_request: | |
| branches: [ main ] | |
| paths: | |
| - 'Dockerfile' | |
| - '.dockerignore' | |
| - 'pyproject.toml' | |
| - 'poetry.lock' | |
| - '**.py' | |
| workflow_dispatch: | |
| jobs: | |
| build-and-test: | |
| runs-on: ubuntu-latest | |
| services: | |
| mysql: | |
| image: mysql:8.0 | |
| env: | |
| MYSQL_ROOT_PASSWORD: rootpassword | |
| MYSQL_DATABASE: test_db | |
| MYSQL_USER: django | |
| MYSQL_PASSWORD: django_password | |
| ports: | |
| - 3306:3306 | |
| options: >- | |
| --health-cmd="mysqladmin ping -h 127.0.0.1 -u root -prootpassword" | |
| --health-interval=10s | |
| --health-timeout=5s | |
| --health-retries=3 | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| - name: Configure MySQL | |
| run: | | |
| mysql -h127.0.0.1 -P3306 -uroot -prootpassword -e "SET GLOBAL sql_mode='NO_ENGINE_SUBSTITUTION';" | |
| # Create a temporary Dockerfile that doesn't set up the database during build | |
| - name: Create test Dockerfile | |
| run: | | |
| cat > Dockerfile.test << EOF | |
| # Python base image | |
| FROM python:3.10-slim@sha256:f9fd9a142c9e3bc54d906053b756eb7e7e386ee1cf784d82c251cf640c502512 | |
| # Set working directory | |
| WORKDIR /app | |
| # Install MySQL client and build dependencies | |
| RUN apt-get update && apt-get install -y \ | |
| curl \ | |
| pkg-config \ | |
| build-essential \ | |
| default-libmysqlclient-dev \ | |
| gcc \ | |
| python3-dev \ | |
| && rm -rf /var/lib/apt/lists/* | |
| # Copy dependency manifests & install via Poetry | |
| COPY pyproject.toml poetry.lock* ./ | |
| # Install only dependencies (skip installing the project itself until after sources copied) | |
| RUN python -m pip install --upgrade pip wheel setuptools && \ | |
| pip install poetry==1.8.3 && \ | |
| poetry config virtualenvs.create false --local || true && \ | |
| poetry install --only main --no-root --no-interaction --no-ansi | |
| # Copy project files | |
| COPY . . | |
| # Create necessary directories for static files | |
| RUN mkdir -p /app/static /app/staticfiles | |
| # Collect static files | |
| RUN python manage.py collectstatic --noinput | |
| # Expose port | |
| EXPOSE 8000 | |
| # Don't run migrations or create data during build | |
| CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"] | |
| EOF | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v2 | |
| - name: Build Docker image | |
| uses: docker/build-push-action@v4 | |
| with: | |
| context: . | |
| file: ./Dockerfile.test | |
| push: false | |
| load: true | |
| tags: education-website:test | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| - name: Start Docker container and setup database | |
| run: | | |
| # Create .env file with MySQL configuration | |
| cp .env.sample .env.mysql | |
| sed -i 's|DATABASE_URL=.*|DATABASE_URL=mysql://root:rootpassword@127.0.0.1:3306/test_db|g' .env.mysql | |
| # Start container with MySQL configuration | |
| docker run -d -p 8000:8000 --name education-app \ | |
| --network host \ | |
| -v $(pwd)/.env.mysql:/app/.env \ | |
| -e DJANGO_DB_ENGINE=django.db.backends.mysql \ | |
| -e DJANGO_DB_NAME=test_db \ | |
| -e DJANGO_DB_USER=root \ | |
| -e DJANGO_DB_PASSWORD=rootpassword \ | |
| -e DJANGO_DB_HOST=127.0.0.1 \ | |
| -e DJANGO_DB_PORT=3306 \ | |
| -e DJANGO_SETTINGS_MODULE=web.settings \ | |
| education-website:test | |
| # Wait for container to start | |
| sleep 10 | |
| # Now run migrations and setup database inside the container | |
| docker exec education-app python manage.py migrate | |
| docker exec education-app python manage.py create_test_data | |
| # Create superuser if needed | |
| docker exec -e DJANGO_SUPERUSER_USERNAME=admin -e DJANGO_SUPERUSER_EMAIL=admin@example.com -e DJANGO_SUPERUSER_PASSWORD=adminpassword education-app python manage.py createsuperuser --noinput || true | |
| # Verify database connection | |
| docker exec education-app python -c "from django.db import connection; print('Database backend:', connection.vendor)" | |
| # Wait for app to be fully up | |
| sleep 5 | |
| docker ps | |
| - name: Check container logs | |
| run: docker logs education-app | |
| - name: Test HTTP endpoint | |
| run: | | |
| # Check if the website is responding | |
| curl -s --retry 5 --retry-delay 5 --retry-connrefused http://localhost:8000/en/ -o /dev/null -w "%{http_code}\n" | grep -q "200" && echo "Website is up!" || (echo "Website is down!" && exit 1) | |
| - name: Run Django tests in container | |
| run: | | |
| docker exec education-app python manage.py test --verbosity=3 | |
| - name: Stop Docker container | |
| if: always() | |
| run: docker stop education-app || true | |
| - name: Remove Docker container | |
| if: always() | |
| run: docker rm education-app || true |