diff --git a/.github/workflows/docker.yaml b/.github/workflows/docker.yaml index 5237eaa..bcb45d1 100644 --- a/.github/workflows/docker.yaml +++ b/.github/workflows/docker.yaml @@ -23,11 +23,29 @@ jobs: fetch-depth: 0 - uses: lsst-sqre/build-and-push-to-ghcr@v1 - id: build + id: base + with: + image: "lsst/ppdb-base" + dockerfile: "docker/Dockerfile.base" + github_token: ${{ secrets.GITHUB_TOKEN }} + push: true + + - uses: lsst-sqre/build-and-push-to-ghcr@v1 + id: cli + with: + image: "lsst/ppdb-cli" + dockerfile: "docker/Dockerfile.cli" + github_token: ${{ secrets.GITHUB_TOKEN }} + push: true + build-args: | + BASE_IMAGE=ghcr.io/lsst/ppdb-base:${{ steps.base.outputs.tag }} + + - uses: lsst-sqre/build-and-push-to-ghcr@v1 + id: replication with: image: "lsst/ppdb-replication" dockerfile: "docker/Dockerfile.replication" github_token: ${{ secrets.GITHUB_TOKEN }} push: true - - - run: echo Pushed ghcr.io/lsst/ppdb-replication:${{ steps.build.outputs.tag }} + build-args: | + BASE_IMAGE=ghcr.io/lsst/ppdb-base:${{ steps.base.outputs.tag }} diff --git a/docker/Dockerfile.base b/docker/Dockerfile.base new file mode 100644 index 0000000..a41e26a --- /dev/null +++ b/docker/Dockerfile.base @@ -0,0 +1,39 @@ +FROM python:3.11.6-slim-bookworm + +ENV DEBIAN_FRONTEND=noninteractive + +# Update and install OS dependencies +RUN apt-get -y update && \ + apt-get -y upgrade && \ + apt-get -y install --no-install-recommends git && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* + +# Install required python build dependencies +RUN pip install --upgrade --no-cache-dir pip setuptools wheel uv + +# Install optional dependencies +RUN uv pip install --no-cache-dir --system psycopg2-binary + +# Create the build directory +WORKDIR /build +COPY . /build + +# Install requirements +RUN uv pip install --no-cache-dir --system -r requirements.txt + +# Install the package +RUN uv pip install --no-cache-dir --system --no-deps . + +# Remove the build directory +RUN rm -rf /build + +# Setup the application scripts +WORKDIR /app + +# Install sdm_schemas +# Change this using: -e SDM_SCHEMAS_REF=branch_or_tag_name +ENV SDM_SCHEMAS_REF=main +COPY ./docker/scripts/download-sdm-schemas.sh . +RUN ./download-sdm-schemas.sh && rm download-sdm-schemas.sh +ENV SDM_SCHEMAS_DIR=/app/sdm_schemas diff --git a/docker/Dockerfile.cli b/docker/Dockerfile.cli new file mode 100644 index 0000000..862983d --- /dev/null +++ b/docker/Dockerfile.cli @@ -0,0 +1,4 @@ +ARG BASE_IMAGE=ghcr.io/lsst/ppdb-base:main +FROM ${BASE_IMAGE} + +ENTRYPOINT ["ppdb-cli"] diff --git a/docker/Dockerfile.replication b/docker/Dockerfile.replication index ba86f5b..5c080d2 100644 --- a/docker/Dockerfile.replication +++ b/docker/Dockerfile.replication @@ -1,44 +1,14 @@ -FROM python:3.11.6-slim-bookworm +ARG BASE_IMAGE=ghcr.io/lsst/ppdb-base:main +FROM ${BASE_IMAGE} -ENV DEBIAN_FRONTEND=noninteractive +# Install optional requirements +RUN uv pip install --no-cache-dir --system cassandra-driver -# Update and install OS dependencies -RUN apt-get -y update && \ - apt-get -y upgrade && \ - apt-get -y install --no-install-recommends git && \ - apt-get clean && \ - rm -rf /var/lib/apt/lists/* - -# Install required python build dependencies -RUN pip install --upgrade --no-cache-dir pip setuptools wheel uv - -# Create the build directory -WORKDIR /build -COPY . /build - -# Install requirements -RUN uv pip install --no-cache-dir --system cassandra-driver psycopg2-binary -RUN uv pip install --no-cache-dir --system -r requirements.txt - -# Install the package -RUN uv pip install --no-cache-dir --system --no-deps . - -# Setup the application scripts WORKDIR /app -# Install sdm_schemas -# Change this using: -e SDM_SCHEMAS_REF=branch_or_tag_name -ENV SDM_SCHEMAS_REF=main -COPY ./docker/scripts/download-sdm-schemas.sh . -RUN ./download-sdm-schemas.sh && rm download-sdm-schemas.sh -ENV SDM_SCHEMAS_DIR=/app/sdm_schemas - # Copy the entrypoint script COPY docker/scripts/entrypoint-replication.sh . RUN chmod +x /app/entrypoint-replication.sh -# Remove the build directory -RUN rm -rf /build - # Run the wrapper script for the ppdb-replication command CMD ["/app/entrypoint-replication.sh"] diff --git a/docker/scripts/build.sh b/docker/scripts/build.sh new file mode 100755 index 0000000..7e23c99 --- /dev/null +++ b/docker/scripts/build.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash + +############################################################################## +# Build the PPDB Docker images - run this script from the root of the project. +# +# This should only be used for local development. The production images are +# built and pushed to the container registry from a GitHub Action. +############################################################################## + +set -e + +# Build the base image +docker build -t lsst/ppdb-base:local -f docker/Dockerfile.base . + +# Build the CLI image +docker build --build-arg BASE_IMAGE=lsst/ppdb-base:local -t lsst/ppdb-cli:local -f docker/Dockerfile.cli . + +# Build the replication image +docker build --build-arg BASE_IMAGE=lsst/ppdb-base:local -t lsst/ppdb-replication:local -f docker/Dockerfile.replication . diff --git a/docker/scripts/entrypoint-replication.sh b/docker/scripts/entrypoint-replication.sh index e7ced8a..7fbfccf 100755 --- a/docker/scripts/entrypoint-replication.sh +++ b/docker/scripts/entrypoint-replication.sh @@ -16,19 +16,16 @@ set -euo pipefail command -v ppdb-replication >/dev/null 2>&1 || { echo "ppdb-replication command not found"; exit 1; } echo "Found ppdb-replication command" -# Function to check if an environment variable is set -check_env_var() { - local var_name="$1" - local var_value="${!var_name}" - if [ -z "${var_value:-}" ]; then - echo "$var_name is a required environment variable" - exit 1 - fi -} - # Check if the required environment variables are set -check_env_var "PPDB_REPLICATION_APDB_CONFIG" -check_env_var "PPDB_REPLICATION_PPDB_CONFIG" +if [ -z "${PPDB_REPLICATION_APDB_CONFIG:-}" ]; then + echo "ERROR: PPDB_REPLICATION_APDB_CONFIG is a required environment variable" + exit 1 +fi + +if [ -z "${PPDB_REPLICATION_PPDB_CONFIG:-}" ]; then + echo "ERROR: PPDB_REPLICATION_PPDB_CONFIG is a required environment variable" + exit 1 +fi # Build the command from the environment variables _CMD="ppdb-replication"