From d42316adbed380bfafd6e998c65615f4da87cdae Mon Sep 17 00:00:00 2001 From: Peter Jung Date: Thu, 12 Sep 2024 16:02:14 +0200 Subject: [PATCH] Run pytests in Docker (#11) --- .github/workflows/python_ci.yaml | 23 ++++++++++++++++++----- Dockerfile | 10 ++++++++-- Makefile | 13 +++++++++++++ build_and_push.sh | 4 ---- 4 files changed, 39 insertions(+), 11 deletions(-) create mode 100644 Makefile delete mode 100755 build_and_push.sh diff --git a/.github/workflows/python_ci.yaml b/.github/workflows/python_ci.yaml index 70d823d..94502e3 100644 --- a/.github/workflows/python_ci.yaml +++ b/.github/workflows/python_ci.yaml @@ -10,7 +10,7 @@ jobs: mypy: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 with: submodules: true - uses: ./.github/actions/python_prepare @@ -20,13 +20,26 @@ jobs: pytest: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 with: submodules: true - uses: ./.github/actions/python_prepare - name: Run pytest run: poetry run pytest tests + pytest-docker: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Extract environment variables for Docker container + # No variables required right now, add them here if needed. + run: | + echo "" >> .env + - name: Build Dockerimage + run: make build + - name: Run pytest in Docker + run: make tests-docker + test-build-image: runs-on: ubuntu-latest steps: @@ -40,7 +53,7 @@ jobs: black: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - uses: ./.github/actions/python_prepare - name: Check with black run: poetry run black --check . @@ -48,7 +61,7 @@ jobs: autoflake: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - uses: ./.github/actions/python_prepare - name: Check with autoflake run: | @@ -58,7 +71,7 @@ jobs: isort: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - uses: ./.github/actions/python_prepare - name: Check with isort run: | diff --git a/Dockerfile b/Dockerfile index af1dd17..4950f15 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,10 +13,16 @@ WORKDIR /app COPY pyproject.toml poetry.lock ./ -RUN --mount=type=cache,target=$POETRY_CACHE_DIR poetry install --no-root --only main +RUN --mount=type=cache,target=$POETRY_CACHE_DIR poetry install --no-root FROM --platform=linux/amd64 python:3.10.14-bookworm AS runtime +RUN apt-get update && apt-get install -y postgresql + +RUN useradd -m appuser + +USER appuser + ENV VIRTUAL_ENV=/app/.venv \ PATH="/app/.venv/bin:$PATH" @@ -24,7 +30,7 @@ WORKDIR /app COPY --from=builder ${VIRTUAL_ENV} ${VIRTUAL_ENV} -COPY labs_api ./labs_api +COPY . . ENV PYTHONPATH=/app diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..41310c1 --- /dev/null +++ b/Makefile @@ -0,0 +1,13 @@ +.PHONY: build push tests-docker + +IMAGE_NAME := ghcr.io/gnosis/labs-api:$(if $(GITHUB_SHA),$(GITHUB_SHA),test) + +build: + docker build . -t $(IMAGE_NAME) + +push: + # This should be done from Github's CD pipeline, but keeping it here for any testing purposes required in the future. + docker push $(IMAGE_NAME) + +tests-docker: build + docker run --env-file .env --rm $(IMAGE_NAME) pytest tests diff --git a/build_and_push.sh b/build_and_push.sh deleted file mode 100755 index e304db7..0000000 --- a/build_and_push.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -# This script builds the docker image and pushes it to the GitHub Container Registry, -# it should be done from Github's CD pipeline, but keeping it here for any testing purposes required in the future. -docker build . -t ghcr.io/gnosis/labs-api:test && docker push ghcr.io/gnosis/labs-api:test