Skip to content

Commit

Permalink
feat: whitenoise for statics, compose refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
fivehanz committed Oct 28, 2024
1 parent 5d04bcf commit 9dcb80e
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 68 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/docker-CI.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: Docker Buildx CI

on:
push:
branches:
- main
# push:
# branches:
# - main
pull_request:
branches:
- main
Expand Down
43 changes: 43 additions & 0 deletions .github/workflows/docker-ci-cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Docker CI/CD (prod)

on:
push:
branches:
- main

jobs:
build-and-push:
runs-on: ubuntu-24.04
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
platforms: linux/amd64,linux/arm64

- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PAT }}

- name: Extract commit hash as tag
run: |
VERSION=$(git rev-parse --short HEAD)
echo "VERSION=$VERSION" >> $GITHUB_ENV
- name: Build and push image
id: push
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: |
haniel56/hanz-web:${{ env.VERSION }}
haniel56/hanz-web:latest
platforms: linux/amd64,linux/arm64
2 changes: 1 addition & 1 deletion assets/css/output.css

Large diffs are not rendered by default.

73 changes: 34 additions & 39 deletions deployment/compose/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ services:
networks:
- hanz-nw
depends_on:
- db
- redis
environment:
- DATABASE_URL=postgres://user:dbpassword@db:5432/hanz-db
- REDIS_URL=redis://redis:6379/1
- RUN_MIGRATION=true
- COLLECT_STATIC=true
- UID=${UID:-1000}
- GID=${GID:-1000}
env_file:
Expand All @@ -24,26 +23,26 @@ services:
"granian --interface wsgi hanz.wsgi:application \
--host 0.0.0.0 --port 8000 --workers 3 --log-level info"

static_build:
build:
context: ../../
dockerfile: Dockerfile
container_name: hanz_prod_static_build
networks:
- hanz-nw
depends_on:
- db
- redis
environment:
- DATABASE_URL=postgres://user:dbpassword@db:5432/hanz-db
- REDIS_URL=redis://redis:6379/1
- COLLECT_STATIC=true
- UID=${UID:-1000}
- GID=${GID:-1000}
env_file:
- .env
volumes:
- ../../static:/app/src/static/
# static_build:
# build:
# context: ../../
# dockerfile: Dockerfile
# container_name: hanz_prod_static_build
# networks:
# - hanz-nw
# depends_on:
# - db
# - redis
# environment:
# - DATABASE_URL=postgres://user:dbpassword@db:5432/hanz-db
# - REDIS_URL=redis://redis:6379/1
# - COLLECT_STATIC=true
# - UID=${UID:-1000}
# - GID=${GID:-1000}
# env_file:
# - .env
# volumes:
# - ../../static:/app/src/static/

celery_worker:
container_name: hanz_celery_worker
Expand All @@ -52,7 +51,6 @@ services:
dockerfile: Dockerfile
command: celery -A hanz worker -l error
environment:
- DATABASE_URL=postgres://user:dbpassword@db:5432/hanz-db
- UID=${UID:-1000}
- GID=${GID:-1000}
env_file:
Expand All @@ -61,7 +59,6 @@ services:
- hanz-nw
depends_on:
- web
- db
- redis

celery_beat:
Expand All @@ -71,7 +68,6 @@ services:
dockerfile: Dockerfile
command: celery -A hanz beat -l error
environment:
- DATABASE_URL=postgres://user:dbpassword@db:5432/hanz-db
- UID=${UID:-1000}
- GID=${GID:-1000}
env_file:
Expand All @@ -80,22 +76,21 @@ services:
- hanz-nw
depends_on:
- web
- db
- redis

db:
image: postgres:15-alpine
container_name: hanz_prod_postgres_db
restart: unless-stopped
networks:
- hanz-nw
volumes:
- /opt/databases/prod/hanz-web/postgresql/db:/var/lib/postgresql/db
environment:
- POSTGRES_DB=hanz-db
- POSTGRES_USER=user
- POSTGRES_PASSWORD=dbpassword
- PGDATA=/var/lib/postgresql/db
# db:
# image: postgres:15-alpine
# container_name: hanz_prod_postgres_db
# restart: unless-stopped
# networks:
# - hanz-nw
# volumes:
# - /opt/databases/prod/hanz-web/postgresql/db:/var/lib/postgresql/db
# environment:
# - POSTGRES_DB=hanz-db
# - POSTGRES_USER=user
# - POSTGRES_PASSWORD=dbpassword
# - PGDATA=/var/lib/postgresql/db

redis:
image: redis:7-alpine
Expand Down
18 changes: 9 additions & 9 deletions deployment/nginx/vhost.conf
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ server {
proxy_send_timeout 300;
}

location /static {
gzip_static on;
alias /opt/apps/prod/hanz-web/static/;
try_files $uri =404;
}

location /media {
alias /opt/apps/prod/hanz-web/media/;
}
# location /static {
# gzip_static on;
# alias /opt/apps/prod/hanz-web/static/;
# try_files $uri =404;
# }
#
# location /media {
# alias /opt/apps/prod/hanz-web/media/;
# }

# Enable Gzip compression
gzip on;
Expand Down
28 changes: 14 additions & 14 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
services:
nginx:
image: nginx:stable
container_name: hanz_dev_nginx_server
restart: unless-stopped
ports:
- "8080:80"
networks:
- hanz-nw
volumes:
- ./deployment/nginx/nginx.conf:/etc/nginx/nginx.conf
- ./deployment/nginx/vhost.dev.conf:/etc/nginx/conf.d/default.conf
- ./media:/app/media
- ./static:/app/static
# nginx:
# image: nginx:stable
# container_name: hanz_dev_nginx_server
# restart: unless-stopped
# ports:
# - "8080:80"
# networks:
# - hanz-nw
# volumes:
# - ./deployment/nginx/nginx.conf:/etc/nginx/nginx.conf
# - ./deployment/nginx/vhost.dev.conf:/etc/nginx/conf.d/default.conf
# - ./media:/app/media
# - ./static:/app/static

web:
build:
context: .
dockerfile: dev.Dockerfile
container_name: hanz_app_dev
container_name: hanz_web_dev
command: "python manage.py runserver 0.0.0.0:8000"
restart: unless-stopped
volumes:
Expand Down
1 change: 1 addition & 0 deletions hanz/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"django.middleware.security.SecurityMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
"wagtail.contrib.redirects.middleware.RedirectMiddleware",
"wagtailcache.cache.FetchFromCacheMiddleware", # wagtail cache
]
Expand Down
18 changes: 16 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ wagtail-storages = "<2,>=1.1"
celery = "^5.4.0"
django-celery-beat = "^2.6.0"
django-plausible = "^0.5.0"
whitenoise = "^6.7.0"


[tool.poetry.group.dev.dependencies]
Expand Down

0 comments on commit 9dcb80e

Please sign in to comment.