diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 52639232..e30ef7a0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -108,11 +108,32 @@ jobs: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - - name: Unit tests + - name: Run Test suite run: | - backend_image=${{ vars.DOCKERHUB_ORGANIZATION }}/hope-support-images:payment-gateway-${{ github.sha }}-dev docker compose \ - -f ./ops/compose.ci-test.yml \ - up --exit-code-from backend + mkdir output + docker run --rm \ + --network host \ + -e PYTHONPATH=/app/src \ + -e DATABASE_URL=${DATABASE_URL} \ + -e CELERY_BROKER_URL=${CELERY_BROKER_URL} \ + -e CACHE_URL=${CACHE_URL} \ + -v "./output/:/app/output" \ + -v "./src/:/app/src" \ + -v "./tests:/app/tests" \ + -v "./pytest.ini:/app/pytest.ini" \ + -t ${{env.IMAGE}} \ + pytest tests/ --selenium -n auto -v --maxfail=5 --migrations --cov-report xml:./output/coverage.xml + + - name: Upload coverage to Codecov + uses: codecov/codecov-action@v4 + continue-on-error: true + with: + env_vars: OS,PYTHON + fail_ci_if_error: true + files: /app/output/coverage.xml + token: ${{ secrets.CODECOV_TOKEN }} + verbose: false + name: codecov-${{env.GITHUB_REF_NAME}} build_and_push_prd: needs: [build_and_push_dev] diff --git a/docker/Dockerfile b/docker/Dockerfile index 6e06dbc0..d9f0a1f8 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -43,7 +43,7 @@ RUN --mount=type=cache,target=/root/.uv-cache \ --python=/usr/local/bin/python \ --python-preference=system \ --frozen -RUN pip install uwsgi +RUN uv pip install . uwsgi WORKDIR /app COPY .. ./ @@ -69,11 +69,13 @@ ENV PATH=$PATH:/app/.venv/bin/ \ RUN pip install uv uwsgi RUN --mount=type=cache,target=/root/.uv-cache \ + --mount=type=bind,source=./src/hope_payment_gateway,target=/app/src/hope_payment_gateway \ + --mount=type=bind,source=./src/hope_api_auth,target=/app/src/hope_api_auth \ uv sync --cache-dir=/root/.uv-cache \ --python=/usr/local/bin/python \ --python-preference=system \ - --frozen -RUN pip install uwsgi + --frozen \ + uv pip install . COPY --chown=hpg:hpg .. ./ COPY --chown=hpg:hpg --from=builder /app /app diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index ab744faf..c39689fc 100755 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -1,44 +1,45 @@ #!/bin/bash -set -eou pipefail -production() { - uwsgi \ - --http :8000 \ - --master \ - --module=hope_payment_gateway.config.wsgi \ - --processes=2 \ - --buffer-size=8192 -} - -if [ $# -eq 0 ]; then - production -fi +export MEDIA_ROOT="${MEDIA_ROOT:-/var/run/app/media}" +export STATIC_ROOT="${STATIC_ROOT:-/var/run/app/static}" +export UWSGI_PROCESSES="${UWSGI_PROCESSES:-"4"}" +export DJANGO_SETTINGS_MODULE="${DJANGO_SETTINGS_MODULE:-hope_payment_gateway.config.settings}" case "$1" in - dev) - ./docker/wait-for-it.sh db:5432 - django-admin upgrade - django-admin runserver 0.0.0.0:8000 - ;; - tests) - ./docker/wait-for-it.sh db:5432 - pytest --create-db - ;; - prd) - production - ;; - celery_worker) - export C_FORCE_ROOT=1 - celery -A hope_payment_gateway.celery worker -l info - ;; - celery_beat) - celery -A hope_payment_gateway.celery beat -l info - ;; - celery_flower) - celery flower -A hope_payment_gateway.celery --address=0.0.0.0 --broker=$CELERY_BROKER_URL - ;; - *) - exec "$@" - ;; -esac \ No newline at end of file + run) + django-admin upgrade --with-check + set -- tini -- "$@" + MAPPING="" + if [ "${STATIC_URL}" = "/static/" ]; then + MAPPING="--static-map ${STATIC_URL}=${STATIC_ROOT}" + fi + set -- tini -- "$@" + set -- uwsgi --http :8000 \ + --module hope_payment_gateway.config.wsgi \ + --mimefile=/conf/mime.types \ + --uid hope \ + --gid unicef \ + --buffer-size 8192 \ + --http-buffer-size 8192 \ + $MAPPING + ;; + upgrade) + django-admin upgrade --with-check + ;; + worker) + set -- tini -- "$@" + set -- gosu user:app celery -A hope_payment_gateway.config.celery worker -E --loglevel=ERROR --concurrency=4 + ;; + beat) + set -- tini -- "$@" + set -- gosu user:app celery -A hope_payment_gateway.config.celery beat --loglevel=ERROR --scheduler django_celery_beat.schedulers:DatabaseScheduler + ;; + flower) + export DATABASE_URL="sqlite://:memory:" + set -- tini -- "$@" + set -- gosu user:app celery -A hope_payment_gateway.config.celery flower + ;; +esac + +exec "$@" diff --git a/ops/compose.ci-test.yml b/ops/compose.ci-test.yml index 26335652..15b739cf 100644 --- a/ops/compose.ci-test.yml +++ b/ops/compose.ci-test.yml @@ -3,7 +3,7 @@ version: '3.8' services: backend: image: ${backend_image} - command: tests + command: pytest tests/ -n auto -v --maxfail=5 --migrations --cov-report xml:./output/coverage.xml environment: - SECRET_KEY=secret-key - DATABASE_URL=postgres://postgres:postgres@db:5432/postgres