Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Absorb `build-nginx` job into `build-images` job (#944)
Dhruv Bhanushali097792482c65
diff --git a/.github/workflows/ci_cd.yml b/.github/workflows/ci_cd.ymlindex 66f6d8105..68bb64457 100644--- a/.github/workflows/ci_cd.yml+++ b/.github/workflows/ci_cd.yml@@ -91,16 +91,18 @@ jobs:if "frontend" in changes:build_matrix["image"].append("frontend")- build_matrix["include"].append({"image": "frontend", "target": "app"})+ build_matrix["include"].append({"image": "frontend", "context": "frontend", "target": "app"})publish_matrix["image"].append("frontend")if "api" in changes or "ingestion_server" in changes:# Always build the ingestion server and API images for either changesetbuild_matrix["image"] += ["api", "ingestion_server"]build_matrix["include"] += [- {"image": "ingestion_server", "target": "ing"},- {"image": "api", "target": "api"},+ {"image": "ingestion_server", "context": "ingestion_server", "target": "ing"},+ {"image": "api", "context": "api", "target": "api"},]if "api" in changes:+ build_matrix["image"].append("api_nginx")+ build_matrix["include"].append({"image": "api_nginx", "context": "api", "target": "nginx"})publish_matrix["image"] += ["api", "api_nginx"]if "ingestion_server" in changes:publish_matrix["image"].append("ingestion_server")@@ -230,7 +232,7 @@ jobs:- name: Build image `${{ matrix.image }}`uses: docker/build-push-action@v4with:- context: ${{ matrix.image }}+ context: ${{ matrix.context }}target: ${{ matrix.target }}push: falsetags: openverse-${{ matrix.image }}@@ -461,68 +463,6 @@ jobs:- name: Run makemigrationsrun: just api/dj makemigrations --check --noinput --merge- #########- # NGINX #- #########-- build-nginx:- # This requires a separate job due because it uses the API image built by- # the `build-images` job.- name: Build `nginx` Docker image- runs-on: ubuntu-latest- if: needs.get-changes.outputs.api == 'true'- needs:- - get-changes- - build-images- - get-image-tag-- steps:- - name: Checkout repository- uses: actions/checkout@v3-- - name: Setup CI env- uses: ./.github/actions/setup-env- with:- github_token: ${{ secrets.GITHUB_TOKEN }}- setup_python: false- # Python is not needed to build the image.- setup_nodejs: false- # Node.js is not needed to build the image.- install_recipe: ""-- - name: Load Docker images- uses: ./.github/actions/load-img-- - name: collectstatic- run: |- just api/collectstatic- env:- DC_USER: root-- - name: Setup Docker Buildx- uses: docker/setup-buildx-action@v2- with:- install: true-- - name: Build image `api_nginx`- uses: docker/build-push-action@v4- with:- context: api- target: nginx # from `api/Dockerfile`- push: false- tags: openverse-api_nginx- cache-from: type=gha,scope=nginx- cache-to: type=gha,scope=nginx- outputs: type=docker,dest=/tmp/api_nginx.tar- build-args: |- SEMANTIC_VERSION=${{ needs.get-image-tag.outputs.image-tag }}-- - name: Upload image `api_nginx`- uses: actions/upload-artifact@v3- with:- name: api_nginx- path: /tmp/api_nginx.tar-############# Frontend #############@@ -911,7 +851,6 @@ jobs:- determine-images- get-image-tag- build-images- - build-nginx- test-ing # test for ingestion server- test-api # test for API- nuxt-build # test for frontenddiff --git a/api/Dockerfile b/api/Dockerfileindex 34362738b..c8d64b31b 100644--- a/api/Dockerfile+++ b/api/Dockerfile@@ -34,29 +34,6 @@ COPY Pipfile Pipfile.lock ./# Install Python dependencies system-wide (uses the active virtualenv)RUN pipenv install --system --deploy --dev-#########-# Nginx #-#########--# This target assumes that the build host has run `manage.py collectstatic`-# `just api/collectstatic` is provided as a convenient alias for running it the expected way--FROM nginx:1.23.3-alpine as nginx--LABEL org.opencontainers.image.source = "https://github.com/WordPress/openverse"--WORKDIR /app--COPY nginx.conf.template /etc/nginx/templates/openverse-api.conf.template-COPY /static /app/static--# Only environment variables with this prefix will be available in the template-ENV NGINX_ENVSUBST_FILTER="DJANGO_NGINX_"-ENV DJANGO_NGINX_ENVIRONMENT="local"-# Add the release version to the docker container-ARG SEMANTIC_VERSION-ENV DJANGO_NGINX_GIT_REVISION=$SEMANTIC_VERSION-######## API ########@@ -90,13 +67,24 @@ RUN apt-get update \&& rm -rf /var/lib/apt/lists/* \&& mkdir -p /var/log/openverse_api/openverse_api.log-# Create a non-root user-RUN useradd --create-home opener+# Create a folder for placing static files+RUN mkdir /static++# Create a non-root user, and make it the owner of the static dir created above+RUN useradd --create-home opener \+ && chown -R opener /staticUSER opener# Copy code into the final imageCOPY --chown=opener . /api/+# Collect static assets, these are used by the next stage, `nginx`+RUN env \+ SETUP_ES="False" \+ STATIC_ROOT="/static" \+ DJANGO_SECRET_KEY="any string" \+ python manage.py collectstatic+# Add the release version to the docker containerARG SEMANTIC_VERSIONENV SEMANTIC_VERSION=$SEMANTIC_VERSION@@ -111,3 +99,25 @@ ENTRYPOINT ["./run.sh"]# Run Django dev server, can be overridden from Docker ComposeCMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]++#########+# NGINX #+#########++FROM nginx:1.23.3-alpine as nginx++LABEL org.opencontainers.image.source = "https://github.com/WordPress/openverse"++WORKDIR /app++COPY nginx.conf.template /etc/nginx/templates/openverse-api.conf.template++# Copy static files from `api` target+COPY --from=api /static /app/static++# Only environment variables with this prefix will be available in the template+ENV NGINX_ENVSUBST_FILTER="DJANGO_NGINX_"+ENV DJANGO_NGINX_ENVIRONMENT="local"+# Add the release version to the docker container+ARG SEMANTIC_VERSION+ENV DJANGO_NGINX_GIT_REVISION=$SEMANTIC_VERSIONdiff --git a/api/catalog/configuration/elasticsearch.py b/api/catalog/configuration/elasticsearch.pyindex af0df41ad..53a8e5997 100644--- a/api/catalog/configuration/elasticsearch.py+++ b/api/catalog/configuration/elasticsearch.py@@ -43,10 +43,14 @@ def _elasticsearch_connect():return _es-ES = _elasticsearch_connect()-#: Elasticsearch client, also aliased to connection 'default'--connections.add_connection("default", ES)+SETUP_ES = config("SETUP_ES", default=True, cast=bool)+if SETUP_ES:+ ES = _elasticsearch_connect()+ #: Elasticsearch client, also aliased to connection 'default'++ connections.add_connection("default", ES)+else:+ ES = NoneMEDIA_INDEX_MAPPING = {media_type: config(f"{media_type.upper()}_INDEX_NAME", default=media_type)diff --git a/api/catalog/settings.py b/api/catalog/settings.pyindex adbe01245..38058994a 100644--- a/api/catalog/settings.py+++ b/api/catalog/settings.py@@ -48,7 +48,7 @@ DEBUG = config("DJANGO_DEBUG_ENABLED", default=False, cast=bool)ENVIRONMENT = config("ENVIRONMENT", default="local")-ALLOWED_HOSTS = config("ALLOWED_HOSTS").split(",") + [+ALLOWED_HOSTS = config("ALLOWED_HOSTS", default="").split(",") + [gethostname(),gethostbyname(gethostname()),]diff --git a/api/env.template b/api/env.templateindex 128b5f03f..bcfb0ece1 100644--- a/api/env.template+++ b/api/env.template@@ -37,6 +37,7 @@ SEMANTIC_VERSION=1.0.0#WATERMARK_ENABLED=False+#SETUP_ES=True#ELASTICSEARCH_URL=es#ELASTICSEARCH_PORT=9200#ELASTICSEARCH_AWS_REGION=us-east-1diff --git a/api/justfile b/api/justfileindex 0b89f2fbf..54ae38b8e 100644--- a/api/justfile+++ b/api/justfile@@ -83,13 +83,6 @@ ipython:dbshell:just dj dbshell-# Run `collectstatic` to prepare for building the `nginx` Dockerfile target.-collectstatic:- # The STATIC_ROOT environment variable is relative to the Django container's- # default working directory i.e. the `/api/` directory. The resulting output- # will be `/api/static/`, which is ignored by Git for convenience.- STATIC_ROOT="./static" just dj collectstatic --noinput-########## Tests ##########@@ -105,3 +98,18 @@ test-local *args:# Run smoke test for the API docsdoc-test: wait-upcurl --fail 'http://localhost:50280/v1/?format=openapi'++#########+# NGINX #+#########++# Build and run the NGINX image locally+nginx upstream_url='api.openverse.engineering':+ # upstream_url can also be set to 172.17.0.1:50280 for local testing+ docker build --target nginx . -t openverse-api_nginx:latest+ @echo "--> NGINX server will be run at http://localhost:9090, upstream at {{ upstream_url }}"+ @echo "--> Try a static URL like http://localhost:9090/static/admin/css/base.css to test"+ docker run --rm -p 9090:8080 -it \+ -e DJANGO_NGINX_UPSTREAM_URL="{{ upstream_url }}" \+ -e DJANGO_NGINX_GIT_REVISION="$(git rev-parse HEAD)" \+ openverse-api_nginx:latestdiff --git a/docker/nginx/justfile b/docker/nginx/justfileindex d3f9be00b..7392c3bad 100644--- a/docker/nginx/justfile+++ b/docker/nginx/justfile@@ -10,22 +10,6 @@ NO_COLOR := "\\033[0m"just --list --unsorted-# Aliases collectstatic from the API directory-# https://github.com/casey/just/issues/1550-collectstatic:- just ../api/collectstatic--# Run the NGINX image locally-build-run upstream_url='api.openverse.engineering': collectstatic- # upstream_url can also be set to 172.17.0.1:50280 for local testing- docker build --target nginx . -t openverse-api_nginx:latest- @echo "--> NGINX server will be run at http://localhost:9090, upstream at {{ upstream_url }}"- @echo "--> Try a static URL like http://localhost:9090/static/admin/css/base.css to test"- docker run --rm -p 9090:8080 -it \- -e DJANGO_NGINX_UPSTREAM_URL="{{ upstream_url }}" \- -e DJANGO_NGINX_GIT_REVISION="$(git rev-parse HEAD)" \- openverse-api_nginx:latest-# Make locally trusted certificates (requires mkcert installed)cert:mkdir -p nginx/certs/