|
8 | 8 |
|
9 | 9 | jobs: |
10 | 10 | build: |
11 | | - |
| 11 | + # Use the latest Ubuntu runner |
12 | 12 | runs-on: ubuntu-latest |
| 13 | + |
| 14 | + # Matrix strategy (allows easy expansion to multiple Python versions if needed) |
13 | 15 | strategy: |
14 | | - max-parallel: 4 |
15 | 16 | matrix: |
16 | 17 | python-version: [3.12] |
17 | 18 |
|
| 19 | + # Service containers for PostgreSQL and Redis |
18 | 20 | services: |
19 | 21 | postgres: |
20 | 22 | image: postgres:13 |
21 | 23 | env: |
22 | 24 | POSTGRES_USER: postgres |
23 | 25 | POSTGRES_PASSWORD: postgres |
24 | 26 | POSTGRES_DB: twf |
| 27 | + # Health check to ensure PostgreSQL is ready before running tests |
25 | 28 | options: >- |
26 | | - --health-cmd="pg_isready -U postgres -d twf" |
27 | | - --health-interval=10s |
28 | | - --health-timeout=10s |
29 | | - --health-retries=10 |
30 | | - --health-start-period=30s |
| 29 | + --health-cmd "pg_isready -U postgres -d twf" |
| 30 | + --health-interval 10s |
| 31 | + --health-timeout 10s |
| 32 | + --health-retries 10 |
| 33 | + --health-start-period 10s |
| 34 | + redis: |
| 35 | + image: redis:latest |
| 36 | + # Health check to ensure Redis is ready before running tests |
| 37 | + options: >- |
| 38 | + --health-cmd "redis-cli ping" |
| 39 | + --health-interval 10s |
| 40 | + --health-timeout 5s |
| 41 | + --health-retries 5 |
| 42 | + --health-start-period 5s |
| 43 | +
|
| 44 | + # Set environment variables (Celery broker URL for Redis) |
| 45 | + env: |
| 46 | + CELERY_BROKER_URL: "redis://redis:6379/0" |
31 | 47 |
|
32 | 48 | steps: |
33 | | - - uses: actions/checkout@v3 |
34 | | - |
35 | | - - name: Set up Python ${{ matrix.python-version }} |
36 | | - uses: actions/setup-python@v3 |
37 | | - with: |
38 | | - python-version: ${{ matrix.python-version }} |
39 | | - |
40 | | - - name: Install Dependencies |
41 | | - run: | |
42 | | - python -m pip install --upgrade pip |
43 | | - pip install -r requirements.txt |
44 | | -
|
45 | | - - name: Create .env file |
46 | | - run: | |
47 | | - echo "DB_NAME=twf" >> .env |
48 | | - echo "DB_USER=postgres" >> .env |
49 | | - echo "DB_PASSWORD=postgres" >> .env |
50 | | - echo "DB_HOST=postgres" >> .env |
51 | | - echo "DB_PORT=5432" >> .env |
52 | | -
|
53 | | - - name: Install PostgreSQL client |
54 | | - run: sudo apt-get update && sudo apt-get install -y postgresql-client |
55 | | - |
56 | | - - name: Wait for PostgreSQL to be ready |
57 | | - run: | |
58 | | - echo "Waiting for PostgreSQL to be ready..." |
59 | | - for i in {1..45}; do |
60 | | - pg_isready -h postgres -U postgres -d twf && echo "PostgreSQL is ready!" && exit 0 |
61 | | - echo "Postgres not ready yet..." |
62 | | - sleep 2 |
63 | | - done |
64 | | - echo "PostgreSQL failed to start in time" |
65 | | - exit 1 |
66 | | -
|
67 | | - - name: Migrate database |
68 | | - run: | |
69 | | - python manage.py migrate |
70 | | -
|
71 | | - - name: Run Tests |
72 | | - run: | |
73 | | - python manage.py test |
74 | | -
|
75 | | - - name: Clean up .env file |
76 | | - run: rm .env |
| 49 | + # Checkout the repository code |
| 50 | + - uses: actions/checkout@v3 |
| 51 | + |
| 52 | + # Set up Python 3.12 |
| 53 | + - name: Set up Python ${{ matrix.python-version }} |
| 54 | + uses: actions/setup-python@v3 |
| 55 | + with: |
| 56 | + python-version: ${{ matrix.python-version }} |
| 57 | + |
| 58 | + # Install Python dependencies |
| 59 | + - name: Install Dependencies |
| 60 | + run: | |
| 61 | + python -m pip install --upgrade pip |
| 62 | + pip install -r requirements.txt |
| 63 | +
|
| 64 | + # Create .env file with database connection settings for Django |
| 65 | + - name: Create .env file |
| 66 | + run: | |
| 67 | + echo "DB_NAME=twf" >> .env |
| 68 | + echo "DB_USER=postgres" >> .env |
| 69 | + echo "DB_PASSWORD=postgres" >> .env |
| 70 | + echo "DB_HOST=postgres" >> .env |
| 71 | + echo "DB_PORT=5432" >> .env |
| 72 | +
|
| 73 | + # (Optional) Display .env contents for debugging |
| 74 | + - name: Check .env content |
| 75 | + run: cat .env |
| 76 | + |
| 77 | + # Install PostgreSQL and Redis client tools for health checks (pg_isready, redis-cli) |
| 78 | + - name: Install Postgres & Redis clients |
| 79 | + run: sudo apt-get update && sudo apt-get install -y postgresql-client redis-tools |
| 80 | + |
| 81 | + # Wait for PostgreSQL service to be ready (using pg_isready with retries) |
| 82 | + - name: Wait for PostgreSQL to be ready |
| 83 | + run: | |
| 84 | + echo "Waiting for PostgreSQL to be ready..." |
| 85 | + for i in {1..30}; do |
| 86 | + pg_isready -h postgres -U postgres -d twf && echo "PostgreSQL is ready! ✅" && exit 0 |
| 87 | + echo "PostgreSQL not ready yet, waiting..." |
| 88 | + sleep 2 |
| 89 | + done |
| 90 | + echo "PostgreSQL failed to start in time ❌" |
| 91 | + exit 1 |
| 92 | +
|
| 93 | + # Wait for Redis service to be ready (using redis-cli ping with retries) |
| 94 | + - name: Wait for Redis to be ready |
| 95 | + run: | |
| 96 | + echo "Waiting for Redis to be ready..." |
| 97 | + for i in {1..30}; do |
| 98 | + redis-cli -h redis ping && echo "Redis is ready! ✅" && exit 0 |
| 99 | + echo "Redis not ready yet, waiting..." |
| 100 | + sleep 2 |
| 101 | + done |
| 102 | + echo "Redis failed to start in time ❌" |
| 103 | + exit 1 |
| 104 | +
|
| 105 | + # (Optional) Start a Celery worker in the background to simulate task processing |
| 106 | + - name: Start Celery worker (background) |
| 107 | + run: | |
| 108 | + echo "Starting Celery worker in background..." |
| 109 | + celery -A twf worker --loglevel=info & |
| 110 | +
|
| 111 | + # Run Django database migrations |
| 112 | + - name: Migrate database |
| 113 | + run: python manage.py migrate |
| 114 | + |
| 115 | + # Run Django tests |
| 116 | + - name: Run tests |
| 117 | + run: python manage.py test |
| 118 | + |
| 119 | + # Clean up the .env file (remove sensitive data after tests) |
| 120 | + - name: Clean up .env file |
| 121 | + run: rm .env |
0 commit comments