Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Deduce Python/Node.js/pnpm version in Docker images (#1225)

Dhruv Bhanushalib5f73c71aea9
diff --git a/.github/workflows/ci_cd.yml b/.github/workflows/ci_cd.yml
index 4abb02218..4c13bdc0c 100644
--- a/.github/workflows/ci_cd.yml
+++ b/.github/workflows/ci_cd.yml
@@ -161,26 +161,22 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v3
- # ℹ️Step only applies for frontend and catalog images.
- # Frontend downloads translations and catalog runs `just` recipes.
+ # Sets up `just` and Node.js.
+ # Node.js is needed for the frontend to download translations.
+ # `just` is needed to run the recipes and set build args.
- name: Setup CI env
- if: matrix.image == 'frontend' || matrix.image == 'catalog'
uses: ./.github/actions/setup-env
with:
setup_python: false
- # Python is not needed to build the image.
- install_recipe: node-install
+ setup_nodejs: ${{ matrix.image == 'frontend' && 'true' || 'false' }}
+ install_recipe: ${{ matrix.image == 'frontend' && 'node-install' || '' }}
- # ℹ️While this step only applies for catalog image, it is run for all.
- # This step
- # - sets the Python version used by the catalog image
- # - sets the Airflow version used by the catalog image
- - name: Prepare catalog for building
- id: prepare-catalog
- # if: matrix.image == 'catalog'
+ # Sets build args specifying versions needed to build Docker image.
+ - name: Prepare build args
+ id: prepare-build-args
run: |
- echo "python_version=$(just catalog/py-version)" >> "$GITHUB_OUTPUT"
- echo "airflow_version=$(just catalog/airflow-version)" >> "$GITHUB_OUTPUT"
+ just versions
+ just versions >> "$GITHUB_OUTPUT"
# ℹ️Step only applies for frontend image.
# This step
@@ -214,8 +210,12 @@ jobs:
outputs: type=docker,dest=/tmp/${{ matrix.image }}.tar
build-args: |
SEMANTIC_VERSION=${{ needs.get-image-tag.outputs.image_tag }}
- PROJECT_PY_VERSION=${{ steps.prepare-catalog.outputs.python_version }}
- PROJECT_AIRFLOW_VERSION=${{ steps.prepare-catalog.outputs.airflow_version }}
+ CATALOG_PY_VERSION=${{ steps.prepare-build-args.outputs.catalog_py_version }}
+ CATALOG_AIRFLOW_VERSION=${{ steps.prepare-build-args.outputs.catalog_airflow_version }}
+ API_PY_VERSION=${{ steps.prepare-build-args.outputs.api_py_version }}
+ INGESTION_PY_VERSION=${{ steps.prepare-build-args.outputs.ingestion_py_version }}
+ FRONTEND_NODE_VERSION=${{ steps.prepare-build-args.outputs.frontend_node_version }}
+ FRONTEND_PNPM_VERSION=${{ steps.prepare-build-args.outputs.frontend_pnpm_version }}
- name: Upload image `${{ matrix.image }}`
uses: actions/upload-artifact@v3
diff --git a/api/Dockerfile b/api/Dockerfile
index a77d22c95..3ee907709 100644
--- a/api/Dockerfile
+++ b/api/Dockerfile
@@ -1,3 +1,6 @@
+# Automatically build image using Python version specified in the `Pipfile`.
+ARG API_PY_VERSION
+
##################
# Audio waveform #
##################
@@ -13,9 +16,13 @@ RUN ldd /usr/local/bin/audiowaveform | tr -s '[:blank:]' '\n' | grep '^/' | \
# Python builder #
##################
-FROM python:3.10 AS builder
+FROM python:${API_PY_VERSION} AS builder
+
+# Container optimizations
+ENV PYTHONUNBUFFERED=1
+ENV PIP_NO_CACHE_DIR=1
+ENV PIP_NO_COLOR=1
-ENV PYTHONBUFFERED=1
# Activate the virtualenv
ENV PATH="/venv/bin:$PATH"
@@ -38,11 +45,15 @@ RUN pipenv install --system --deploy --dev
# API #
#######
-FROM python:3.10-slim as api
+FROM python:${API_PY_VERSION}-slim AS api
LABEL org.opencontainers.image.source="https://github.com/WordPress/openverse"
-ENV PYTHONBUFFERED=1
+# Container optimizations
+ENV PYTHONUNBUFFERED=1
+ENV PIP_NO_CACHE_DIR=1
+ENV PIP_NO_COLOR=1
+
# Activate the virtualenv
ENV PATH="/venv/bin:$PATH"
diff --git a/api/justfile b/api/justfile
index 07dfc9bec..0c4db4c38 100644
--- a/api/justfile
+++ b/api/justfile
@@ -10,6 +10,20 @@ NO_COLOR := "\\033[0m"
just --list --unsorted
+###########
+# Version #
+###########
+
+export API_PY_VERSION := `grep 'python_version' Pipfile | awk -F' = ' '{print $2}' | sed 's/"//g'`
+
+# Print the Python version specified in the `Pipfile`
+@py-version:
+ echo $API_PY_VERSION
+
+###########
+# Install #
+###########
+
# Install dependencies
install *args="--dev":
pipenv install {{ args }}
diff --git a/catalog/Dockerfile b/catalog/Dockerfile
index 096615fa4..9d65fc98a 100644
--- a/catalog/Dockerfile
+++ b/catalog/Dockerfile
@@ -1,9 +1,9 @@
# Cannot use $PYTHON_VERSION because that is overwritten by the base image
# with the patch version (we only care about major and minor version)
-ARG PROJECT_PY_VERSION
-ARG PROJECT_AIRFLOW_VERSION
+ARG CATALOG_PY_VERSION
+ARG CATALOG_AIRFLOW_VERSION
-FROM apache/airflow:slim-${PROJECT_AIRFLOW_VERSION}-python${PROJECT_PY_VERSION} as cat
+FROM apache/airflow:slim-${CATALOG_AIRFLOW_VERSION}-python${CATALOG_PY_VERSION} as cat
LABEL org.opencontainers.image.source = "https://github.com/WordPress/openverse"
@@ -58,11 +58,11 @@ WORKDIR ${AIRFLOW_HOME}
COPY ${REQUIREMENTS_FILE} requirements_prod.txt ${AIRFLOW_HOME}/
# args go out of scope when a new build stage starts so it must be redeclared here
-ARG PROJECT_PY_VERSION
-ARG PROJECT_AIRFLOW_VERSION
+ARG CATALOG_PY_VERSION
+ARG CATALOG_AIRFLOW_VERSION
# https://airflow.apache.org/docs/apache-airflow/stable/installation/installing-from-pypi.html#constraints-files
-ARG CONSTRAINTS_FILE="https://raw.githubusercontent.com/apache/airflow/constraints-${PROJECT_AIRFLOW_VERSION}/constraints-${PROJECT_PY_VERSION}.txt"
+ARG CONSTRAINTS_FILE="https://raw.githubusercontent.com/apache/airflow/constraints-${CATALOG_AIRFLOW_VERSION}/constraints-${CATALOG_PY_VERSION}.txt"
RUN pip install --user -r ${REQUIREMENTS_FILE} -c ${CONSTRAINTS_FILE}
diff --git a/catalog/justfile b/catalog/justfile
index f6c0ac54b..4cbf630bd 100644
--- a/catalog/justfile
+++ b/catalog/justfile
@@ -16,16 +16,16 @@ SERVICE := env_var_or_default("SERVICE", "scheduler")
# Version #
###########
-export PROJECT_PY_VERSION := `grep '# PYTHON' requirements_prod.txt | awk -F= '{print $2}'`
-export PROJECT_AIRFLOW_VERSION := `grep '^apache-airflow' requirements_prod.txt | awk -F= '{print $3}'`
+export CATALOG_PY_VERSION := `grep '# PYTHON' requirements_prod.txt | awk -F= '{print $2}'`
+export CATALOG_AIRFLOW_VERSION := `grep '^apache-airflow' requirements_prod.txt | awk -F= '{print $3}'`
# Print the required Python version
@py-version:
- echo $PROJECT_PY_VERSION
+ echo $CATALOG_PY_VERSION
# Print the current Airflow version
@airflow-version:
- echo $PROJECT_AIRFLOW_VERSION
+ echo $CATALOG_AIRFLOW_VERSION
###########
# Install #
@@ -34,7 +34,7 @@ export PROJECT_AIRFLOW_VERSION := `grep '^apache-airflow' requirements_prod.txt
# Create a virtual environment using the project Python version
venv:
# Invokes `python<version>`, like `python3.10`
- bash -c "python$PROJECT_PY_VERSION -m venv venv/"
+ bash -c "python$CATALOG_PY_VERSION -m venv venv/"
# Check that the active Python version matches the required Python version
check-py-version:
@@ -42,7 +42,7 @@ check-py-version:
import os
import sys
current = f"{sys.version_info[0]}.{sys.version_info[1]}"
- required = os.environ.get("PROJECT_PY_VERSION")
+ required = os.environ.get("CATALOG_PY_VERSION")
if current != required:
print(f"Detected Python version {current} but required {required}", file=sys.stderr)
sys.exit(1)
diff --git a/docker-compose.yml b/docker-compose.yml
index 6f52adaab..d5ee2c6db 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -15,10 +15,11 @@ x-airflow-common: &airflow-common
- catalog/.env
build:
context: ./catalog/
- args:
+ target: cat
+ args: # Automatically inferred from env vars, unless specified
- REQUIREMENTS_FILE=requirements_dev.txt
- - PROJECT_PY_VERSION=${PROJECT_PY_VERSION}
- - PROJECT_AIRFLOW_VERSION=${PROJECT_AIRFLOW_VERSION}
+ - CATALOG_PY_VERSION
+ - CATALOG_AIRFLOW_VERSION
volumes:
- ./catalog:/opt/airflow/catalog
- catalog-cache:/home/airflow/.cache
@@ -46,7 +47,9 @@ services:
- catalog_dependencies
- ingestion_server
- api
- build: ./docker/upstream_db/
+ build:
+ context: ./docker/upstream_db/
+ target: db
image: openverse-upstream_db
expose:
- "5432"
@@ -228,8 +231,9 @@ services:
build:
context: ./api/
target: api
- args:
- SEMANTIC_VERSION: ${SEMANTIC_VERSION:-v1.0.0}
+ args: # Automatically inferred from env vars, unless specified
+ - SEMANTIC_VERSION=${SEMANTIC_VERSION:-v1.0.0}
+ - API_PY_VERSION
image: openverse-api
volumes:
- ./api:/api
@@ -253,7 +257,11 @@ services:
profiles:
- ingestion_server
- api
- build: ./ingestion_server/
+ build:
+ context: ./ingestion_server/
+ target: ing
+ args: # Automatically inferred from env vars, unless specified
+ - INGESTION_PY_VERSION
image: openverse-ingestion_server
command: gunicorn -c ./gunicorn.conf.py
ports:
@@ -275,7 +283,11 @@ services:
profiles:
- ingestion_server
- api
- build: ./ingestion_server/
+ build:
+ context: ./ingestion_server/
+ target: ing
+ args: # Automatically inferred from env vars, unless specified
+ - INGESTION_PY_VERSION
image: openverse-ingestion_server
command: gunicorn -c ./gunicorn_worker.conf.py
expose:
diff --git a/frontend/Dockerfile b/frontend/Dockerfile
index 065f97555..40f909bd9 100644
--- a/frontend/Dockerfile
+++ b/frontend/Dockerfile
@@ -1,12 +1,18 @@
+# Automatically build image using Node.js version specified in `package.json`.
+ARG FRONTEND_NODE_VERSION
+
###################
# Node.js builder #
###################
-FROM node:16-alpine as builder
+FROM node:${FRONTEND_NODE_VERSION}-alpine as builder
+
+# Automatically use the right version of pnpm.
+ARG FRONTEND_PNPM_VERSION
# Install system packages needed to build on macOS
RUN apk add --no-cache --virtual .gyp python3 make g++ \
- && npm install -g [email protected]
+ && npm install -g pnpm@${FRONTEND_PNPM_VERSION}
USER node
diff --git a/frontend/justfile b/frontend/justfile
index d6a790f6e..5dcb9faad 100644
--- a/frontend/justfile
+++ b/frontend/justfile
@@ -10,6 +10,21 @@ NO_COLOR := "\\033[0m"
just --list --unsorted
+###########
+# Version #
+###########
+
+export FRONTEND_NODE_VERSION := `grep "volta" -A 1 ../package.json | tail -n 1 | awk -F'[:"]' '{print $5}' | cut -d. -f1`
+export FRONTEND_PNPM_VERSION := `grep 'packageManager' ../package.json | awk -F'@' '{print $2}' | sed 's/",//g'`
+
+# Print the Node.js version specified in `package.json`
+@node-version:
+ echo $FRONTEND_NODE_VERSION
+
+# Print the pnpm version specified in `package.json`
+@pnpm-version:
+ echo $FRONTEND_PNPM_VERSION
+
######
# Up #
######
diff --git a/ingestion_server/Dockerfile b/ingestion_server/Dockerfile
index 40893b7ea..b0ec96d91 100644
--- a/ingestion_server/Dockerfile
+++ b/ingestion_server/Dockerfile
@@ -1,10 +1,17 @@
+# Automatically build image using Python version specified in the `Pipfile`.
+ARG INGESTION_PY_VERSION
+
##################
# Python builder #
##################
-FROM python:3.10 as builder
+FROM python:${INGESTION_PY_VERSION} as builder
+
+# Container optimizations
+ENV PYTHONUNBUFFERED=1
+ENV PIP_NO_CACHE_DIR=1
+ENV PIP_NO_COLOR=1
-ENV PYTHONBUFFERED=1
# Activate the virtualenv
ENV PATH="/venv/bin:$PATH"
@@ -27,11 +34,15 @@ RUN pipenv install --system --deploy --dev
# Ingestion server #
####################
-FROM python:3.10-slim as ing
+FROM python:${INGESTION_PY_VERSION}-slim as ing
LABEL org.opencontainers.image.source="https://github.com/WordPress/openverse"
-ENV PYTHONBUFFERED=1
+# Container optimizations
+ENV PYTHONUNBUFFERED=1
+ENV PIP_NO_CACHE_DIR=1
+ENV PIP_NO_COLOR=1
+
# Activate the virtualenv
ENV PATH="/venv/bin:$PATH"
diff --git a/ingestion_server/justfile b/ingestion_server/justfile
index 849da977a..a2d8829a0 100644
--- a/ingestion_server/justfile
+++ b/ingestion_server/justfile
@@ -10,6 +10,20 @@ NO_COLOR := "\\033[0m"
just --list --unsorted
+###########
+# Version #
+###########
+
+export INGESTION_PY_VERSION := `grep 'python_version' Pipfile | awk -F' = ' '{print $2}' | sed 's/"//g'`
+
+# Print the Python version specified in the `Pipfile`
+@py-version:
+ echo $INGESTION_PY_VERSION
+
+###########
+# Install #
+###########
+
# Install dependencies
install *args="--dev":
pipenv install {{ args }}
diff --git a/ingestion_server/test/gen_integration_compose.py b/ingestion_server/test/gen_integration_compose.py
index d5798c145..8ea9f28f7 100644
--- a/ingestion_server/test/gen_integration_compose.py
+++ b/ingestion_server/test/gen_integration_compose.py
@@ -112,8 +112,8 @@ def _change_directories(conf: dict):
conf["services"][service]["volumes"] = ["../:/ingestion_server"]
conf["services"][service]["build"] = "../"
- upstream_db_build = conf["services"]["upstream_db"]["build"]
- conf["services"]["upstream_db"]["build"] = f"../../{upstream_db_build}"
+ upstream_db_build = conf["services"]["upstream_db"]["build"]["context"]
+ conf["services"]["upstream_db"]["build"]["context"] = f"../../{upstream_db_build}"
def _rename_services(conf: dict):
diff --git a/justfile b/justfile
index 5492f6a3a..13563e09d 100644
--- a/justfile
+++ b/justfile
@@ -123,8 +123,21 @@ DOCKER_FILE := "-f " + (
)
EXEC_DEFAULTS := if IS_CI == "" { "" } else { "-T" }
-export PROJECT_PY_VERSION := `just catalog/py-version`
-export PROJECT_AIRFLOW_VERSION := `just catalog/airflow-version`
+export CATALOG_PY_VERSION := `just catalog/py-version`
+export CATALOG_AIRFLOW_VERSION := `just catalog/airflow-version`
+export API_PY_VERSION := `just api/py-version`
+export INGESTION_PY_VERSION := `just ingestion_server/py-version`
+
+versions:
+ #!/usr/bin/env bash
+ cat <<EOF
+ catalog_py_version=$(just catalog/py-version)
+ catalog_airflow_version=$(just catalog/airflow-version)
+ api_py_version=$(just api/py-version)
+ ingestion_py_version=$(just ingestion_server/py-version)
+ frontend_node_version=$(just frontend/node-version)
+ frontend_pnpm_version=$(just frontend/pnpm-version)
+ EOF
# Run `docker-compose` configured with the correct files and environment
dc *args: