Skip to content

Commit bc5fe49

Browse files
authored
merge: fear: Add environment (#3)
fear: Add environment
2 parents 93da23f + 7e8b76d commit bc5fe49

File tree

18 files changed

+1688
-1
lines changed

18 files changed

+1688
-1
lines changed

.env.ci

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# ===== Priority order of environment variables =====
2+
# = 1. Shell environment variables =====
3+
# = 2. Environment variables in .env file =====
4+
# = 1. .env.example =====
5+
# = 2. .env.local =====
6+
# = ! In CI/CD, only .env.ci is used. =====
7+
# = ! In test, only .env.test is used. =====
8+
# ===================================================
9+
10+
# ===== Following environment variables are required in SHELL environment. ======
11+
DOCSID_IS_CI=0
12+
DOCSID_IS_TEST=0
13+
14+
# ===== Following environment variables are required in .env file. ======

.env.example

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# ===== Priority order of environment variables =====
2+
# = 1. Shell environment variables =====
3+
# = 2. Environment variables in .env file =====
4+
# = 1. .env.example =====
5+
# = 2. .env.local =====
6+
# = ! In CI/CD, only .env.ci is used. =====
7+
# = ! In test, only .env.test is used. =====
8+
# ===================================================
9+
10+
# ===== Following environment variables are required in SHELL environment. ======
11+
DOCSID_IS_CI=0
12+
DOCSID_IS_TEST=0
13+
14+
# ===== Following environment variables are required in .env file. ======

.env.local

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# ===== Priority order of environment variables =====
2+
# = 1. Shell environment variables =====
3+
# = 2. Environment variables in .env file =====
4+
# = 1. .env.example =====
5+
# = 2. .env.local =====
6+
# = ! In CI/CD, only .env.ci is used. =====
7+
# = ! In test, only .env.test is used. =====
8+
# ===================================================
9+
10+
# ===== Following environment variables are required in SHELL environment. ======
11+
DOCSID_IS_CI=0
12+
DOCSID_IS_TEST=0
13+
14+
# ===== Following environment variables are required in .env file. ======
15+
# Database
16+
DOCSID_POSTGRES_HOST=postgres
17+
DOCSID_POSTGRES_PORT=5432
18+
DOCSID_POSTGRES_NAME=docsid
19+
DOCSID_POSTGRES_USER=docsid_user
20+
DOCSID_POSTGRES_PASSWORD=VKuiExj78jhSRjj/AX5y82Jtqd6p+tEh44pRUlBT63c=

.env.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# ===== Priority order of environment variables =====
2+
# = 1. Shell environment variables =====
3+
# = 2. Environment variables in .env file =====
4+
# = 1. .env.example =====
5+
# = 2. .env.local =====
6+
# = ! In CI/CD, only .env.ci is used. =====
7+
# = ! In test, only .env.test is used. =====
8+
# ===================================================
9+
10+
# ===== Following environment variables are required in SHELL environment. ======
11+
DOCSID_IS_CI=0
12+
DOCSID_IS_TEST=0
13+
14+
# ===== Following environment variables are required in .env file. ======

.github/workflows/python.yml

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
name: Python CI
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
env:
10+
DOCKER_BUILDKIT: 1
11+
COMPOSE_DOCKER_CLI_BUILD: 1
12+
IMAGE_CACHE_DIR: /tmp/cache/docker-image
13+
IMAGE_CACHE_KEY: cache-image
14+
15+
jobs:
16+
build:
17+
name: Build docker image
18+
runs-on: ubuntu-latest
19+
20+
steps:
21+
- name: Checkout repository
22+
id: checkout
23+
uses: actions/checkout@v2
24+
25+
- uses: satackey/action-docker-layer-caching@v0.0.11
26+
continue-on-error: true
27+
28+
- name: Docker build
29+
env:
30+
DOCSID_IS_CI: "1"
31+
run: make build
32+
33+
flake8:
34+
name: Run flake8
35+
runs-on: ubuntu-latest
36+
needs: build
37+
38+
steps:
39+
- name: Checkout repository
40+
uses: actions/checkout@v2
41+
42+
- uses: satackey/action-docker-layer-caching@v0.0.11
43+
continue-on-error: true
44+
45+
- name: Run flake8
46+
env:
47+
DOCSID_IS_CI: "1"
48+
run: |
49+
make flake8
50+
51+
mypy:
52+
name: Run mypy
53+
runs-on: ubuntu-latest
54+
needs: build
55+
56+
steps:
57+
- name: Checkout repository
58+
uses: actions/checkout@v2
59+
60+
- uses: satackey/action-docker-layer-caching@v0.0.11
61+
continue-on-error: true
62+
63+
- name: Run mypy
64+
env:
65+
DOCSID_IS_CI: "1"
66+
run: |
67+
make mypy
68+
69+
black:
70+
name: Run black
71+
runs-on: ubuntu-latest
72+
needs: build
73+
74+
steps:
75+
- name: Checkout repository
76+
uses: actions/checkout@v2
77+
78+
- uses: satackey/action-docker-layer-caching@v0.0.11
79+
continue-on-error: true
80+
81+
- name: Run black
82+
env:
83+
DOCSID_IS_CI: "1"
84+
run: |
85+
make black_check
86+
87+
pytest:
88+
name: Run pytest
89+
runs-on: ubuntu-latest
90+
needs: build
91+
92+
steps:
93+
- name: Checkout repository
94+
uses: actions/checkout@v2
95+
96+
- uses: satackey/action-docker-layer-caching@v0.0.11
97+
continue-on-error: true
98+
99+
- name: Run pytest
100+
env:
101+
DOCSID_IS_CI: "1"
102+
run: |
103+
make pytest_xml
104+
105+
- name: Upload coverage to Codecov
106+
uses: codecov/codecov-action@v3
107+
with:
108+
token: ${{ secrets.CODECOV_TOKEN }}
109+
file: ./coverage.xml
110+
env_vars: OS,PYTHON
111+
name: codecov-umbrella
112+
fail_ci_if_error: true
113+
verbose: true

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,8 @@ cython_debug/
157157
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
158158
# and can be added to the global gitignore or merged into this file. For a more nuclear
159159
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
160-
#.idea/
160+
.idea/
161+
162+
# Pytest report
163+
assets/
164+
report.html

Makefile

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# CI
2+
ifeq ($(DOCSID_IS_CI),"1")
3+
ifneq (,$(wildcard ./.env.ci))
4+
include .env.ci
5+
export
6+
else
7+
$(error ".env.ci file is missing")
8+
endif
9+
10+
# TEST
11+
else ifeq ($(DOCSID_IS_TEST),"1")
12+
ifneq (,$(wildcard ./.env.test))
13+
include .env.test
14+
export
15+
else
16+
$(error ".env.test file is missing")
17+
endif
18+
19+
# LOCAL
20+
else
21+
ifneq (,$(wildcard ./.env.example))
22+
include .env.example
23+
export
24+
else
25+
$(error ".env.example file is missing")
26+
endif
27+
28+
ifneq (,$(wildcard ./.env.local))
29+
include .env.local
30+
export
31+
endif
32+
endif
33+
34+
.PHONE: build
35+
build:
36+
docker compose build --build-arg BUILDKIT_INLINE_CACHE=1
37+
38+
.PHONY: up
39+
up:
40+
docker compose up -d --build
41+
$(MAKE) logs
42+
43+
.PHONY: down
44+
down:
45+
docker compose down
46+
47+
.PHONY: restart
48+
restart:
49+
docker compose restart
50+
51+
.PHONY: destroy
52+
destroy:
53+
docker compose down --rmi all --volumes --remove-orphans
54+
55+
.PHONY: logs
56+
logs:
57+
docker compose logs -f
58+
59+
.PHONY: shell
60+
shell:
61+
docker compose run --rm api bash
62+
63+
.PHONY: flake8
64+
flake8:
65+
docker compose run --rm api bash -c "python entry.py flake8 ./"
66+
67+
.PHONY: mypy
68+
mypy:
69+
docker compose run --rm api bash -c "python entry.py mypy ./"
70+
71+
.PHONY: black
72+
black:
73+
docker compose run --rm api bash -c "python entry.py black ./"
74+
75+
.PHONY: black_check
76+
black_check:
77+
docker compose run --rm api bash -c "python entry.py black ./ --check"
78+
79+
.PHONY: pytest_html
80+
pytest_html:
81+
docker compose run --rm api bash -c "pytest -v tests/ --cov=./src/ --html=report.html"
82+
83+
.PHONY: pytest_xml
84+
pytest_xml:
85+
docker compose run --rm api bash -c "pytest -v tests/ --cov=./src/ --cov-report=xml"

compose.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
services:
2+
postgres:
3+
build:
4+
context: .
5+
dockerfile: ./docker/postgres/Dockerfile
6+
networks:
7+
docsid_nw:
8+
ports:
9+
- "54321:5432"
10+
environment:
11+
- POSTGRES_USER=${DOCSID_POSTGRES_USER}
12+
- POSTGRES_PASSWORD=${DOCSID_POSTGRES_PASSWORD}
13+
volumes:
14+
- ./docker/postgres/initdb:/docker-entrypoint-initdb.d
15+
- docsid_postgres_data:/var/lib/postgresql/data
16+
restart: unless-stopped
17+
tty: true
18+
19+
api:
20+
build:
21+
context: .
22+
dockerfile: ./docker/python/Dockerfile
23+
target: development
24+
volumes:
25+
# Mount Volume to separate .venv from the host
26+
- python_venv:/deploy/.venv
27+
- ./:/deploy
28+
working_dir: /deploy
29+
command: >
30+
bash -c "
31+
python entry.py sleep infinity
32+
"
33+
restart: unless-stopped
34+
tty: true
35+
36+
networks:
37+
docsid_nw:
38+
driver: bridge
39+
driver_opts:
40+
com.docker.network.enable_ipv6: "false"
41+
ipam:
42+
driver: default
43+
44+
volumes:
45+
python_venv:
46+
driver: local
47+
48+
docsid_postgres_data:
49+
driver: local

docker/postgres/Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM postgres:14.5-bullseye
2+
3+
ENV LC_ALL=en_US.UTF-8 \
4+
TZ=JST-9 \
5+
TERM=xtermdocker-attachingdocker-attaching
6+
7+
COPY ./docker/postgres/initdb /docker-entrypoint-initdb.d

docker/postgres/initdb/01.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CREATE DATABASE "docsid";
2+
CREATE DATABASE "docsid-test";

0 commit comments

Comments
 (0)