diff --git a/.github/workflows/deploy-previews.yml b/.github/workflows/deploy-previews.yml index 2c6fa4aa50..2e942d6b98 100644 --- a/.github/workflows/deploy-previews.yml +++ b/.github/workflows/deploy-previews.yml @@ -1186,17 +1186,14 @@ jobs: - name: Deploy to Cloud Run run: |- - gcloud run deploy ${{ needs.branch-name.outputs.lowercase }}-website-preview \ - --region=${{ secrets.REGION }} \ + gcloud run deploy website-backend-nginix-website-preview \ + --region=${REGION} \ --max-instances=10 \ --timeout=60 \ --concurrency=10 \ - --image=${{ env.REGISTRY_URL }}/${{ env.PROJECT_ID }}/pr-previews/website-pr-previews:${{ github.sha }} \ - --port=8080 \ + --image=${REGISTRY_URL}/${PROJECT_ID}/pr-previews/website-pr-previews:${GITHUB_SHA} \ --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 \ - --command="/bin/sh","-c","cat /etc/env/.env >> /app/.env; /app/entrypoint.sh" \ --allow-unauthenticated - name: Get preview service url diff --git a/src/website/Dockerfile b/src/website/Dockerfile index 48f41122d1..1b163df12e 100644 --- a/src/website/Dockerfile +++ b/src/website/Dockerfile @@ -11,6 +11,8 @@ WORKDIR /app RUN apt-get update && apt-get install -y \ gcc \ libpq-dev \ + nginx \ + supervisor \ && apt-get clean # Copy requirements and install @@ -20,10 +22,23 @@ RUN pip install --no-cache-dir -r requirements.txt # Copy the rest of the code COPY . . -# Expose the port for Gunicorn -EXPOSE 8000 +# Remove default Nginx config +RUN rm /etc/nginx/conf.d/default.conf + +# Make sure we have a directory for static files (to serve them via Nginx) +RUN mkdir -p /usr/share/nginx/html/static/ # Add execute permission to entrypoint.sh RUN chmod +x /app/entrypoint.sh +# Copy custom Nginx config +COPY nginx.conf /etc/nginx/nginx.conf + +# Copy supervisor config +COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf + +# Default port for Cloud Run is 8080, ensure Nginx listens on this port +ENV PORT 8080 +EXPOSE 8080 + ENTRYPOINT ["/app/entrypoint.sh"] diff --git a/src/website/docker-compose.yml b/src/website/docker-compose.yml deleted file mode 100644 index 2da1611a56..0000000000 --- a/src/website/docker-compose.yml +++ /dev/null @@ -1,37 +0,0 @@ -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: diff --git a/src/website/entrypoint.sh b/src/website/entrypoint.sh index 8aeb5a94b8..e0b79fe390 100644 --- a/src/website/entrypoint.sh +++ b/src/website/entrypoint.sh @@ -9,5 +9,5 @@ python manage.py migrate --noinput echo "Collecting static files..." python manage.py collectstatic --noinput -echo "Starting Gunicorn..." -exec gunicorn core.wsgi:application --bind 0.0.0.0:8000 --timeout 600 --workers 3 --log-level info +echo "Starting Supervisor (which runs Nginx + Gunicorn)..." +exec /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf diff --git a/src/website/nginx/nginx.conf b/src/website/nginx.conf similarity index 72% rename from src/website/nginx/nginx.conf rename to src/website/nginx.conf index bb2f31dcc3..f0f7f09eb7 100644 --- a/src/website/nginx/nginx.conf +++ b/src/website/nginx.conf @@ -19,20 +19,17 @@ http { 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; + # Gunicorn will be running on 127.0.0.1:8000 inside the same container + server 127.0.0.1:8000; } server { - listen 80; + listen ${PORT}; 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; diff --git a/src/website/nginx/Dockerfile b/src/website/nginx/Dockerfile deleted file mode 100644 index 3f6c8e8f6a..0000000000 --- a/src/website/nginx/Dockerfile +++ /dev/null @@ -1,10 +0,0 @@ -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 diff --git a/src/website/supervisord.conf b/src/website/supervisord.conf new file mode 100644 index 0000000000..1636b94653 --- /dev/null +++ b/src/website/supervisord.conf @@ -0,0 +1,13 @@ +[supervisord] +nodaemon=true + +[program:gunicorn] +command=gunicorn core.wsgi:application --bind 0.0.0.0:8000 --timeout 600 --workers 3 --log-level info +directory=/app +autostart=true +autorestart=true + +[program:nginx] +command=/usr/sbin/nginx -g "daemon off;" +autostart=true +autorestart=true