From 0b3585dde31433b80ac074e6e3b227e965f272ff Mon Sep 17 00:00:00 2001 From: Tobias McNulty Date: Mon, 15 Sep 2025 21:54:48 -0400 Subject: [PATCH 1/4] Streamline Docker setup and run tests via Docker --- .dockerignore | 16 +++++++++++++++ .github/workflows/docker-test-build.yml | 11 ++++++++++ Makefile | 7 +++++++ README.rst | 20 ++++++++++++++++--- docker-compose.yml | 7 ++++++- .../01_trac_db.sql | 0 docker-entrypoint-initdb.d/02_trac_schema.sh | 3 +++ docker-entrypoint.dev.sh | 10 ---------- 8 files changed, 60 insertions(+), 14 deletions(-) create mode 100644 .dockerignore rename initdb/tracdb.sql => docker-entrypoint-initdb.d/01_trac_db.sql (100%) create mode 100755 docker-entrypoint-initdb.d/02_trac_schema.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000000..81df230348 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,16 @@ +# Copied from .gitignore +__pycache__ +data +*.pyc +*.db +locale/*/LC_MESSAGES/django.mo +*/locale/*/LC_MESSAGES/django.mo +.sass-cache/ +.coverage +.direnv +.envrc +djangoproject/cache +djangoproject/static/css/*.map +djangoproject/static/css/*.css +# Additional ignores for Docker +.git/ diff --git a/.github/workflows/docker-test-build.yml b/.github/workflows/docker-test-build.yml index 57f9634b04..91d40553be 100644 --- a/.github/workflows/docker-test-build.yml +++ b/.github/workflows/docker-test-build.yml @@ -25,6 +25,9 @@ jobs: - name: Checkout repository uses: actions/checkout@v4 + - name: Set up Docker Compose + uses: docker/setup-compose-action@v1 + - name: Test docker image build (local development) uses: docker/build-push-action@v6 with: @@ -32,3 +35,11 @@ jobs: push: false build-args: | REQ_FILE=requirements/${{ matrix.req_file }} + + - if: matrix.req_file == 'tests.txt' + name: Run the tests via Docker + run: | + docker compose up -d db + # Wait for Postgres to be ready + docker compose exec db sh -c 'until pg_isready ; do sleep 1; done' + docker compose run --rm web make ci diff --git a/Makefile b/Makefile index 2bf7ed7063..9f37e9dbad 100644 --- a/Makefile +++ b/Makefile @@ -30,3 +30,10 @@ test: watch-scss: watchmedo shell-command --patterns=*.scss --recursive --command="make compile-scss-debug" $(SCSS) + +reset-local-db: + python -m manage flush --no-input + python -m manage loaddata dev_sites + python -m manage loaddata doc_releases + python -m manage loaddata dashboard_production_metrics + python -m manage update_metrics diff --git a/README.rst b/README.rst index f5e30542de..ca95fa438e 100644 --- a/README.rst +++ b/README.rst @@ -354,12 +354,26 @@ Running Locally with Docker docker compose up -3. View the site at http://localhost:8000/ - -4. Run the tests:: +3. Run the tests:: docker compose run --rm web python -m manage test +4. Load the sample / local dev data:: + + docker compose run --rm web make reset-local-db + + If preferred, refer to the "Install and run locally from a virtual environment" + for more granular management commands to load specific data sets. + +5. View the site at http://www.djangoproject.localhost:8000/ + or http://dashboard.djangoproject.localhost:8000/. + +6. For docs, download the documentation (takes awhile):: + + docker compose exec -it web python -m manage update_docs + +7. View the docs at http://docs.djangoproject.localhost:8000/. + Pre-commit checks ----------------- diff --git a/docker-compose.yml b/docker-compose.yml index 48fce609b4..d50408a3ce 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -42,4 +42,9 @@ services: timeout: 10s retries: 10 volumes: - - ./initdb/tracdb.sql:/docker-entrypoint-initdb.d/tracdb.sql + # Mount the Postgres initialization scripts + - ./docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d + # This file contains the initial Trac database schema, but needs + # to be loaded into the database manually (via 02_trac_schema.sh) + # since it applies to the trac database, not the main Django database. + - ./tracdb/trac.sql:/trac.sql diff --git a/initdb/tracdb.sql b/docker-entrypoint-initdb.d/01_trac_db.sql similarity index 100% rename from initdb/tracdb.sql rename to docker-entrypoint-initdb.d/01_trac_db.sql diff --git a/docker-entrypoint-initdb.d/02_trac_schema.sh b/docker-entrypoint-initdb.d/02_trac_schema.sh new file mode 100755 index 0000000000..8024f468b6 --- /dev/null +++ b/docker-entrypoint-initdb.d/02_trac_schema.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +PGPASSWORD=secret psql --username=code.djangoproject --dbname=code.djangoproject < /trac.sql diff --git a/docker-entrypoint.dev.sh b/docker-entrypoint.dev.sh index a15a7149b8..06edae5407 100755 --- a/docker-entrypoint.dev.sh +++ b/docker-entrypoint.dev.sh @@ -1,17 +1,7 @@ #!/bin/sh -python -m manage flush --no-input -PGPASSWORD=secret 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 "$@" From a7a672b63a9c6e673aaaa2c8cba5711b2d77b7d0 Mon Sep 17 00:00:00 2001 From: Tobias McNulty Date: Tue, 23 Sep 2025 21:50:16 -0400 Subject: [PATCH 2/4] Used docker-compose for test build, build-push-action for non-test build --- .github/workflows/docker-test-build.yml | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/.github/workflows/docker-test-build.yml b/.github/workflows/docker-test-build.yml index ce513b0d76..3da0c55e7e 100644 --- a/.github/workflows/docker-test-build.yml +++ b/.github/workflows/docker-test-build.yml @@ -25,20 +25,8 @@ jobs: - name: Checkout repository uses: actions/checkout@v4 - - name: Set up Docker Compose - uses: docker/setup-compose-action@v1 - - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 - - - name: Log in to the Container registry - uses: docker/login-action@v3 - with: - registry: ${{ env.REGISTRY }} - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} - - - name: Test docker image build (local development) + - if: matrix.req_file != 'tests.txt' + name: Test docker image build (${{ matrix.req_file }}) uses: docker/build-push-action@v6 with: context: . @@ -46,9 +34,14 @@ jobs: build-args: | REQ_FILE=requirements/${{ matrix.req_file }} + - if: matrix.req_file == 'tests.txt' + name: Set up Docker Compose + uses: docker/setup-compose-action@v1 + - if: matrix.req_file == 'tests.txt' name: Run the tests via Docker run: | + docker compose build docker compose up -d db # Wait for Postgres to be ready docker compose exec db sh -c 'until pg_isready ; do sleep 1; done' From d1cb373ae70991ed8e250c8c7aac48f56b5b0977 Mon Sep 17 00:00:00 2001 From: Tobias McNulty Date: Wed, 24 Sep 2025 12:27:04 -0400 Subject: [PATCH 3/4] Removed duplicate settings from `docker.py` --- djangoproject/settings/docker.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/djangoproject/settings/docker.py b/djangoproject/settings/docker.py index 35216e40c6..1484a008e2 100644 --- a/djangoproject/settings/docker.py +++ b/djangoproject/settings/docker.py @@ -24,8 +24,3 @@ SECRET_KEY = os.environ.get("SECRET_KEY") ALLOWED_HOSTS = [".localhost", "127.0.0.1", "www.127.0.0.1"] - -LOCALE_MIDDLEWARE_EXCLUDED_HOSTS = ["docs.djangoproject.localhost"] - -# django-hosts settings -PARENT_HOST = "djangoproject.localhost:8000" From 67514f57ec31a57d743d4776e3dd12b86a241380 Mon Sep 17 00:00:00 2001 From: Tobias McNulty Date: Wed, 24 Sep 2025 13:58:31 -0400 Subject: [PATCH 4/4] Removed unnecessary lines --- .github/workflows/docker-test-build.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/docker-test-build.yml b/.github/workflows/docker-test-build.yml index 3da0c55e7e..5492325f6b 100644 --- a/.github/workflows/docker-test-build.yml +++ b/.github/workflows/docker-test-build.yml @@ -42,7 +42,4 @@ jobs: name: Run the tests via Docker run: | docker compose build - docker compose up -d db - # Wait for Postgres to be ready - docker compose exec db sh -c 'until pg_isready ; do sleep 1; done' docker compose run --rm web make ci