Skip to content

Commit bcd5669

Browse files
authored
Merge pull request #7 from fastapi-mvc/refactor_to_nix_flake
Refactor to nix flake
2 parents 4d2b9ef + 7ff6e8b commit bcd5669

13 files changed

+342
-252
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ jobs:
4545
- name: Install Nix ❄️
4646
uses: cachix/install-nix-action@v18
4747
with:
48-
nix_path: nixpkgs=channel:nixos-22.05
48+
nix_path: nixpkgs=channel:nixos-22.11
4949
- name: Setup Cachix ❄️
5050
uses: cachix/cachix-action@v12
5151
with:
@@ -64,7 +64,7 @@ jobs:
6464
- name: Generate poetry.lock
6565
if: steps.lock-cache.outputs.cache-hit != 'true'
6666
working-directory: ./example
67-
run: nix-shell -p poetry --run "poetry lock --no-update"
67+
run: NIXPKGS_ALLOW_INSECURE=1 nix-shell -p poetry --run "poetry lock --no-update"
6868
- name: Set short commit SHA
6969
run: echo "SHORT_SHA=${GITHUB_SHA::7}" >> $GITHUB_ENV
7070
- name: Push generated test project

template/Makefile.jinja

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,73 @@ endif
1010
SHELL=/usr/bin/env bash -o pipefail -o errexit
1111

1212
TAG ?= $(shell cat TAG)
13-
{%- if nix %}
14-
USE_NIX ?= $(shell command -v nix || echo "False")
15-
{% endif %}
13+
POETRY_HOME ?= ${HOME}/.local/share/pypoetry
14+
POETRY_BINARY ?= ${POETRY_HOME}/venv/bin/poetry
15+
POETRY_VERSION ?= 1.2.0
16+
1617
help: ## Display this help
1718
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z0-9_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
1819

1920
.PHONY: show-version
2021
show-version: ## Display version
2122
echo -n "${TAG}"
22-
{% if nix %}
23-
ifeq ($(USE_NIX), False)
24-
include Poetry.mk
25-
else
26-
include Nix.mk
27-
endif
28-
{% else %}
29-
include Poetry.mk
23+
24+
.PHONY: build
25+
build: ## Build {{project_name}} package
26+
echo "[build] Build {{project_name}} package."
27+
${POETRY_BINARY} build
28+
29+
.PHONY: install
30+
install: ## Install {{project_name}} with poetry
31+
@build/install.sh
32+
33+
.PHONY: image
34+
image: ## Build {{container_image_name}} image
35+
@build/image.sh
36+
37+
.PHONY: metrics
38+
metrics: install ## Run {{project_name}} metrics checks
39+
echo "[metrics] Run {{project_name}} PEP 8 checks."
40+
${POETRY_BINARY} run flake8 --select=E,W,I --max-line-length 80 --import-order-style pep8 --statistics --count {{package_name}}
41+
echo "[metrics] Run {{project_name}} PEP 257 checks."
42+
${POETRY_BINARY} run flake8 --select=D --ignore D301 --statistics --count {{package_name}}
43+
echo "[metrics] Run {{project_name}} pyflakes checks."
44+
${POETRY_BINARY} run flake8 --select=F --statistics --count {{package_name}}
45+
echo "[metrics] Run {{project_name}} code complexity checks."
46+
${POETRY_BINARY} run flake8 --select=C901 --statistics --count {{package_name}}
47+
echo "[metrics] Run {{project_name}} open TODO checks."
48+
${POETRY_BINARY} run flake8 --select=T --statistics --count {{package_name}} tests
49+
echo "[metrics] Run {{project_name}} black checks."
50+
${POETRY_BINARY} run black -l 80 --check {{package_name}}
51+
52+
.PHONY: unit-test
53+
unit-test: install ## Run {{project_name}} unit tests
54+
echo "[unit-test] Run {{project_name}} unit tests."
55+
${POETRY_BINARY} run pytest tests/unit
56+
57+
.PHONY: integration-test
58+
integration-test: install ## Run {{project_name}} integration tests
59+
echo "[unit-test] Run {{project_name}} integration tests."
60+
${POETRY_BINARY} run pytest tests/integration
61+
62+
.PHONY: coverage
63+
coverage: install ## Run {{project_name}} tests coverage
64+
echo "[coverage] Run {{project_name}} tests coverage."
65+
${POETRY_BINARY} run pytest --cov-config=.coveragerc --cov={{package_name}} --cov-fail-under=90 --cov-report=xml --cov-report=term-missing tests
66+
67+
.PHONY: test
68+
test: unit-test integration-test ## Run {{project_name}} tests
69+
70+
.PHONY: docs
71+
docs: install ## Build {{project_name}} documentation
72+
echo "[docs] Build {{project_name}} documentation."
73+
${POETRY_BINARY} run sphinx-build docs site
74+
{% if helm %}
75+
.PHONY: dev-env
76+
dev-env: image ## Start a local Kubernetes cluster using minikube and deploy application
77+
@build/dev-env.sh
78+
79+
.PHONY: clean
80+
clean: ## Remove .cache directory and cached minikube
81+
minikube delete && rm -rf ~/.cache ~/.minikube
3082
{% endif %}

template/Poetry.mk.jinja

Lines changed: 0 additions & 63 deletions
This file was deleted.

template/{% if github_actions %}.github{% endif %}/workflows/main.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ name: CI
22

33
on:
44
push:
5+
branches:
6+
- master
57
pull_request:
8+
branches:
9+
- master
610

711
env:
812
POETRY_HOME: /opt/poetry
@@ -54,7 +58,7 @@ jobs:
5458

5559
steps:
5660
- uses: actions/checkout@v3
57-
- name: Set up Python
61+
- name: Set up Python 3.9
5862
uses: actions/setup-python@v4
5963
with:
6064
python-version: 3.9
@@ -74,22 +78,18 @@ jobs:
7478
metrics:
7579
needs: install
7680
runs-on: ubuntu-latest
77-
strategy:
78-
fail-fast: false
79-
matrix:
80-
python-version: [ '3.8', '3.9', '3.10' ]
8181

8282
steps:
8383
- uses: actions/checkout@v3
84-
- name: Set up Python ${{ matrix.python-version }}
84+
- name: Set up Python 3.9
8585
uses: actions/setup-python@v4
8686
with:
87-
python-version: ${{ matrix.python-version }}
87+
python-version: 3.9
8888
- name: Load Poetry cache
8989
uses: actions/cache@v3
9090
with:
9191
path: ${{ env.POETRY_HOME }}
92-
key: ${{ runner.os }}-${{ matrix.python-version }}-${{ hashFiles('./pyproject.toml') }}-{{ hashFiles('./poetry.lock') }}
92+
key: ${{ runner.os }}-3.9-${{ hashFiles('./pyproject.toml') }}-{{ hashFiles('./poetry.lock') }}
9393
- name: Run metrics checks
9494
run: make metrics
9595
unit-tests:
@@ -140,7 +140,7 @@ jobs:
140140

141141
steps:
142142
- uses: actions/checkout@v3
143-
- name: Set up Python ${{ matrix.python-version }}
143+
- name: Set up Python 3.9
144144
uses: actions/setup-python@v4
145145
with:
146146
python-version: 3.9

template/{% if github_actions %}.github{% endif %}/workflows/{% if helm %}integration.yml{% endif %}.jinja

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
{% raw %}name: K8s integration
22

33
on:
4-
push:
5-
branches:
6-
- master
7-
pull_request:
8-
branches:
9-
- master
104
workflow_dispatch:
115

126
jobs:

template/{% if github_actions %}.github{% endif %}/workflows/{% if nix %}nix.yml{% endif %}.jinja

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ on:
1010
- master
1111

1212
env:
13-
NIX_CHANNEL: nixpkgs=channel:nixos-22.05
13+
NIX_CHANNEL: nixpkgs=channel:nixos-22.11
1414

1515
jobs:
1616
# This job checks if an identical workflow is being triggered by different
@@ -27,7 +27,7 @@ jobs:
2727
skip_after_successful_duplicate: 'true'
2828
concurrent_skipping: same_content
2929
do_not_skip: '["pull_request", "workflow_dispatch", "schedule"]'
30-
nixpkgs-fmt:
30+
nix-checks:
3131
needs: pre_job
3232
if: ${{ needs.pre_job.outputs.should_skip != 'true' }}
3333
runs-on: ubuntu-latest
@@ -39,9 +39,19 @@ jobs:
3939
- uses: actions/checkout@v3
4040
- name: Check format
4141
run: nix-shell -p nixpkgs-fmt --run 'nixpkgs-fmt --check .'
42+
- name: Run nix flake check
43+
run: nix flake check
44+
- name: Run metrics checks
45+
run: nix run .#metrics
46+
- name: Run tests
47+
run: nix run .#test
4248
nix-build:
4349
needs: pre_job
4450
if: ${{ needs.pre_job.outputs.should_skip != 'true' }}
51+
strategy:
52+
fail-fast: false
53+
matrix:
54+
python-version: [ '38', '39', '310' ]
4555
runs-on: ubuntu-latest
4656

4757
steps:
@@ -59,43 +69,6 @@ jobs:
5969
with:
6070
name: fastapi-mvc
6171
authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
62-
- name: Install make
63-
run: nix-env -i gnumake -f '<nixpkgs>'
64-
- name: Build package
65-
run: make build
66-
- name: Build container image
67-
run: make image
68-
nix-ci:
69-
needs: pre_job
70-
if: ${{ needs.pre_job.outputs.should_skip != 'true' }}
71-
runs-on: ubuntu-latest
72-
strategy:
73-
fail-fast: false
74-
matrix:
75-
python-version: [ 'python38', 'python39', 'python310' ]
76-
env:
77-
PYTHON_NIXPKG: ${{ matrix.python-version }}
78-
79-
steps:
80-
- uses: actions/checkout@v3
81-
- name: Install Nix
82-
uses: cachix/install-nix-action@v17
83-
with:
84-
nix_path: ${{ env.NIX_CHANNEL }}
85-
# Remove bellow step if you do not want to use Cachix - Nix binary cache.
86-
# For OpenSource projects there is free 5GB of storage.
87-
# https://www.cachix.org
88-
- name: Setup Cachix Nix cache
89-
uses: cachix/cachix-action@v10
90-
with:
91-
name: fastapi-mvc
92-
authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
93-
- name: Install make
94-
run: nix-env -i gnumake -f '<nixpkgs>'
95-
- name: Run metrics checks
96-
run: make metrics
97-
- name: Run test
98-
run: make test
99-
- name: Run coverage
100-
run: make coverage
72+
- name: {% endraw %}Build {{project_name}}
73+
run: nix build .#{{project_name}}-py{% raw %}${{ matrix.python-version }}
10174
{%- endraw %}

template/{% if nix %}Nix.mk{% endif %}.jinja

Lines changed: 0 additions & 55 deletions
This file was deleted.
File renamed without changes.

0 commit comments

Comments
 (0)