Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
yakimka committed Sep 3, 2023
0 parents commit 1c90d1b
Show file tree
Hide file tree
Showing 29 changed files with 1,655 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
**/.aws
**/.cache
**/.DS_Store
**/.env
**/.git
**/.gitignore
**/.idea
**/.mypy_cache
**/.pytest_cache
**/.ssh
**/.venv
**/.vscode
**/__pycache__
**/coverage
**/env
**/venv
.github
.gitlab*
docker-compose.yml
Dockerfile
README.md
9 changes: 9 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Security Warning! Do not commit this file to any VCS!
# This is a local file to speed up development process,
# so you don't have to change your environment variables.
#
# This is not applied to `.env.template`!
# Template files must be committed to the VCS, but must not contain
# any secret values.

ENVIRONMENT=development
15 changes: 15 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: 2
updates:
- package-ecosystem: pip
directory: "/"
schedule:
interval: daily
time: "02:00"
open-pull-requests-limit: 10

- package-ecosystem: github-actions
directory: "/"
schedule:
interval: daily
time: "02:00"
open-pull-requests-limit: 10
200 changes: 200 additions & 0 deletions .github/workflows/workflow-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
name: CI\CD

on:
push:
branches:
- main
tags:
- '*'
pull_request:
workflow_dispatch:
# Update docker hub retention policy
schedule:
- cron: "21 7 8 * *"

env:
DOCKER_USERNAME: "yakim"
PROJECT_NAME: "DiffLume"
DOCKER_COMPOSE_SERVICE_NAME: "app"
MAIN_PY_VERSION: "3.11"
PIP_NO_CACHE_DIR: "off"
POETRY_VIRTUALENVS_IN_PROJECT: "true"
POETRY_NO_INTERACTION: "1"
REGISTRY: ""
DOCKER_BUILDKIT: "1"
COMPOSE_DOCKER_CLI_BUILD: "1"

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

permissions: read-all

jobs:
check-code:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.10', '3.11']

steps:
- uses: actions/checkout@v3

- run: echo "IMAGE_FULL_NAME=$(echo ${DOCKER_USERNAME}/${PROJECT_NAME})" >> $GITHUB_ENV
- run: echo "DEV_VERSION=`(cat Dockerfile-dev; cat .github/workflows/workflow-ci.yml)|sha1sum |cut -c 1-8`" >> $GITHUB_ENV
- run: echo "DEV_IMAGE=${IMAGE_FULL_NAME}:dev-${{ matrix.python-version }}-${DEV_VERSION}" >> $GITHUB_ENV
- run: echo "VERSION=$(echo ${GITHUB_REF:10})" >> $GITHUB_ENV
- run: echo "SHORT_VERSION=$(echo ${VERSION%.*})" >> $GITHUB_ENV

- name: Prepare Docker
env:
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
run: |
cp .env.template .env
docker login "$REGISTRY" -u "$DOCKER_USERNAME" --password="${DOCKERHUB_TOKEN}"
docker buildx create --use --driver=docker-container
docker --version && docker compose --version
- name: Load cached venv and cache
id: cached-venv-and-cache
uses: actions/cache@v3
with:
path: |
.venv
.cache
key: py${{ matrix.python-version }}-${{ hashFiles('./poetry.lock') }}

- name: Build docker dev image
run: |
docker pull ${DEV_IMAGE} || (
PYTHON_VERSION=${{ matrix.python-version }} docker compose build ${DOCKER_COMPOSE_SERVICE_NAME} ;
docker tag ${PROJECT_NAME}:dev ${DEV_IMAGE} ;
docker push ${DEV_IMAGE}
)
docker tag ${DEV_IMAGE} ${PROJECT_NAME}:dev
- name: Run checks
run: docker compose run -e CI=1 --user=$(id -u) --rm devtools ./ci.sh

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
file: ./coverage.xml
# token: ${{ secrets.CODECOV_TOKEN }} # not required for public repos

- uses: actions/upload-artifact@v3
with:
name: built-package-py${{ matrix.python-version }}
path: dist/

release-package:
runs-on: ubuntu-latest
needs: [ check-code ]

steps:
- uses: actions/checkout@v3
- uses: actions/download-artifact@v3
with:
name: built-package-py${{ env.MAIN_PY_VERSION }}
path: dist/

- run: echo "IMAGE_FULL_NAME=$(echo ${DOCKER_USERNAME}/${PROJECT_NAME})" >> $GITHUB_ENV
- run: echo "DEV_VERSION=`(cat Dockerfile-dev; cat .github/workflows/workflow-ci.yml)|sha1sum |cut -c 1-8`" >> $GITHUB_ENV
- run: echo "DEV_IMAGE=${IMAGE_FULL_NAME}:dev-${MAIN_PY_VERSION}-${DEV_VERSION}" >> $GITHUB_ENV

- name: Prepare Docker
env:
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
run: |
cp .env.template .env
docker login "$REGISTRY" -u "$DOCKER_USERNAME" --password="${DOCKERHUB_TOKEN}"
docker buildx create --use --driver=docker-container
docker --version && docker compose --version
- name: Pull and spin dev container
run: |
docker pull ${DEV_IMAGE}
docker tag ${DEV_IMAGE} ${PROJECT_NAME}:dev
docker compose run --user=$(id -u) --rm -d devtools sleep infinity
- run: echo "PROJECT_VERSION=$(docker compose exec devtools poetry version --short)" >> $GITHUB_ENV

- name: Login to PyPI
env:
PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
run: |
echo "Login"
docker compose exec devtools poetry config pypi-token.pypi $PYPI_TOKEN || true
- name: Check if tag version matches project version
if: startsWith(github.ref, 'refs/tags/')
run: |
TAG=${GITHUB_REF:10}
echo $TAG
echo $PROJECT_VERSION
if [[ "$TAG" != "$PROJECT_VERSION" ]]; then exit 1; fi
- name: Build and publish (dry-run)
if: github.actor != 'dependabot[bot]'
run: docker compose exec devtools poetry publish --dry-run

- name: Build and publish
if: startsWith(github.ref, 'refs/tags/')
run: docker compose exec devtools poetry publish

release-image:
runs-on: ubuntu-latest
needs: [ check-code ]

steps:
- uses: actions/checkout@v3
- uses: actions/download-artifact@v3
with:
name: built-package-py${{ env.MAIN_PY_VERSION }}
path: dist/

- run: echo "IMAGE_FULL_NAME=$(echo ${DOCKER_USERNAME}/${PROJECT_NAME})" >> $GITHUB_ENV
- run: echo "DEV_VERSION=`(cat Dockerfile-dev; cat .github/workflows/workflow-ci.yml)|sha1sum |cut -c 1-8`" >> $GITHUB_ENV
- run: echo "DEV_IMAGE=${IMAGE_FULL_NAME}:dev-${MAIN_PY_VERSION}-${DEV_VERSION}" >> $GITHUB_ENV
- run: echo "VERSION=$(echo ${GITHUB_REF:10})" >> $GITHUB_ENV
- run: echo "SHORT_VERSION=$(echo ${VERSION%.*})" >> $GITHUB_ENV

- name: Prepare Docker
env:
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
run: |
cp .env.template .env
docker login "$REGISTRY" -u "$DOCKER_USERNAME" --password="${DOCKERHUB_TOKEN}"
docker buildx create --use --driver=docker-container
docker --version && docker compose --version
- name: Pull and spin dev container
run: |
docker pull ${DEV_IMAGE}
docker tag ${DEV_IMAGE} ${PROJECT_NAME}:dev
docker compose run --user=$(id -u) --rm -d devtools sleep infinity
- run: echo "PROJECT_VERSION=$(docker compose exec devtools poetry version --short)" >> $GITHUB_ENV

# https://docs.docker.com/build/cache/backends/gha/
- name: Expose GitHub Runtime
uses: crazy-max/ghaction-github-runtime@v2

- name: Build image
run: >
docker buildx build --load
--build-arg WHEEL=${PROJECT_NAME}-${PROJECT_VERSION}-py3-none-any.whl
-t ${IMAGE_FULL_NAME}:latest . --cache-to type=gha,mode=max --cache-from type=gha
- name: Add version from git tag
if: startsWith(github.ref, 'refs/tags/')
run: |
docker tag "${IMAGE_FULL_NAME}:latest" "${IMAGE_FULL_NAME}:${VERSION}"
docker tag "${IMAGE_FULL_NAME}:latest" "${IMAGE_FULL_NAME}:${SHORT_VERSION}"
- name: Push production images
if: startsWith(github.ref, 'refs/tags/') || github.ref == 'refs/heads/main'
run: |
echo "Showing debug information"
docker image ls
docker push "${IMAGE_FULL_NAME}" --all-tags
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.venv
docs/_build
*.sqlite3
*.db
docker-compose.override.yml
48 changes: 48 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-added-large-files
- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.25.0
hooks:
- id: check-github-workflows
- id: check-dependabot
- repo: https://github.com/psf/black
rev: 23.7.0
hooks:
- id: black
args: [--preview]
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort
- repo: https://github.com/PyCQA/flake8
rev: 6.1.0
hooks:
- id: flake8
additional_dependencies: [
# "flake8-assertive~=2.1.0"
# "flake8-future-annotations~=1.1.0",
"dlint~=0.14.1",
"flake8-async~=22.11.14",
"flake8-bandit~=4.1.1",
"flake8-bugbear~=23.7.10",
"flake8-comprehensions~=3.14.0",
"flake8-eradicate~=1.5.0",
"flake8-mock==0.4",
"flake8-noqa~=1.3.2",
"flake8-pie~=0.16.0",
"flake8-pytest-style~=1.7.2",
"flake8-self~=0.2.2",
"flake8-simplify~=0.20.0",
"flake8-type-checking~=2.4.1",
"flake8-typing-imports~=1.14.0",
"flake8-unused-arguments~=0.0.13",
"flake8-warnings~=0.4.0",
"pep8-naming~=0.13.3",
]
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Version history

We follow [Semantic Versions](https://semver.org/).


## Version 0.1.0

- Initial release
63 changes: 63 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# How to contribute


## Dependencies

We use [poetry](https://github.com/python-poetry/poetry) to manage the dependencies.

To install them you would need to run `install` command:

```bash
poetry install
```

To activate your `virtualenv` run `poetry shell`.


## One magic command

Run `make checks` to run everything we have!


## Tests

We use `pytest` and `flake8` for quality control.

To run all tests:

```bash
make test
```

To run linting:

```bash
make lint
```
Keep in mind: default virtual environment folder excluded by flake8 style checking is `.venv`.
If you want to customize this parameter, you should do this in `pyproject.toml`.


## Type checks

We use `mypy` to run type checks on our code.
To use it:

```bash
make mypy
```


## Submitting your code

What the point of this method?

1. We use protected `main`/`master` branch,
so the only way to push your code is via pull request
2. We use issue branches: to implement a new feature or to fix a bug
create a new branch
3. Then create a pull request to `main`/`master` branch
4. We use `git tag`s to make releases, so we can track what has changed
since the latest release

In this method, the latest version of the app is always in the `main`/`master` branch.
Loading

0 comments on commit 1c90d1b

Please sign in to comment.