Skip to content

Commit

Permalink
Added production docker image and preview domain support (#1627)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasmcnulty authored Oct 2, 2024
1 parent 5c038b8 commit 1be8f13
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 43 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Create and publish a Docker image

on:
push:
branches: ["main"]

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
flavor: |
latest=true
tags: |
type=ref,event=branch
type=sha
- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
37 changes: 37 additions & 0 deletions .github/workflows/docker-test-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Docker test build

on:
pull_request:
push:
branches:
- main

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

permissions:
contents: read

jobs:
build-image:
runs-on: ubuntu-latest

strategy:
matrix:
req_file:
- prod.txt
- tests.txt

# steps taken (and trimmed) from docker-publish.yml
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Test docker image build (local development)
uses: docker/build-push-action@v6
with:
context: .
push: false
build-args: |
REQ_FILE=requirements/${{ matrix.req_file }}
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:

jobs:
tests:
runs-on: ubuntu-20.04
runs-on: ubuntu-24.04
strategy:
fail-fast: false

Expand All @@ -29,9 +29,9 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: 3.8
python-version: "3.8"
- name: Install dependencies
run: |
python -m pip install --upgrade pip setuptools coveralls "tox<4"
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ locale/*/LC_MESSAGES/django.mo
*/locale/*/LC_MESSAGES/django.mo
.sass-cache/
.coverage
.direnv
.envrc
.tox
djangoproject/cache
djangoproject/static/js/lib/jquery-flot/examples
Expand Down
13 changes: 8 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ RUN apt-get update \
&& apt-get install --assume-yes --no-install-recommends \
gettext \
git \
libpq5 \
make \
netcat-openbsd \
npm \
postgresql-client-13 \
rsync \
zlib1g \
&& rm -rf /var/lib/apt/lists/*

ARG REQ_FILE=requirements/prod.txt

# install python dependencies
COPY ./requirements ./requirements
RUN apt-get update \
Expand All @@ -28,22 +32,21 @@ RUN apt-get update \
gcc \
libc6-dev \
libpq-dev \
&& python3 -m pip install --no-cache-dir -r requirements/tests.txt \
zlib1g-dev \
&& python3 -m pip install --no-cache-dir -r ${REQ_FILE} \
&& apt-get purge --assume-yes --auto-remove \
gcc \
libc6-dev \
libpq-dev \
zlib1g-dev \
&& rm -rf /var/lib/apt/lists/*

# install node dependencies
COPY ./package.json ./package.json
RUN npm install

# copy docker-entrypoint.sh
COPY ./docker-entrypoint.sh ./docker-entrypoint.sh

# copy project
COPY . .

# run docker-entrypoint.sh
ENTRYPOINT ["./docker-entrypoint.sh"]
ENTRYPOINT ["./docker-entrypoint.prod.sh"]
20 changes: 11 additions & 9 deletions djangoproject/settings/prod.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
from .common import * # noqa

DOMAIN_NAME = os.getenv("DOMAIN_NAME", "djangoproject.com")

ALLOWED_HOSTS = [
"www.djangoproject.com",
"djangoproject.com",
"docs.djangoproject.com",
"dashboard.djangoproject.com",
f"www.{DOMAIN_NAME}",
DOMAIN_NAME,
f"docs.{DOMAIN_NAME}",
f"dashboard.{DOMAIN_NAME}",
] + SECRETS.get("allowed_hosts", [])

LOCALE_MIDDLEWARE_EXCLUDED_HOSTS = ["docs.djangoproject.com"]
LOCALE_MIDDLEWARE_EXCLUDED_HOSTS = [f"docs.{DOMAIN_NAME}"]

DEBUG = False
DEBUG = os.getenv("DJANGO_DEBUG", "false").lower() == "true"
THUMBNAIL_DEBUG = DEBUG

CACHES = {
Expand Down Expand Up @@ -37,7 +39,7 @@

MEDIA_ROOT = str(DATA_DIR.joinpath("media"))

MEDIA_URL = "https://media.djangoproject.com/"
MEDIA_URL = f"https://media.{DOMAIN_NAME}/"

MIDDLEWARE = (
["django.middleware.cache.UpdateCacheMiddleware"]
Expand All @@ -58,7 +60,7 @@

STATIC_ROOT = str(DATA_DIR.joinpath("static"))

STATIC_URL = "https://static.djangoproject.com/"
STATIC_URL = f"https://static.{DOMAIN_NAME}/"

# Docs settings
DOCS_BUILD_ROOT = DATA_DIR.joinpath("data", "docbuilds")
Expand All @@ -67,7 +69,7 @@

HOST_SCHEME = "https"

PARENT_HOST = "djangoproject.com"
PARENT_HOST = DOMAIN_NAME

# django-push settings

Expand Down
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ services:
build:
context: ./
dockerfile: Dockerfile
args:
- REQ_FILE=requirements/tests.txt
entrypoint: docker-entrypoint.dev.sh
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/usr/src/app/
Expand Down
25 changes: 25 additions & 0 deletions docker-entrypoint.dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/sh

echo "Waiting for postgres..."

while ! nc -z db 5432; do
sleep 0.1
done

echo "PostgreSQL started"

python -m manage flush --no-input
# PGPASSWORD=djangoproject psql --host db --port 5432 --username=code.djangoproject --dbname=code.djangoproject < tracdb/trac.sql
python -m manage migrate
make compile-scss # must come before collectstatic
python -m manage collectstatic --no-input --clear
python -m manage loaddata dev_sites
python -m manage loaddata doc_releases
# git config --global url."https://".insteadOf git://
# python -m manage update_docs
python -m manage loaddata dashboard_production_metrics
# python -m manage loaddata dashboard_example_data
python -m manage update_metrics
#python -m manage update_index

exec "$@"
8 changes: 8 additions & 0 deletions docker-entrypoint.prod.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh

python -m manage migrate
python -m manage compilemessages
make compile-scss # must come before collectstatic
python -m manage collectstatic --no-input --clear

exec "$@"
25 changes: 0 additions & 25 deletions docker-entrypoint.sh

This file was deleted.

3 changes: 2 additions & 1 deletion requirements/prod.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-r common.txt
gunicorn==23.0.0
redis==5.0.8
sentry-sdk==2.11.0
sentry-sdk==2.14.0

0 comments on commit 1be8f13

Please sign in to comment.