Skip to content

Commit

Permalink
updated deploypreview workflow and docker files accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
OchiengPaul442 committed Dec 11, 2024
1 parent 60fe22e commit 8b99e73
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 15 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/deploy-previews.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1192,7 +1192,7 @@ jobs:
--timeout=60 \
--concurrency=10 \
--image=${{ env.REGISTRY_URL }}/${{ env.PROJECT_ID }}/pr-previews/website-pr-previews:${{ github.sha }} \
--port=8000 \
--port=8080 \
--cpu=1000m \
--memory=1024Mi \
--update-secrets=/etc/env/.env=sta-env-website-backend:latest,/etc/config/google_application_credentials.json=sta-key-analytics-service-account:latest \
Expand Down Expand Up @@ -1221,5 +1221,5 @@ jobs:
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'website changes in this PR available for preview [here](${{ needs.website.outputs.url }})'
})
body: 'website changes in this PR available for preview [here](${needs.website.outputs.url})'
})
12 changes: 5 additions & 7 deletions src/website/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ FROM python:3.11-slim
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set the working directory inside the container
WORKDIR /app

# Install system dependencies
Expand All @@ -14,18 +13,17 @@ RUN apt-get update && apt-get install -y \
libpq-dev \
&& apt-get clean

# Copy requirements file and install dependencies
COPY requirements.txt ./
# Copy requirements and install
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy the rest of the backend code into the container
# Copy the rest of the code
COPY . .

# Expose the port the Django app will run on
# Expose the port for Gunicorn
EXPOSE 8000

# Add execution permissions to entrypoint.sh
# Add execute permission to entrypoint.sh
RUN chmod +x /app/entrypoint.sh

# Set the entrypoint for the container
ENTRYPOINT ["/app/entrypoint.sh"]
37 changes: 37 additions & 0 deletions src/website/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
version: "3.8"
services:
web:
build:
context: .
dockerfile: Dockerfile
env_file: .env
expose:
- "8000"
depends_on:
- db
volumes:
- ./staticfiles:/app/staticfiles

nginx:
build:
context: ./nginx
dockerfile: Dockerfile
ports:
- "80:80"
depends_on:
- web
volumes:
# Mount the staticfiles into Nginx container
- ./staticfiles:/usr/share/nginx/html/static

db:
image: postgres:15
environment:
POSTGRES_USER: your_user
POSTGRES_PASSWORD: your_password
POSTGRES_DB: your_db
volumes:
- db_data:/var/lib/postgresql/data

volumes:
db_data:
7 changes: 2 additions & 5 deletions src/website/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
#!/bin/sh

# Exit immediately if a command exits with a non-zero status
# Exit on error
set -e

# Run Django migrations
echo "Running migrations..."
python manage.py migrate --noinput

# Collect static files (ensure the static files directory exists)
echo "Collecting static files..."
python manage.py collectstatic --noinput

# Start Gunicorn server to serve the Django application
echo "Starting Gunicorn server..."
echo "Starting Gunicorn..."
exec gunicorn core.wsgi:application --bind 0.0.0.0:8000 --timeout 600 --workers 3 --log-level info
10 changes: 10 additions & 0 deletions src/website/nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM nginx:alpine

# Remove default configuration
RUN rm /etc/nginx/conf.d/default.conf

# Copy your custom nginx configuration
COPY nginx.conf /etc/nginx/nginx.conf

# Expose port 80 for Nginx
EXPOSE 80
51 changes: 51 additions & 0 deletions src/website/nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
user nginx;
worker_processes auto;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;

include /etc/nginx/mime.types;
default_type application/octet-stream;

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

# Set client body size limit to 10MB
client_max_body_size 10M;

upstream django_app {
# Points to the Django/Gunicorn container service name defined in docker-compose
server web:8000;
}

server {
listen 80;
server_name _;

# Serving static files directly from Nginx
# Assuming 'staticfiles' directory is where Django collectstatic places files
location /static/ {
alias /usr/share/nginx/html/static/;
expires 1y;
access_log off;
add_header Cache-Control "public";
}

location / {
proxy_pass http://django_app;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}

0 comments on commit 8b99e73

Please sign in to comment.