From 27d7e9d15149c043f03b8dca725818f18a515927 Mon Sep 17 00:00:00 2001 From: Abolfazl Ziaeemehr Date: Tue, 4 Feb 2025 12:49:40 +0100 Subject: [PATCH] Refactor Docker image build workflow --- .github/workflows/docker-image.yml | 37 +++++++++++++++++ Dockerfile | 28 +++++++++++++ vbi/utils.py | 65 ++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+) create mode 100644 .github/workflows/docker-image.yml create mode 100644 Dockerfile diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml new file mode 100644 index 0000000..69b7fbe --- /dev/null +++ b/.github/workflows/docker-image.yml @@ -0,0 +1,37 @@ +name: build & push Docker image to ghcr + +on: + push: + tags: + - v* + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +jobs: + docker-build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Log in to the Container registry + uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + + - name: Build and push Docker image + uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f3fb861 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,28 @@ +# Use the smallest official Python image +FROM python:3.9-slim + +# Set environment variables to avoid interactive prompts +ENV DEBIAN_FRONTEND=noninteractive + +# Set the working directory inside the container +WORKDIR /app + +RUN apt-get update && apt-get install -y --no-install-recommends \ + swig \ + build-essential \ + git \ + && rm -rf /var/lib/apt/lists/* + + +# Copy only the package files (without installing dependencies) +COPY . . + +# Install Python dependencies +RUN pip install --no-cache-dir hatchling setuptools wheel && \ + pip install . + +# Optional: Support GPU version (comment out if not needed) +ARG GPU_SUPPORT=false +RUN if [ "$GPU_SUPPORT" = "true" ]; then pip install cupy; fi + +CMD ["python", "-c", "from vbi.utils import test_imports; test_imports()"] diff --git a/vbi/utils.py b/vbi/utils.py index 39d1ad0..6d65713 100644 --- a/vbi/utils.py +++ b/vbi/utils.py @@ -302,3 +302,68 @@ def set_diag(A: np.ndarray, k: int = 0, value: float = 0.0): idx = np.where(a1) A[idx] = value return A + + +def test_imports(): + """ + Import some of required dependencies and print their versions if available + and warn if not available. + + The required dependencies are: + - vbi + - numpy + - scipy + - matplotlib + - sbi + - torch [also check if GPU is available] + - cupy [also check if GPU is available] + + """ + + try: + import vbi + print(f"vbi: {vbi.__version__}") + except ImportError: + print("vbi not found") + + try: + import numpy + print(f"numpy: {numpy.__version__}") + except ImportError: + print("numpy not found") + + try: + import scipy + print(f"scipy: {scipy.__version__}") + except ImportError: + print("scipy not found") + + try: + import matplotlib + print(f"matplotlib: {matplotlib.__version__}") + except ImportError: + print("matplotlib not found") + + try: + import sbi + print(f"sbi: {sbi.__version__}") + except ImportError: + print("sbi not found") + + try: + import torch + print(f"torch: {torch.__version__}") + print(f"GPU available: {torch.cuda.is_available()}") + except ImportError: + print("torch not found") + + try: + import cupy + print(f"cupy: {cupy.__version__}") + print(f"GPU available: {cupy.cuda.is_available()}") + except ImportError: + print("cupy not found") + + + + \ No newline at end of file