From b6a44d772b905a60c523668a6219431cd77d858e Mon Sep 17 00:00:00 2001 From: Carlos Regis Date: Mon, 2 Oct 2023 18:48:36 +0000 Subject: [PATCH 1/2] ci: Add GitHub Actions ci workflow --- .github/workflows/ci.yaml | 105 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 .github/workflows/ci.yaml diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 00000000..45cedfa1 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,105 @@ +name: ci + +on: [push, pull_request, workflow_dispatch] + +env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + +jobs: + requirements: + # Ubunty latest @ 13-Sep-23: + # Ubuntu 22.04.3 LTS | Python 3.10.12 | Pipx 1.2.0 | No poetry | PostgreSQL 14.9 + runs-on: ubuntu-latest + steps: + - name: Check out repository + uses: actions/checkout@v4 + + - name: Set up python using the poetry config + id: setup-python + uses: actions/setup-python@v4 + with: + python-version-file: 'pyproject.toml' + + - name: Install Poetry + uses: snok/install-poetry@v1 + with: + virtualenvs-create: true + virtualenvs-in-project: true + installer-parallel: true + + - name: Load cached venv, if cache exists + id: cached-poetry-dependencies + uses: actions/cache@v3 + with: + path: .venv + key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }} + restore-keys: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}- + + - name: Install dependencies, if cache does not exist + if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true' + run: poetry install --no-root --no-interaction + + - name: Install root project, if required + run: poetry install --root --no-interaction + + - name: Run tests + run: | + source .venv/bin/activate + poetry -V + poetry check + + ruff: + runs-on: ubuntu-latest + needs: [requirements] + steps: + - uses: actions/checkout@v4 + - run: python -m pipx install poetry + - name: Set up Python version from Poetry config + uses: actions/setup-python@v4 + with: + python-version-file: 'pyproject.toml' + cache: 'poetry' # caching poetry dependencies + # Warning: poetry cache is not found + - run: poetry install --root --no-interaction --only dev + - name: Ruff linting + uses: chartboost/ruff-action@v1 + with: + args: --config=pyproject.toml + + black: + runs-on: ubuntu-latest + needs: [requirements] + steps: + - uses: actions/checkout@v4 + #- run: python -m pipx install poetry + - uses: actions/setup-python@v4 + with: + python-version-file: 'pyproject.toml' + cache: 'poetry' # caching poetry dependencies + # Warning: poetry cache is not found + - run: poetry install --root --no-interaction --only dev + - name: Black style formatting + uses: psf/black@stable + with: + options: --check --diff --color --config=pyproject.toml + # env: + # CHANGED_FILES: ${{ steps.file_changes.outputs.added_modified }} + + test: + runs-on: ubuntu-latest + needs: [ruff, black] + steps: + - uses: actions/checkout@v4 + - run: python -m pipx install poetry + - name: Set up Python version from Poetry config + uses: actions/setup-python@v4 + with: + python-version-file: 'pyproject.toml' + cache: 'poetry' # caching poetry dependencies + # Warning: poetry cache is not found + - run: poetry install --root --no-interaction --only test + - name: Unit testing + run: | + poetry -V + poetry run pytest + poetry run coverage report From 354eac036b5329d5ddc8f009328df240f7d7362a Mon Sep 17 00:00:00 2001 From: Carlos Regis Date: Thu, 5 Oct 2023 19:56:21 +0000 Subject: [PATCH 2/2] Add test job into circleci config --- .circleci/config.yml | 68 +++++++++++++++++++++++++++++++++------ .github/workflows/ci.yaml | 3 -- 2 files changed, 59 insertions(+), 12 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 7405afdd..ca517b28 100755 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -5,6 +5,12 @@ orbs: executors: app-executor: + parameters: + workspace-dir: + type: string + default: /tmp/workspace + environment: + WORKSPACE_DIR: << parameters.workspace-dir >> docker: - image: cimg/python:3.10 auth: @@ -87,24 +93,59 @@ jobs: - project - env + test: + executor: app-executor + parallelism: 2 + environment: + TEST_RESULTS: /tmp/test-results + steps: + - *fast-checkout + - setup_remote_docker: + docker_layer_caching: true + - attach_workspace: + at: $WORKSPACE_DIR + - run: + name: Unit tests + command: | + mkdir -p $TEST_RESULTS + cd $WORKSPACE_DIR/project/$APP_DIR + docker compose up -d --build + docker compose exec django python -V && poetry -V + docker compose exec django poetry install --with test,dev + + echo "*** Running tests with docker compose, poetry, and django unittest..." + docker compose exec django poetry run python manage.py test + + echo "*** Running tests with docker compose, poetry, and pytest..." + docker compose exec django poetry run pytest + + docker compose down + - store_artifacts: + path: $TEST_RESULTS + destination: raw-test-output + - store_test_results: + path: $TEST_RESULTS + build: executor: app-executor steps: - *fast-checkout - - setup_remote_docker + - setup_remote_docker: + docker_layer_caching: true - run: name: Build django image command: | set -ex - cd /tmp/workspace/project - source /tmp/workspace/env + cd $WORKSPACE_DIR/project + source $WORKSPACE_DIR/env docker build -t ldssa/django:$IMAGE_TAG . - mkdir -p /tmp/workspace/image - docker save -o /tmp/workspace/image/django.tar ldssa/django:$IMAGE_TAG + mkdir -p $WORKSPACE_DIR/image + docker save -o $WORKSPACE_DIR/image/django.tar ldssa/django:$IMAGE_TAG - persist_to_workspace: root: /tmp/workspace paths: - image/django.tar + push: executor: app-executor steps: @@ -114,9 +155,9 @@ jobs: - run: name: Push django image command: | - source /tmp/workspace/env - docker load -i /tmp/workspace/image/django.tar - docker login -u $DOCKER_USER -p $DOCKER_PASS + source $WORKSPACE_DIR/env + docker load -i $WORKSPACE_DIR/image/django.tar + echo "$DOCKER_PASS" | docker login -u $DOCKER_USER --password-stdin docker push ldssa/django:$IMAGE_TAG workflows: @@ -146,17 +187,26 @@ workflows: - persist-checkout: context: - Common Env + - build: context: - Common Env requires: - persist-checkout + + - test: + context: + - Common Env + requires: + - build + - approval: type: approval context: - Common Env requires: - - build + - test + - push: context: - Common Env diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 45cedfa1..4d6d99fe 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -2,9 +2,6 @@ name: ci on: [push, pull_request, workflow_dispatch] -env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - jobs: requirements: # Ubunty latest @ 13-Sep-23: