1+ name : DRF CI/CD
2+
3+ on :
4+ push :
5+ branches :
6+ - develop
7+ pull_request :
8+
9+ jobs :
10+ test :
11+ runs-on : ubuntu-latest
12+
13+ services :
14+ postgres :
15+ image : postgres:16
16+ env :
17+ POSTGRES_DB : learning_machine
18+ POSTGRES_USER : postgres
19+ POSTGRES_PASSWORD : postgres
20+ ports :
21+ - 5432:5432
22+ options : >-
23+ --health-cmd="pg_isready -U postgres -d learning_machine"
24+ --health-interval=10s
25+ --health-timeout=5s
26+ --health-retries=5
27+
28+ redis :
29+ image : redis:7
30+ ports :
31+ - 6379:6379
32+
33+ env :
34+ SECRET_KEY : test-secret-key
35+ DEBUG : " True"
36+ ALLOWED_HOSTS : localhost,127.0.0.1
37+
38+ DATABASE_NAME : learning_machine
39+ DATABASE_USER : postgres
40+ DATABASE_PASSWORD : postgres
41+ DATABASE_HOST : localhost
42+ DATABASE_PORT : 5432
43+
44+ STRIPE_API_KEY : test-stripe-key
45+
46+ CELERY_BROKER_URL : redis://localhost:6379/0
47+ CELERY_RESULT_BACKEND : redis://localhost:6379/0
48+ CELERY_TASK_ALWAYS_EAGER : " False"
49+ CELERY_TASK_EAGER_PROPAGATES : " False"
50+
51+ EMAIL_HOST : smtp.example.com
52+ EMAIL_PORT : 465
53+ EMAIL_HOST_USER : test@example.com
54+ EMAIL_HOST_PASSWORD : test-password
55+ EMAIL_USE_TLS : " False"
56+ EMAIL_USE_SSL : " True"
57+ DEFAULT_FROM_EMAIL : test@example.com
58+
59+ steps :
60+ - name : Check out code
61+ uses : actions/checkout@v4
62+
63+ - name : Install Poetry
64+ run : pipx install poetry
65+
66+ - name : Set up Python
67+ uses : actions/setup-python@v4
68+ with :
69+ python-version : " 3.10"
70+ cache : " poetry"
71+
72+ - name : Install dependencies
73+ run : poetry install --no-interaction --no-root
74+
75+ - name : Run migrations
76+ run : poetry run python manage.py migrate
77+
78+ - name : Run tests
79+ run : poetry run python manage.py test
80+
81+ lint :
82+ runs-on : ubuntu-latest
83+ needs : test
84+
85+ steps :
86+ - name : Check out code
87+ uses : actions/checkout@v4
88+
89+ - name : Install Poetry
90+ run : pipx install poetry
91+
92+ - name : Set up Python
93+ uses : actions/setup-python@v4
94+ with :
95+ python-version : " 3.10"
96+ cache : " poetry"
97+
98+ - name : Install dependencies
99+ run : poetry install --no-interaction --no-root
100+
101+ - name : Run flake8
102+ run : poetry run flake8 . --exclude=.venv,venv,migrations,htmlcov --max-line-length=119
103+
104+ build :
105+ runs-on : ubuntu-latest
106+ needs : lint
107+
108+ steps :
109+ - name : Check out code
110+ uses : actions/checkout@v4
111+
112+ - name : Build Docker image
113+ run : docker build -t learning-machine:test .
114+
115+ deploy :
116+ runs-on : ubuntu-latest
117+ needs : build
118+ if : github.event_name == 'push' && github.ref == 'refs/heads/develop'
119+
120+ steps :
121+ - name : Check out code
122+ uses : actions/checkout@v4
123+
124+ - name : Set up SSH
125+ uses : webfactory/ssh-agent@v0.9.0
126+ with :
127+ ssh-private-key : ${{ secrets.SSH_KEY }}
128+
129+ - name : Add server to known hosts
130+ run : |
131+ mkdir -p ~/.ssh
132+ ssh-keyscan ${{ secrets.SERVER_IP }} >> ~/.ssh/known_hosts
133+
134+ - name : Create deploy directory on server
135+ run : |
136+ ssh ${{ secrets.SSH_USER }}@${{ secrets.SERVER_IP }} "mkdir -p ${{ secrets.DEPLOY_DIR }}"
137+
138+ - name : Copy project files to server
139+ run : |
140+ rsync -az --delete \
141+ --exclude '.git' \
142+ --exclude '.github' \
143+ --exclude '.env' \
144+ --exclude '.venv' \
145+ --exclude 'venv' \
146+ --exclude '__pycache__' \
147+ --exclude '.pytest_cache' \
148+ --exclude 'htmlcov' \
149+ --exclude 'media' \
150+ --exclude 'staticfiles' \
151+ ./ ${{ secrets.SSH_USER }}@${{ secrets.SERVER_IP }}:${{ secrets.DEPLOY_DIR }}/
152+
153+ - name : Create .env on server
154+ run : |
155+ ssh ${{ secrets.SSH_USER }}@${{ secrets.SERVER_IP }} << EOF
156+ cd ${{ secrets.DEPLOY_DIR }}
157+
158+ cat > .env << 'ENVEOF'
159+ ${{ secrets.ENV_FILE }}
160+ ENVEOF
161+ EOF
162+
163+ - name : Deploy with Docker Compose
164+ run : |
165+ ssh ${{ secrets.SSH_USER }}@${{ secrets.SERVER_IP }} << 'EOF'
166+ cd ${{ secrets.DEPLOY_DIR }}
167+
168+ docker compose -f docker-compose.prod.yml up -d db redis
169+ docker compose -f docker-compose.prod.yml build
170+
171+ docker compose -f docker-compose.prod.yml run --rm web python manage.py migrate
172+ docker compose -f docker-compose.prod.yml run --rm web python manage.py collectstatic --noinput
173+
174+ docker compose -f docker-compose.prod.yml up -d
175+
176+ docker image prune -f
177+ EOF
0 commit comments