Skip to content
This repository was archived by the owner on Oct 10, 2022. It is now read-only.

Commit 9d71312

Browse files
authored
Merge pull request #15 from fastapi-mvc/add_nix_ci
Add nix CI, refactor makefile
2 parents 306d356 + 72f7e91 commit 9d71312

File tree

22 files changed

+366
-178
lines changed

22 files changed

+366
-178
lines changed

.github/workflows/main.yml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ jobs:
4242
nix_path: nixpkgs=channel:nixos-22.05
4343
- name: Generate test project
4444
run: nix-shell -p cookiecutter --run "cookiecutter --config-file test-config.yml --no-input ."
45+
- name: Setup poetry.lock cache
46+
uses: actions/cache@v3
47+
id: lock-cache
48+
with:
49+
path: ./example/poetry.lock
50+
key: ${{ hashFiles('**/pyproject.toml') }}
51+
- name: Generate poetry.lock
52+
if: steps.lock-cache.outputs.cache-hit != 'true'
53+
working-directory: ./example
54+
run: nix-shell -p poetry --run "poetry lock --no-update"
4555
- name: Set short commit SHA
4656
run: echo "SHORT_SHA=${GITHUB_SHA::7}" >> $GITHUB_ENV
4757
- name: Push generated test project
@@ -54,15 +64,15 @@ jobs:
5464
destination-repository-name: 'example'
5565
user-name: "github-actions[bot]"
5666
user-email: github-actions[bot]@users.noreply.github.com
57-
commit-message: ${{ format('[skip ci][{0}] Update generated project', env.SHORT_SHA) }}
67+
commit-message: ${{ format('[skip ci][fastapi-mvc/cookiecutter@{0}] Update generated project', env.SHORT_SHA) }}
5868
target-branch: master
5969
dispatch-remote-workflow:
6070
needs: generate
6171
runs-on: ubuntu-latest
6272
strategy:
6373
fail-fast: false
6474
matrix:
65-
workflow: [ 'main.yml', 'docs.yml']
75+
workflow: [ 'main.yml', 'docs.yml', 'nix.yml']
6676

6777
steps:
6878
- name: Dispatch ${{ matrix.workflow }} workflow
@@ -81,5 +91,5 @@ jobs:
8191
repo: example
8292
owner: fastapi-mvc
8393
run_id: ${{ steps.return_dispatch.outputs.run_id }}
84-
run_timeout_seconds: 600
94+
run_timeout_seconds: 1200
8595
poll_interval_ms: 5000

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,6 @@ venv
3232

3333
# direnv
3434
.direnv
35+
36+
# Nix build result
37+
result

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ This file documents changes to [fastapi-mvc/cookiecutter](https://github.com/fas
44

55
## Unreleased
66

7+
### Features
8+
9+
* Refactor Makefile to utilize different set of targets for Nix and Poetry [#2](https://github.com/fastapi-mvc/cookiecutter/issues/2). PR [#15](https://github.com/fastapi-mvc/cookiecutter/pull/15)
10+
* Add Nix CI workflow [#1](https://github.com/fastapi-mvc/cookiecutter/issues/1). PR [#15](https://github.com/fastapi-mvc/cookiecutter/pull/15)
11+
712
### Internal
813

914
* Add CI workflow for executing template checks [#3](https://github.com/fastapi-mvc/cookiecutter/issues/3). PR [#6](https://github.com/fastapi-mvc/cookiecutter/pull/6)

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ Cookiecutter template used by [fastapi-mvc/fastapi-mvc](https://github.com/fasta
2222
* Makefile, GitHub actions and utilities
2323
* Helm chart for Kubernetes deployment
2424
* Dockerfile with K8s and cloud in mind
25-
* Generate pieces of code or even your own generators
2625
* Reproducible development environment using Vagrant or Nix
2726

2827
## Quickstart

hooks/post_gen_project.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ def set_nix():
7272
"shell.nix",
7373
"image.nix",
7474
"default.nix",
75+
"editable.nix",
76+
"overlay.nix",
77+
"Nix.mk",
78+
".github/workflows/nix.yml",
7579
]
7680
)
7781

{{cookiecutter.folder_name}}/.dockerignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ venv
2323
.direnv
2424
site
2525
.git
26-
.github
26+
.github
27+
result

{{cookiecutter.folder_name}}/.github/workflows/main.yml

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ jobs:
6060
uses: actions/cache@v3
6161
with:
6262
path: ${{ env.POETRY_HOME }}
63-
key: ${{ runner.os }}-${{ matrix.python-version }}-${{ hashFiles('./pyproject.toml') }}
63+
key: ${{ runner.os }}-${{ matrix.python-version }}-${{ hashFiles('./pyproject.toml') }}-{{ hashFiles('./poetry.lock') }}
6464
- name: Install package
6565
run: make install
6666
if: steps.cached-poetry.outputs.cache-hit != 'true'
@@ -78,9 +78,9 @@ jobs:
7878
uses: actions/cache@v3
7979
with:
8080
path: ${{ env.POETRY_HOME }}
81-
key: ${{ runner.os }}-3.9-${{ hashFiles('./pyproject.toml') }}
81+
key: ${{ runner.os }}-3.9-${{ hashFiles('./pyproject.toml') }}-{{ hashFiles('./poetry.lock') }}
8282
- name: Build wheel
83-
run: $POETRY_HOME/bin/poetry build
83+
run: make build
8484
- name: Archive build artifacts
8585
uses: actions/upload-artifact@v2
8686
with:
@@ -91,6 +91,7 @@ jobs:
9191
needs: install
9292
runs-on: ubuntu-latest
9393
strategy:
94+
fail-fast: false
9495
matrix:
9596
python-version: [ '3.7', '3.8', '3.9', '3.10' ]
9697

@@ -104,13 +105,14 @@ jobs:
104105
uses: actions/cache@v3
105106
with:
106107
path: ${{ env.POETRY_HOME }}
107-
key: ${{ runner.os }}-${{ matrix.python-version }}-${{ hashFiles('./pyproject.toml') }}
108+
key: ${{ runner.os }}-${{ matrix.python-version }}-${{ hashFiles('./pyproject.toml') }}-{{ hashFiles('./poetry.lock') }}
108109
- name: Run metrics checks
109110
run: make metrics
110111
unit-tests:
111112
needs: install
112113
runs-on: ubuntu-latest
113114
strategy:
115+
fail-fast: false
114116
matrix:
115117
python-version: [ '3.7', '3.8', '3.9', '3.10' ]
116118

@@ -124,13 +126,14 @@ jobs:
124126
uses: actions/cache@v3
125127
with:
126128
path: ${{ env.POETRY_HOME }}
127-
key: ${{ runner.os }}-${{ matrix.python-version }}-${{ hashFiles('./pyproject.toml') }}
129+
key: ${{ runner.os }}-${{ matrix.python-version }}-${{ hashFiles('./pyproject.toml') }}-{{ hashFiles('./poetry.lock') }}
128130
- name: Run unit tests
129131
run: make unit-test
130132
integration-tests:
131133
needs: install
132134
runs-on: ubuntu-latest
133135
strategy:
136+
fail-fast: false
134137
matrix:
135138
python-version: [ '3.7', '3.8', '3.9', '3.10' ]
136139

@@ -144,7 +147,7 @@ jobs:
144147
uses: actions/cache@v3
145148
with:
146149
path: ${{ env.POETRY_HOME }}
147-
key: ${{ runner.os }}-${{ matrix.python-version }}-${{ hashFiles('./pyproject.toml') }}
150+
key: ${{ runner.os }}-${{ matrix.python-version }}-${{ hashFiles('./pyproject.toml') }}-{{ hashFiles('./poetry.lock') }}
148151
- name: Run integration tests
149152
run: make integration-test
150153
coverage:
@@ -161,12 +164,6 @@ jobs:
161164
uses: actions/cache@v3
162165
with:
163166
path: ${{ env.POETRY_HOME }}
164-
key: ${{ runner.os }}-3.9-${{ hashFiles('./pyproject.toml') }}
167+
key: ${{ runner.os }}-3.9-${{ hashFiles('./pyproject.toml') }}-{{ hashFiles('./poetry.lock') }}
165168
- name: Run coverage{% endraw %}
166-
run: $POETRY_HOME/bin/poetry run pytest --cov={{cookiecutter.package_name}} --cov-fail-under=70 --cov-report=xml --cov-report=term-missing tests
167-
- name: Archive coverage artifacts
168-
uses: actions/upload-artifact@v2
169-
with:
170-
name: coverage
171-
path: ./coverage.xml
172-
retention-days: 60
169+
run: $POETRY_HOME/bin/poetry run pytest --cov={{cookiecutter.package_name}} --cov-fail-under=70 --cov-report=xml --cov-report=term-missing tests
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
{%- raw %}
2+
name: ❄️ Nix CI ❄️
3+
4+
on:
5+
push:
6+
branches:
7+
- master
8+
pull_request:
9+
branches:
10+
- master
11+
# This trigger is required by fastapi-mvc automation to dispatch this concrete workflow
12+
# from fastapi-mvc 'CI workflow' (https://github.com/fastapi-mvc/cookiecutter/actions/workflows/main.yml),
13+
# and await its result. By no means should this be included in the template this file was generated from.
14+
# It's just a temporary monkey patch to fulfill the GH automation use case.
15+
# Feel free to remove it.
16+
workflow_dispatch:
17+
inputs:
18+
distinct_id:
19+
required: true
20+
description: "Input required by codex-/return-dispatch@v1"
21+
22+
jobs:
23+
# This job checks if an identical workflow is being triggered by different
24+
# event and skips it. For instance there is no need to run the same pipeline
25+
# twice for pull_request and push for identical commit sha.
26+
pre_job:
27+
runs-on: ubuntu-latest
28+
outputs:
29+
should_skip: ${{ steps.skip_check.outputs.should_skip }}
30+
steps:
31+
# This echo is required by codex-/return-dispatch@v1 in order to identify dispatched workflow.
32+
# By no means should this be included in the template this file was generated from.
33+
# It's just a temporary monkey patch to fulfill the GH automation use case.
34+
# Feel free to remove it.
35+
- name: echo distinct ID ${{ github.event.inputs.distinct_id }}
36+
run: echo ${{ github.event.inputs.distinct_id }}
37+
- id: skip_check
38+
uses: fkirc/[email protected]
39+
with:
40+
skip_after_successful_duplicate: 'true'
41+
concurrent_skipping: same_content
42+
do_not_skip: '["pull_request", "workflow_dispatch", "schedule"]'
43+
nix-build:
44+
needs: pre_job
45+
if: ${{ needs.pre_job.outputs.should_skip != 'true' }}
46+
runs-on: ubuntu-latest
47+
48+
steps:
49+
- uses: actions/checkout@v3
50+
- name: Install Nix ❄️
51+
uses: cachix/install-nix-action@v17
52+
with:
53+
extra_nix_config: "system-features = nixos-test benchmark big-parallel kvm"
54+
nix_path: nixpkgs=channel:nixos-22.05
55+
# Remove bellow step if you do not want to use Cachix - Nix binary cache.
56+
# For OpenSource projects there is free 5GB of storage.
57+
# https://www.cachix.org
58+
- name: Setup Cachix ❄️
59+
uses: cachix/cachix-action@v10
60+
with:
61+
name: fastapi-mvc
62+
authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
63+
- name: Install make
64+
run: nix-env -i gnumake -f '<nixpkgs>'
65+
- name: Build package
66+
run: make build
67+
- name: Build container image
68+
run: make image
69+
nix-ci:
70+
needs: pre_job
71+
if: ${{ needs.pre_job.outputs.should_skip != 'true' }}
72+
runs-on: ubuntu-latest
73+
strategy:
74+
fail-fast: false
75+
matrix:
76+
python-version: [ 'python37', 'python38', 'python39', 'python310' ]
77+
env:
78+
PYTHON_NIXPKG: ${{ matrix.python-version }}
79+
80+
steps:
81+
- uses: actions/checkout@v3
82+
- name: Install Nix
83+
uses: cachix/install-nix-action@v17
84+
with:
85+
nix_path: nixpkgs=channel:nixos-22.05
86+
# Remove bellow step if you do not want to use Cachix - Nix binary cache.
87+
# For OpenSource projects there is free 5GB of storage.
88+
# https://www.cachix.org
89+
- name: Setup Cachix Nix cache
90+
uses: cachix/cachix-action@v10
91+
with:
92+
name: fastapi-mvc
93+
authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
94+
- name: Install make
95+
run: nix-env -i gnumake -f '<nixpkgs>'
96+
- name: Run metrics checks
97+
run: make metrics
98+
- name: Run unit-tests
99+
run: make unit-test
100+
- name: Run integration-tests
101+
run: make integration-test
102+
{%- endraw %}

{{cookiecutter.folder_name}}/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,6 @@ venv/
3636

3737
# Sphinx build dir
3838
site/
39+
40+
# Nix build result
41+
result

{{cookiecutter.folder_name}}/Makefile

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

1212
TAG ?= $(shell cat TAG)
13-
13+
{%- if cookiecutter.nix == "yes" %}
14+
USE_NIX ?= $(shell command -v nix || echo "False")
15+
{% endif %}
1416
help: ## Display this help
1517
@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)
1618

17-
.PHONY: image
18-
image: ## Build {{cookiecutter.docker_image_name}} image
19-
@build/image.sh
20-
21-
.PHONY: clean-image
22-
clean-image: ## Clean {{cookiecutter.docker_image_name}} image
23-
@build/clean-image.sh
24-
25-
.PHONY: install
26-
install: ## Install {{cookiecutter.project_name}} with poetry
27-
@build/install.sh
28-
29-
.PHONY: metrics
30-
metrics: install ## Run {{cookiecutter.project_name}} metrics checks
31-
@build/metrics.sh
32-
33-
.PHONY: unit-test
34-
unit-test: install ## Run {{cookiecutter.project_name}} unit tests
35-
@build/unit-test.sh
36-
37-
.PHONY: integration-test
38-
integration-test: install ## Run {{cookiecutter.project_name}} integration tests
39-
@build/integration-test.sh
40-
41-
.PHONY: docs
42-
docs: install ## Build {{cookiecutter.project_name}} documentation
43-
@build/docs.sh
44-
{% if cookiecutter.helm == "yes" %}
45-
.PHONY: dev-env
46-
dev-env: image ## Start a local Kubernetes cluster using minikube and deploy application
47-
@build/dev-env.sh
48-
49-
.PHONY: clean
50-
clean: ## Remove .cache directory and cached minikube
51-
minikube delete && rm -rf ~/.cache ~/.minikube
52-
{% endif %}
5319
.PHONY: show-version
54-
show-version:
55-
echo -n $(TAG)
20+
show-version: ## Display version
21+
echo -n "${TAG}"
22+
{% if cookiecutter.nix == "yes" %}
23+
ifeq ($(USE_NIX), False)
24+
include Poetry.mk
25+
else
26+
include Nix.mk
27+
endif
28+
{% else %}
29+
include Poetry.mk
30+
{% endif %}

0 commit comments

Comments
 (0)