Skip to content

Update README.md

Update README.md #64

name: Foodgram Workflow
on:
push:
branches:
- '*'
jobs:
tests:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9", "3.9.20", "3.11"]
services:
postgres:
image: postgres:13.10
env:
POSTGRES_USER: foodgram_user
POSTGRES_PASSWORD: foodgram_password
POSTGRES_DB: foodgram
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8==6.0.0
pip install -r backend/requirements.txt
- name: Test with flake8 and django tests
env:
POSTGRES_USER: foodgram_user
POSTGRES_PASSWORD: foodgram_password
POSTGRES_DB: foodgram
DB_HOST: db
DB_PORT: 5432
run: |
cd backend
python -m flake8
build_and_push_to_docker_hub:
name: Push backend image to Docker Hub
runs-on: ubuntu-latest
needs: tests
if: github.ref == 'refs/heads/main'
steps:
- name: Check out the repo
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to Docker
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Push to Docker Hub
uses: docker/build-push-action@v4
with:
context: ./backend/
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/foodgram_backend:latest
build_frontend_and_push_to_docker_hub:
name: Push frontend image to DockerHub
runs-on: ubuntu-latest
needs: tests
if: github.ref == 'refs/heads/main'
steps:
- name: Check out the repo
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to Docker
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Push to DockerHub
uses: docker/build-push-action@v4
with:
context: ./frontend/
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/foodgram_frontend:latest
deploy:
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
needs:
- build_and_push_to_docker_hub
- build_frontend_and_push_to_docker_hub
steps:
- name: Check out the repo
uses: actions/checkout@v3
- name: Create project structure and copy files
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.SSH_USER }}
key: ${{ secrets.SSH_KEY }}
passphrase: ${{ secrets.SSH_PASSPHRASE }}
script: |
# Создаем необходимые директории
mkdir -p foodgram/docs
mkdir -p foodgram/frontend
mkdir -p foodgram/infra
- name: Copy project files
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.SSH_USER }}
key: ${{ secrets.SSH_KEY }}
passphrase: ${{ secrets.SSH_PASSPHRASE }}
source: "infra/docker-compose.yml,infra/default.conf,docs/openapi-schema.yml,docs/redoc.html"
target: "foodgram/"
strip_components: 1
- name: Setup environment and deploy
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.SSH_USER }}
key: ${{ secrets.SSH_KEY }}
passphrase: ${{ secrets.SSH_PASSPHRASE }}
script: |
cd foodgram/
# Перемещаем файлы в нужные директории
mv openapi-schema.yml docs/
mv redoc.html docs/
mv docker-compose.yml infra/
mv default.conf infra/
# Создаем и настраиваем .env файл
cd infra/
sudo touch .env
echo DB_ENGINE=django.db.backends.postgresql >> .env
echo POSTGRES_DB=${{ secrets.DB_NAME }} >> .env
echo POSTGRES_USER=${{ secrets.POSTGRES_USER }} >> .env
echo POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }} >> .env
echo DB_HOST=db >> .env
echo DB_PORT=5432 >> .env
echo SECRET_KEY="${{ secrets.SECRET_KEY }}" >> .env
echo ALLOWED_HOSTS=${{ secrets.ALLOWED_HOSTS }} >> .env
echo DEBUG=False >> .env
# Запускаем контейнеры
sudo docker compose down
sudo docker pull ${{ secrets.DOCKER_USERNAME }}/foodgram_backend:latest
sudo docker pull ${{ secrets.DOCKER_USERNAME }}/foodgram_frontend:latest
sudo docker compose up -d
- name: Execute database commands
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.SSH_USER }}
key: ${{ secrets.SSH_KEY }}
passphrase: ${{ secrets.SSH_PASSPHRASE }}
script: |
cd foodgram/infra/
# Ждем, пока база данных станет доступной
echo "Waiting for database to be ready..."
sleep 30
# Проверяем доступность базы данных
until sudo docker compose exec -T db pg_isready -U ${{ secrets.POSTGRES_USER }} -d ${{ secrets.DB_NAME }}; do
echo "Database is unavailable - sleeping"
sleep 2
done
echo "Database is up - executing commands"
# Выполняем миграции и другие команды
sudo docker compose exec -T backend python manage.py makemigrations
sudo docker compose exec -T backend python manage.py migrate
sudo docker compose exec -T backend python manage.py collectstatic --no-input
sudo docker compose exec -T backend python manage.py create_tags
sudo docker compose exec -T backend python manage.py load_ingredients data/ingredients.json
sudo docker compose exec -T backend python manage.py create_users
sudo docker compose exec -T backend python manage.py create_recipes
send_message:
runs-on: ubuntu-latest
needs: deploy
steps:
- name: Send message
uses: appleboy/telegram-action@master
with:
to: ${{ secrets.TELEGRAM_TO }}
token: ${{ secrets.TELEGRAM_TOKEN }}
message: |
Деплой Foodgram успешно завершен!
Коммит: ${{ github.event.head_commit.message }}
Ветка: ${{ github.ref }}
Автор: ${{ github.actor }}