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

fear: Add environment #3

Merged
merged 24 commits into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e0bff73
add: Add `pyproject.toml` and `poetry.lock` for poetry environment
Colk-tech Oct 21, 2023
947aa2a
add: Add `Dockerfile` for Python
Colk-tech Oct 21, 2023
effa894
add: Add `Dockerfile` for PostgreSQL
Colk-tech Oct 21, 2023
15b7358
fix: section `[tool.flake8]` is not loaded
Colk-tech Oct 21, 2023
78a2ec4
add: add `setup.cfg` for flake8 settings
Colk-tech Oct 21, 2023
e1892cf
fix: remove `dev` packages in `production`
Colk-tech Oct 21, 2023
a0dae3f
remove: remove flake8 due to conflict with black
Colk-tech Oct 21, 2023
398b62b
add: add `.idea/` in `.gitignore`
Colk-tech Oct 21, 2023
0c81f74
add: add `.idea/` in `report.html`
Colk-tech Oct 21, 2023
4d34e69
add: add `.env` files
Colk-tech Oct 21, 2023
3e256b5
fix: fix `Dockerfile` for Python to create poetry virtual environment…
Colk-tech Oct 21, 2023
01116d4
add: add `entry.py`
Colk-tech Oct 21, 2023
429b291
add: add `compose.yml`
Colk-tech Oct 21, 2023
4ae86f0
add: add `Makefile` for utility
Colk-tech Oct 21, 2023
b64cb07
add: add CI jobs
Colk-tech Oct 21, 2023
9da988a
add: add `assets/` in `.gitignore`
Colk-tech Oct 21, 2023
0a28b16
fix: add missing `poetry.lock`
Colk-tech Oct 21, 2023
3b5e3fd
fix: fix invalid target in `Makefile`
Colk-tech Oct 21, 2023
71a9121
fix: fix invalid command in `\python.yml`
Colk-tech Oct 21, 2023
8d91fbb
add: add an example of test
Colk-tech Oct 21, 2023
dc229a2
fix: fix `entry.py` to use `sys.argv`
Colk-tech Oct 21, 2023
48f7e3c
add: add `codecov/codecov-action@v1` step in `pytest` job
Colk-tech Oct 21, 2023
49a2653
update: update `codecov/codecov-action` v1 -> v3
Colk-tech Oct 21, 2023
7e8b76d
add: add `./src/example.py`
Colk-tech Oct 21, 2023
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
14 changes: 14 additions & 0 deletions .env.ci
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# ===== Priority order of environment variables =====
# = 1. Shell environment variables =====
# = 2. Environment variables in .env file =====
# = 1. .env.example =====
# = 2. .env.local =====
# = ! In CI/CD, only .env.ci is used. =====
# = ! In test, only .env.test is used. =====
# ===================================================

# ===== Following environment variables are required in SHELL environment. ======
DOCSID_IS_CI=0
DOCSID_IS_TEST=0

# ===== Following environment variables are required in .env file. ======
14 changes: 14 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# ===== Priority order of environment variables =====
# = 1. Shell environment variables =====
# = 2. Environment variables in .env file =====
# = 1. .env.example =====
# = 2. .env.local =====
# = ! In CI/CD, only .env.ci is used. =====
# = ! In test, only .env.test is used. =====
# ===================================================

# ===== Following environment variables are required in SHELL environment. ======
DOCSID_IS_CI=0
DOCSID_IS_TEST=0

# ===== Following environment variables are required in .env file. ======
20 changes: 20 additions & 0 deletions .env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# ===== Priority order of environment variables =====
# = 1. Shell environment variables =====
# = 2. Environment variables in .env file =====
# = 1. .env.example =====
# = 2. .env.local =====
# = ! In CI/CD, only .env.ci is used. =====
# = ! In test, only .env.test is used. =====
# ===================================================

# ===== Following environment variables are required in SHELL environment. ======
DOCSID_IS_CI=0
DOCSID_IS_TEST=0

# ===== Following environment variables are required in .env file. ======
# Database
DOCSID_POSTGRES_HOST=postgres
DOCSID_POSTGRES_PORT=5432
DOCSID_POSTGRES_NAME=docsid
DOCSID_POSTGRES_USER=docsid_user
DOCSID_POSTGRES_PASSWORD=VKuiExj78jhSRjj/AX5y82Jtqd6p+tEh44pRUlBT63c=
14 changes: 14 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# ===== Priority order of environment variables =====
# = 1. Shell environment variables =====
# = 2. Environment variables in .env file =====
# = 1. .env.example =====
# = 2. .env.local =====
# = ! In CI/CD, only .env.ci is used. =====
# = ! In test, only .env.test is used. =====
# ===================================================

# ===== Following environment variables are required in SHELL environment. ======
DOCSID_IS_CI=0
DOCSID_IS_TEST=0

# ===== Following environment variables are required in .env file. ======
113 changes: 113 additions & 0 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
name: Python CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

env:
DOCKER_BUILDKIT: 1
COMPOSE_DOCKER_CLI_BUILD: 1
IMAGE_CACHE_DIR: /tmp/cache/docker-image
IMAGE_CACHE_KEY: cache-image

jobs:
build:
name: Build docker image
runs-on: ubuntu-latest

steps:
- name: Checkout repository
id: checkout
uses: actions/checkout@v2

- uses: satackey/action-docker-layer-caching@v0.0.11
continue-on-error: true

- name: Docker build
env:
DOCSID_IS_CI: "1"
run: make build

flake8:
name: Run flake8
runs-on: ubuntu-latest
needs: build

steps:
- name: Checkout repository
uses: actions/checkout@v2

- uses: satackey/action-docker-layer-caching@v0.0.11
continue-on-error: true

- name: Run flake8
env:
DOCSID_IS_CI: "1"
run: |
make flake8

mypy:
name: Run mypy
runs-on: ubuntu-latest
needs: build

steps:
- name: Checkout repository
uses: actions/checkout@v2

- uses: satackey/action-docker-layer-caching@v0.0.11
continue-on-error: true

- name: Run mypy
env:
DOCSID_IS_CI: "1"
run: |
make mypy

black:
name: Run black
runs-on: ubuntu-latest
needs: build

steps:
- name: Checkout repository
uses: actions/checkout@v2

- uses: satackey/action-docker-layer-caching@v0.0.11
continue-on-error: true

- name: Run black
env:
DOCSID_IS_CI: "1"
run: |
make black_check

pytest:
name: Run pytest
runs-on: ubuntu-latest
needs: build

steps:
- name: Checkout repository
uses: actions/checkout@v2

- uses: satackey/action-docker-layer-caching@v0.0.11
continue-on-error: true

- name: Run pytest
env:
DOCSID_IS_CI: "1"
run: |
make pytest_xml

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage.xml
env_vars: OS,PYTHON
name: codecov-umbrella
fail_ci_if_error: true
verbose: true
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,8 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.idea/

# Pytest report
assets/
report.html
85 changes: 85 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# CI
ifeq ($(DOCSID_IS_CI),"1")
ifneq (,$(wildcard ./.env.ci))
include .env.ci
export
else
$(error ".env.ci file is missing")
endif

# TEST
else ifeq ($(DOCSID_IS_TEST),"1")
ifneq (,$(wildcard ./.env.test))
include .env.test
export
else
$(error ".env.test file is missing")
endif

# LOCAL
else
ifneq (,$(wildcard ./.env.example))
include .env.example
export
else
$(error ".env.example file is missing")
endif

ifneq (,$(wildcard ./.env.local))
include .env.local
export
endif
endif

.PHONE: build
build:
docker compose build --build-arg BUILDKIT_INLINE_CACHE=1

.PHONY: up
up:
docker compose up -d --build
$(MAKE) logs

.PHONY: down
down:
docker compose down

.PHONY: restart
restart:
docker compose restart

.PHONY: destroy
destroy:
docker compose down --rmi all --volumes --remove-orphans

.PHONY: logs
logs:
docker compose logs -f

.PHONY: shell
shell:
docker compose run --rm api bash

.PHONY: flake8
flake8:
docker compose run --rm api bash -c "python entry.py flake8 ./"

.PHONY: mypy
mypy:
docker compose run --rm api bash -c "python entry.py mypy ./"

.PHONY: black
black:
docker compose run --rm api bash -c "python entry.py black ./"

.PHONY: black_check
black_check:
docker compose run --rm api bash -c "python entry.py black ./ --check"

.PHONY: pytest_html
pytest_html:
docker compose run --rm api bash -c "pytest -v tests/ --cov=./src/ --html=report.html"

.PHONY: pytest_xml
pytest_xml:
docker compose run --rm api bash -c "pytest -v tests/ --cov=./src/ --cov-report=xml"
49 changes: 49 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
services:
postgres:
build:
context: .
dockerfile: ./docker/postgres/Dockerfile
networks:
docsid_nw:
ports:
- "54321:5432"
environment:
- POSTGRES_USER=${DOCSID_POSTGRES_USER}
- POSTGRES_PASSWORD=${DOCSID_POSTGRES_PASSWORD}
volumes:
- ./docker/postgres/initdb:/docker-entrypoint-initdb.d
- docsid_postgres_data:/var/lib/postgresql/data
restart: unless-stopped
tty: true

api:
build:
context: .
dockerfile: ./docker/python/Dockerfile
target: development
volumes:
# Mount Volume to separate .venv from the host
- python_venv:/deploy/.venv
- ./:/deploy
working_dir: /deploy
command: >
bash -c "
python entry.py sleep infinity
"
restart: unless-stopped
tty: true

networks:
docsid_nw:
driver: bridge
driver_opts:
com.docker.network.enable_ipv6: "false"
ipam:
driver: default

volumes:
python_venv:
driver: local

docsid_postgres_data:
driver: local
7 changes: 7 additions & 0 deletions docker/postgres/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM postgres:14.5-bullseye

ENV LC_ALL=en_US.UTF-8 \
TZ=JST-9 \
TERM=xtermdocker-attachingdocker-attaching

COPY ./docker/postgres/initdb /docker-entrypoint-initdb.d
2 changes: 2 additions & 0 deletions docker/postgres/initdb/01.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CREATE DATABASE "docsid";
CREATE DATABASE "docsid-test";
Loading
Loading