Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#deploy problems #307

Merged
merged 14 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/db_deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Postgres_Deploy
on: workflow_dispatch

env:
DBS: "${{ vars.DEV_DB }},${{ vars.PROD_DB }}"
POSTGRES_USER: ${{ vars.POSTGRES_USER }}
POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }}
DATABASE: ${{ vars.POSTGRES_DB }}

PGADMIN_DEFAULT_EMAIL: ${{ vars.PGADMIN_DEFAULT_EMAIL }}
PGADMIN_DEFAULT_PASSWORD: ${{ secrets.PGADMIN_DEFAULT_PASSWORD }}

jobs:
deploy:
runs-on: self-hosted

steps:
- name: Checkout
uses: actions/checkout@v3
- name: Docker compouse DOWN old containers
run: docker compose -f docker-compose.db.yml down -v
- name: Docker compouse UP new containers
run: docker compose -f docker-compose.db.yml up -d --build
- name: Clean up old docker resources
run: |
docker container prune -f
docker image prune -f
44 changes: 44 additions & 0 deletions .github/workflows/django_cd_dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Django CD DEV
on: workflow_dispatch

env:
DEBUG: ${{ vars.DEV_DEBUG }}
SECRET_KEY: ${{ secrets.DEV_SECRET_KEY }}
ENGINE: ${{ vars.ENGINE }}
DB_HOST: ${{ vars.POSTGRES_HOST }}
DB_PORT: ${{ vars.POSTGRES_PORT }}
PG_DB: ${{ vars.DEV_DB }}
PG_USER: ${{ vars.POSTGRES_USER }}
PG_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }}
POSTGRES_DB: ${{ vars.POSTGRES_DB }}
EMAIL_BACKEND: ${{ vars.EMAIL_BACKEND }}
EMAIL_HOST: ${{ vars.EMAIL_HOST }}
EMAIL_PORT: ${{ vars.EMAIL_PORT }}
EMAIL_USE_TLS: ${{ vars.EMAIL_USE_TLS }}
EMAIL_HOST_USER: ${{ vars.EMAIL_HOST_USER }}
EMAIL_HOST_PASSWORD: ${{ vars.EMAIL_HOST_PASSWORD }}
CORS_ORIGIN_WHITELIST: ${{ vars.CORS_ORIGIN_WHITELIST }}
REACT_APP_BASE_API_URL: ${{ vars.REACT_APP_BASE_API_URL }}
REACT_APP_PUBLIC_URL: ${{ vars.REACT_APP_PUBLIC_URL }}

jobs:
deploy:
runs-on: self-hosted

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Docker compouse DOWN old Django container and frontend container (+internal nginx)
run: docker compose -f docker-compose.dev.yml down -v
- name: Docker compouse UP Django container and frontend container (+internal nginx)
run: docker compose -f docker-compose.dev.yml up -d --build
- name: Docker compouse MIGRATE models
run: docker compose -f docker-compose.dev.yml exec ${{ vars.API_DEV }} python manage.py migrate --noinput
- name: Docker compouse COLLECTSTATIC
run: docker compose -f docker-compose.dev.yml exec ${{ vars.API_DEV }} python manage.py collectstatic --no-input

- name: Clean up old docker resources
run: |
docker container prune -f
docker image prune -f
33 changes: 33 additions & 0 deletions .github/workflows/django_cd_prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Django CD PROD
on: workflow_dispatch

env:
DEBUG: ${{ vars.PROD_DEBUG }}
SECRET_KEY: ${{ secrets.PROD_SECRET_KEY }}
ENGINE: ${{ vars.ENGINE }}
HOST: ${{ vars.POSTGRES_HOST }}
PORT: ${{ vars.POSTGRES_PORT }}
DB: ${{ vars.PROD_DB }}
USER: ${{ vars.POSTGRES_USER }}
PASSWORD: ${{ secrets.POSTGRES_PASSWORD }}
DATABASE: ${{ vars.POSTGRES_DB }}

jobs:
deploy:
runs-on: self-hosted

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Docker compouse DOWN old Django container
run: docker compose -f docker-compose.dev.yml down -v
- name: Docker compouse UP Django container and frontend container (+internal nginx)
run: docker compose -f docker-compose.dev.yml up -d --build
- name: Docker compouse MIGRATE models
run: docker compose -f docker-compose.dev.yml exec api-dev python manage.py migrate --noinput

- name: Clean up old docker resources
run: |
docker container prune -f
docker image prune -f
58 changes: 58 additions & 0 deletions .github/workflows/django_ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: Django CI
on:
workflow_dispatch
env:
DEBUG: ${{ vars.DEV_DEBUG }}
SECRET_KEY: ${{ secrets.DEV_SECRET_KEY }}
ENGINE: ${{ vars.ENGINE }}
DB_HOST: 127.0.0.1
DB_PORT: ${{ vars.POSTGRES_PORT }}
PG_DB: ${{ vars.DEV_DB }}
PG_USER: ${{ vars.POSTGRES_USER }}
PG_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }}
POSTGRES_DB: ${{ vars.POSTGRES_DB }}
EMAIL_BACKEND: "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST: ""
EMAIL_PORT: ""
EMAIL_USE_TLS: ""
EMAIL_HOST_USER: ""
EMAIL_HOST_PASSWORD: ""
CORS_ORIGIN_WHITELIST: ${{ vars.CORS_ORIGIN_WHITELIST }}


jobs:
build:
runs-on: ubuntu-latest
strategy:
max-parallel: 4
matrix:
python-version: ["3.10"]
services:
postgres:
image: postgres:14
env:
POSTGRES_DB: ${{ vars.DEV_DB }}
POSTGRES_USER: ${{ vars.POSTGRES_USER }}
POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }}
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install Dependencies
run: |
pip install --upgrade pip
pip install -r ./requirements.txt
- name: Run migrations
run: python manage.py migrate
- name: Run Tests
run: python manage.py test
18 changes: 18 additions & 0 deletions .github/workflows/nginx_deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Nginx_Deploy
on: workflow_dispatch

jobs:
deploy:
runs-on: self-hosted

steps:
- name: Checkout
uses: actions/checkout@v3
- name: Docker compouse DOWN old containers
run: docker compose -f docker-compose.nginx.yml down -v
- name: Docker compouse UP new containers
run: docker compose -f docker-compose.nginx.yml up -d --build
- name: Clean up old docker resources
run: |
docker container prune -f
docker image prune -f
17 changes: 14 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
FROM python:3.9-alpine

WORKDIR /Forum
RUN mkdir -p /home/forum

RUN addgroup -S forum && adduser -S forum -G forum

ENV HOME=/home/forum
ENV APP_HOME=/home/forum/app
RUN mkdir $APP_HOME

WORKDIR $APP_HOME

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install psycopg2 dependencies
RUN apk update \
&& apk add postgresql-dev gcc python3-dev musl-dev

RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip3 install -r requirements.txt

COPY . /Forum
COPY . $APP_HOME

RUN chown -R forum:forum $APP_HOME

USER forum
11 changes: 0 additions & 11 deletions FrontEnd/docker-compose.yml

This file was deleted.

1 change: 1 addition & 0 deletions FrontEnd/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ server {
include /etc/nginx/mime.types;
try_files $uri $uri/ /index.html;
}

}
4 changes: 4 additions & 0 deletions configs/nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM nginx

RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d
39 changes: 39 additions & 0 deletions configs/nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
server {
listen 80;

# React
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://frontend:80;
proxy_redirect off;
}

# Django API
location /api/ {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://api-dev:8000/api/;
proxy_redirect off;
}

# Django Admin
location /admin/ {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://api-dev:8000/admin/;
proxy_redirect off;
}

# PGAdmin
location /pgadmin/ {
proxy_set_header X-Script-Name /pgadmin;
proxy_set_header Host $host;
proxy_pass http://pgadmin/;


proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_cache_bypass $http_upgrade;
}
}
22 changes: 22 additions & 0 deletions configs/postgres/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

set -e
set -u

function create_user_and_database() {
local database=$1
echo " Creating user and database '$database'"
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
CREATE USER $database;
CREATE DATABASE $database;
GRANT ALL PRIVILEGES ON DATABASE $database TO $database;
EOSQL
}

if [ -n "$POSTGRES_MULTIPLE_DATABASES" ]; then
echo "Multiple database creation requested: $POSTGRES_MULTIPLE_DATABASES"
for db in $(echo $POSTGRES_MULTIPLE_DATABASES | tr ',' ' '); do
create_user_and_database $db
done
echo "Multiple databases created"
fi
40 changes: 40 additions & 0 deletions docker-compose.db.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
version: "3.8"

services:
db:
image: postgres:14
container_name: db
restart: always
volumes:
- ./configs/postgres:/docker-entrypoint-initdb.d
- db_data:/var/lib/postgresql/data

# ports:
# - 5432:5432
networks:
- forum_network
environment:
POSTGRES_MULTIPLE_DATABASES: ${DBS}
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}

pgadmin:
image: dpage/pgadmin4
container_name: pgadmin
restart: always
environment:
PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL}
PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD}
ports:
- "5050:80"
volumes:
- pgadmin_data:/var/lib/pgadmin/data
depends_on:
- db
networks:
- forum_network
networks:
forum_network:
volumes:
db_data:
pgadmin_data:
Loading
Loading