Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run pytests in Docker #11

Merged
merged 3 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading