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

[#270] CI: Add tests to ci pipeline #278

Closed
wants to merge 2 commits into from
Closed
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
68 changes: 59 additions & 9 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down
102 changes: 102 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
name: ci

on: [push, pull_request, workflow_dispatch]

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
Loading