Skip to content

Commit 564d958

Browse files
author
TWF Claude
committed
Fixing Github Workflow Actions
1 parent 4588c9d commit 564d958

1 file changed

Lines changed: 96 additions & 51 deletions

File tree

.github/workflows/django.yml

Lines changed: 96 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,69 +8,114 @@ on:
88

99
jobs:
1010
build:
11-
11+
# Use the latest Ubuntu runner
1212
runs-on: ubuntu-latest
13+
14+
# Matrix strategy (allows easy expansion to multiple Python versions if needed)
1315
strategy:
14-
max-parallel: 4
1516
matrix:
1617
python-version: [3.12]
1718

19+
# Service containers for PostgreSQL and Redis
1820
services:
1921
postgres:
2022
image: postgres:13
2123
env:
2224
POSTGRES_USER: postgres
2325
POSTGRES_PASSWORD: postgres
2426
POSTGRES_DB: twf
27+
# Health check to ensure PostgreSQL is ready before running tests
2528
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"
3147

3248
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

Comments
 (0)