Skip to content

Commit

Permalink
Run pytests in Docker (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
kongzii authored Sep 12, 2024
1 parent 9b2eaa7 commit d42316a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
23 changes: 18 additions & 5 deletions .github/workflows/python_ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -40,15 +53,15 @@ 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 .

autoflake:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: ./.github/actions/python_prepare
- name: Check with autoflake
run: |
Expand All @@ -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: |
Expand Down
10 changes: 8 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,24 @@ 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"

WORKDIR /app

COPY --from=builder ${VIRTUAL_ENV} ${VIRTUAL_ENV}

COPY labs_api ./labs_api
COPY . .

ENV PYTHONPATH=/app

Expand Down
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
4 changes: 0 additions & 4 deletions build_and_push.sh

This file was deleted.

0 comments on commit d42316a

Please sign in to comment.