From 193cee2a5a81df0f050567a7236f253ee0d21df5 Mon Sep 17 00:00:00 2001 From: Peter Jung Date: Thu, 12 Sep 2024 16:02:30 +0200 Subject: [PATCH] Run pytest in Docker (#8) --- .github/workflows/python_ci.yaml | 29 ++++++++++++++++++++++------- Dockerfile | 4 ++-- Makefile | 13 +++++++++++++ build_and_push.sh | 4 ---- 4 files changed, 37 insertions(+), 13 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 2c1bec5..fe85939 100644 --- a/.github/workflows/python_ci.yaml +++ b/.github/workflows/python_ci.yaml @@ -16,7 +16,7 @@ jobs: mypy: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - uses: ./.github/actions/python_prepare - name: Run mypy run: poetry run mypy @@ -24,15 +24,30 @@ jobs: pytest: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - uses: ./.github/actions/python_prepare - name: Run pytest run: poetry run pytest + pytest-docker: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Extract environment variables for Docker container + run: | + echo "MANIFOLD_API_KEY=${{ secrets.MANIFOLD_API_KEY }}" >> .env + echo "SERP_API_KEY=${{ secrets.SERP_API_KEY }}" >> .env + echo "OPENAI_API_KEY=${{ secrets.OPENAI_API_KEY }}" >> .env + echo "BET_FROM_PRIVATE_KEY=${{ secrets.BET_FROM_PRIVATE_KEY }}" >> .env + - name: Build Dockerimage + run: make build + - name: Run pytest in Docker + run: make tests-docker + pytest-paid: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - uses: ./.github/actions/python_prepare - name: Run pytest run: poetry run pytest @@ -45,7 +60,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 . @@ -53,7 +68,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: | @@ -63,7 +78,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: | @@ -73,7 +88,7 @@ jobs: build-image-test: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 # This step uses the `docker/build-push-action` action to build the image, based on your repository's `Dockerfile`. - name: Build and push Docker image uses: docker/build-push-action@4a13e500e55cf31b7a5d59a38ab2040ab0f42f56 diff --git a/Dockerfile b/Dockerfile index 557a957..f1569c5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,7 +13,7 @@ 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 @@ -24,7 +24,7 @@ WORKDIR /app COPY --from=builder ${VIRTUAL_ENV} ${VIRTUAL_ENV} -COPY mech_tool_deployment ./mech_tool_deployment +COPY . . ENV PYTHONPATH=/app ENV PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8dfbc33 --- /dev/null +++ b/Makefile @@ -0,0 +1,13 @@ +.PHONY: build push tests-docker + +IMAGE_NAME := ghcr.io/gnosis/mech-tool-deployment:$(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 cb5b4c6..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/mech-tool-deployment:test && docker push ghcr.io/gnosis/mech-tool-deployment:test