Skip to content

Commit

Permalink
Feature/cd deploy (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
Reagent992 authored Jan 27, 2024
1 parent 997ccba commit 2815d8d
Show file tree
Hide file tree
Showing 27 changed files with 870 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ venv
db.sqlite3
.idea
.vscode
.env
.env
5 changes: 3 additions & 2 deletions .envexample
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ SECRET_KEY=django-insecure-pp6rzgeb5sjtbhfs(d-3*ibq67#0c-8jsd82@65!+=$satw167
DEBUG=False
TIME_ZONE=Europe/Moscow
LANGUAGE_CODE=ru-RU

ALLOWED_HOSTS=localhost,127.0.0.1,
CORS_ALLOWED_ORIGINS=localhost,127.0.0.1,
USE_POSTGRESQL=False
DB_NAME=IPR
DB_NAME=ipr
DB_HOST=db
DB_PORT=5432
POSTGRES_DB=ipr
Expand Down
62 changes: 62 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: ipr deploy

on:
push:
branches:
- main


jobs:
build_backend_and_push_to_docker_hub:
if: github.ref == 'refs/heads/main'
name: Push Docker image to DockerHub
runs-on: ubuntu-latest
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: .
push: true
tags: ${{ secrets.DOCKER_NICKNAME }}/backend_ipr:latest

deploy:
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
needs:
- build_backend_and_push_to_docker_hub
steps:
- name: Checkout repo
uses: actions/checkout@v3
- name: Copy docker-compose.yml via ssh
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USER }}
key: ${{ secrets.SSH_KEY }}
passphrase: ${{ secrets.SSH_PASSPHRASE }}
source: "docker-compose.production.yml"
target: "ipr"
- name: Executing remote ssh commands to deploy
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USER }}
key: ${{ secrets.SSH_KEY }}
passphrase: ${{ secrets.SSH_PASSPHRASE }}
script: |
cd ipr
sudo docker compose -f docker-compose.production.yml pull
sudo docker compose -f docker-compose.production.yml down
sudo docker compose -f docker-compose.production.yml up -d
sudo docker compose -f docker-compose.production.yml exec backend python manage.py migrate
sudo docker compose -f docker-compose.production.yml exec backend python manage.py collectstatic --noinput
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,5 @@ Thumbs.db
/media/
/collected_static/
.vscode
*/migrations/*
/static/
/ipr/migrations/
# Миграции убраны из git. Т.к. до существования БД проще без них.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.12
FROM python:3.12-slim

WORKDIR /app

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ TO-DO: Описание проекта.
| [Pillow](https://pypi.org/project/pillow/) | Библиотека для обработки изображений в Python. |
| [Django filter](https://pypi.org/project/django-filter/) | Библиотека для фильтрации данных в приложениях Django. |
| [Django Notifications](https://github.com/django-notifications/django-notifications) | Уведомления. |
| [django-cors-headers](https://pypi.org/project/django-cors-headers/) | Что-то делает с headers |
| [Flake8](https://pypi.org/project/flake8/), [black](https://pypi.org/project/black/), [isort](https://pypi.org/project/isort/), [Pre-commit](https://pypi.org/project/pre-commit/) | Инструменты для поддержания Code-Style в проекте. |

## Code-Style
Expand Down
38 changes: 38 additions & 0 deletions comments/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generated by Django 5.0.1 on 2024-01-27 21:31

from django.db import migrations, models


class Migration(migrations.Migration):
initial = True

dependencies = []

operations = [
migrations.CreateModel(
name="Comment",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"text",
models.TextField(
max_length=200, verbose_name="Комментарий"
),
),
(
"created_at",
models.DateField(
auto_now_add=True, verbose_name="Дата создания"
),
),
],
),
]
49 changes: 49 additions & 0 deletions comments/migrations/0002_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Generated by Django 5.0.1 on 2024-01-27 21:31

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):
initial = True

dependencies = [
("comments", "0001_initial"),
("ipr", "0001_initial"),
("tasks", "0001_initial"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.AddField(
model_name="comment",
name="author",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="comments",
to=settings.AUTH_USER_MODEL,
verbose_name="Автор комментария",
),
),
migrations.AddField(
model_name="comment",
name="ipr",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="comments",
to="ipr.ipr",
verbose_name="ИПР",
),
),
migrations.AddField(
model_name="comment",
name="task",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="comments",
to="tasks.task",
verbose_name="Задача",
),
),
]
Empty file added comments/migrations/__init__.py
Empty file.
38 changes: 26 additions & 12 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,19 @@
)
USE_POSTGRESQL = env.bool("USE_POSTGRESQL", default=False)
DEBUG = env.bool("DEBUG", default=False)
ALLOWED_HOSTS = env.list("ALLOWED_HOSTS", default=["localhost", "127.0.0.1"])
DB_NAME = env.str("DB_NAME", default="IPR")
DB_USER = env.str("POSTGRES_USER", default="username")
DB_PASSWORD = env.str("POSTGRES_PASSWORD", default="smart-password123")
DB_HOST = env.str("DB_HOST", default="db")
DB_PORT = env.int("DB_PORT", default=5432)
CORS_ALLOWED_ORIGINS = env.list(
"CORS_ALLOWED_ORIGINS", default=["localhost:80", "127.0.0.1:80"]
)
CSRF_TRUSTED_ORIGINS = CORS_ORIGINS_WHITELIST = CORS_ALLOWED_ORIGINS
# -----------------------------------------------------------------------------
BASE_DIR = Path(__file__).resolve().parent.parent

ALLOWED_HOSTS = ["localhost", "127.0.0.1"]

DJANGO_APPS = [
"django.contrib.admin",
Expand All @@ -34,6 +43,7 @@
"django_filters",
"drf_spectacular",
"notifications",
"corsheaders",
]
LOCAL_APPS = [
"api.v1.apps.ApiConfig",
Expand All @@ -49,6 +59,7 @@
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"corsheaders.middleware.CorsMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
Expand Down Expand Up @@ -80,11 +91,11 @@
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": os.getenv("DB_NAME"),
"USER": os.getenv("POSTGRES_USER"),
"PASSWORD": os.getenv("POSTGRES_PASSWORD"),
"HOST": os.getenv("DB_HOST"),
"PORT": os.getenv("DB_PORT"),
"NAME": DB_NAME,
"USER": DB_USER,
"PASSWORD": DB_PASSWORD,
"HOST": DB_HOST,
"PORT": DB_PORT,
}
}
else:
Expand All @@ -94,6 +105,7 @@
"NAME": BASE_DIR / "db.sqlite3",
}
}

AUTH_PASSWORD_VALIDATORS = [
{
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
Expand Down Expand Up @@ -135,6 +147,8 @@
],
"PAGE_SIZE": 10,
"DEFAULT_AUTHENTICATION_CLASSES": [
"rest_framework.authentication.BasicAuthentication",
"rest_framework.authentication.BasicAuthentication",
"rest_framework_simplejwt.authentication.JWTAuthentication",
],
"DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
Expand Down Expand Up @@ -177,9 +191,9 @@
"root",
)
RATING_CHOICES = (
(1, "1 звезда"),
(2, "2 звезды"),
(3, "3 звезды"),
(4, "4 звезды"),
(5, "5 звезд"),
)
(1, "1 звезда"),
(2, "2 звезды"),
(3, "3 звезды"),
(4, "4 звезды"),
(5, "5 звезд"),
)
34 changes: 34 additions & 0 deletions docker-compose.production.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
version: '3'

volumes:
pg_data:
static:
media:

services:
db:
image: postgres:13
env_file: .env
volumes:
- pg_data:/var/lib/postgresql/data
backend:
image: reamisd/backend_ipr:latest
env_file: .env
depends_on:
- db
volumes:
- static:/app/static
- media:/app/media
gateway:
image: nginx:latest
depends_on:
- backend
env_file: .env
ports:
- "8080:80"
volumes:
- static:/static
- media:/media
- ./default.conf:/etc/nginx/conf.d/default.conf
- ./error.log:/var/log/nginx/error.log
- ./access.log:/var/log/nginx/access.log
31 changes: 31 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
version: '3'

volumes:
pg_data:
static:
media:

services:
db:
image: postgres:13
env_file: .env
volumes:
- pg_data:/var/lib/postgresql/data
backend:
build: .
env_file: .env
depends_on:
- db
volumes:
- static:/app/static
- media:/app/media
gateway:
depends_on:
- backend
build: ./nginx/
env_file: .env
ports:
- "80:80"
volumes:
- static:/static
- media:/media
Loading

0 comments on commit 2815d8d

Please sign in to comment.