From 2aa76014e18d08c8e467af3a0ecf1fe997aa1fc3 Mon Sep 17 00:00:00 2001 From: Steven Bal Date: Mon, 29 Jul 2024 14:18:51 +0200 Subject: [PATCH] :construction_worker: Get tests up and running with docker --- .github/workflows/ci.yml | 136 +++++++++++++++++++++++++++++++-------- Dockerfile | 22 +++++++ 2 files changed, 130 insertions(+), 28 deletions(-) create mode 100644 Dockerfile diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7df6a70..5f81d29 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,58 +10,138 @@ on: pull_request: workflow_dispatch: +env: + IMAGE_NAME: openformulieren/open-forms-ext-stuf-zds-payments + jobs: tests: + name: Run the Django test suite runs-on: ubuntu-latest - strategy: - matrix: - python: ['3.10', '3.11', '3.12'] - django: ['4.2'] - - name: Run the test suite (Python ${{ matrix.python }}, Django ${{ matrix.django }}) - env: - PIP_CONSTRAINT: pip-constraints.txt + services: + postgres: + image: postgres:14 + env: + POSTGRES_HOST_AUTH_METHOD: trust + ports: + - 5432:5432 + # Needed because the postgres container does not provide a healthcheck + options: + --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 + --name postgres + redis: + image: redis:6 + ports: + - 6379:6379 + steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v5 + - name: Checkout Open Forms + uses: actions/checkout@v3 + with: + repository: open-formulieren/open-forms + path: open-forms + + - name: Checkout StUF-ZDS payments extension + uses: actions/checkout@v3 + with: + path: extension + + - name: Set up backend environment + uses: maykinmedia/setup-django-backend@v1.1 with: - python-version: ${{ matrix.python }} + apt-packages: 'libxml2-dev libxmlsec1-dev libxmlsec1-openssl gettext postgresql-client gdal-bin' + python-version: '3.10' + optimize-postgres: 'yes' + pg-service: 'postgres' + setup-node: 'yes' + nvmrc-custom-dir: 'open-forms' + npm-ci-flags: '--legacy-peer-deps' + working-directory: ${{ github.workspace }}/open-forms - - name: Install dependencies - run: pip install tox tox-gh-actions + - name: Make symlink in OF to the extension + run: | + ln -s ${{ github.workspace }}/extension/stuf_zds_payments ${{ github.workspace }}/open-forms/src - name: Run tests - run: tox + run: | + export OPEN_FORMS_EXTENSIONS=stuf_zds_payments + + python src/manage.py compilemessages + coverage run --source=stuf_zds_payments src/manage.py test stuf_zds_payments + coverage combine + coverage xml -o coverage-extension.xml env: - PYTHON_VERSION: ${{ matrix.python }} - DJANGO: ${{ matrix.django }} + DJANGO_SETTINGS_MODULE: openforms.conf.ci + SECRET_KEY: dummy + DB_USER: postgres + DB_PASSWORD: '' + working-directory: ${{ github.workspace }}/open-forms - name: Publish coverage report - uses: codecov/codecov-action@v4 + uses: codecov/codecov-action@v3.1.4 + with: + root_dir: ${{ github.workspace }}/extension + working-directory: ${{ github.workspace }}/open-forms + files: ./coverage-extension.xml + + docker_build: + name: Build Docker image + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Set tag + id: vars + run: | + # Strip git ref prefix from version + VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') + + # Strip "v" prefix from tag name (if present at all) + [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//') + + # Use Docker `latest` tag convention + [ "$VERSION" == "main" ] && VERSION=latest + + # PRs result in version 'merge' -> transform that into 'latest' + [ "$VERSION" == "merge" ] && VERSION=latest + + echo ::set-output name=tag::${VERSION} + + - name: Build the Docker image + run: | + docker build . \ + --tag $IMAGE_NAME:$RELEASE_VERSION + env: + RELEASE_VERSION: ${{ steps.vars.outputs.tag }} + + - run: docker image save -o image.tar $IMAGE_NAME:${{ steps.vars.outputs.tag }} + - name: Store image artifact + uses: actions/upload-artifact@v3 with: - token: ${{ secrets.CODECOV_TOKEN }} + name: docker-image + path: image.tar + retention-days: 1 publish: name: Publish package to PyPI runs-on: ubuntu-latest - needs: tests - environment: release - permissions: - id-token: write + needs: + - tests + - docker_build if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags') steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v5 + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 with: python-version: '3.10' - name: Build sdist and wheel run: | - pip install build --upgrade - python -m build - + pip install pip setuptools wheel --upgrade + python setup.py sdist bdist_wheel - name: Publish a Python distribution to PyPI uses: pypa/gh-action-pypi-publish@release/v1 - + with: + user: __token__ + password: ${{ secrets.PYPI_TOKEN }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..5c38365 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,22 @@ +# Stage 1 - Build stuf_zds_payments environment +FROM python:3.12-slim-bullseye AS stuf-zds-payments-build + +WORKDIR /app + +RUN pip install pip -U +COPY . /app +RUN pip install . + + +# Stage 2 - Build the production image with the stuf_zds_payments +FROM openformulieren/open-forms:latest AS production-build + +WORKDIR /app + +# Copy the dependencies of the stuf_zds_payments +COPY --from=stuf-zds-payments-build /usr/local/lib/python3.12 /usr/local/lib/python3.12 + +# Add stuf_zds_payments code to the image +COPY --chown=maykin:root ./stuf_zds_payments /app/src/stuf_zds_payments + +USER maykin \ No newline at end of file