Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
168 lines (137 loc) · 4.33 KB
/
Copy pathMakefile
File metadata and controls
168 lines (137 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
SHELL=/bin/bash -o pipefail
# If no 'ENV=xxx' is provided when calling the targets we will asume 'docker' as the environment
ENV ?= docker
include ./envs/${ENV}.env
export
define hr
@printf '%.s─' $$(seq 1 $$(tput cols))
@echo "$$(tput setaf 3)Running 'make $@'$$(tput sgr0)"
@printf '%.s─' $$(seq 1 $$(tput cols))
endef
# Python package
PACKAGE := app
# Enable docker's buildkit extensions
export DOCKER_BUILDKIT=1
export DOCKER_CLI_EXPERIMENTAL=enabled
# Image info
IMAGE_REPO = donhector
IMAGE_NAME = fastapi-hello-world
IMAGE = $(IMAGE_REPO)/$(IMAGE_NAME)
TAG ?= latest
# Build args
DATE = $$(date "+%Y%m%dT%H%M%S")
COMMIT = $$(git rev-parse --short HEAD)
.DEFAULT_GOAL = help
.PHONY: help
help: ## Shows this help.
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
.PHONY: build
build: ## Build the docker image.
$(call hr)
@docker build -t ${IMAGE_NAME}:${TAG} \
--build-arg BUILD_DATE=${DATE} \
--build-arg VERSION=${TAG} \
--build-arg COMMIT=${COMMIT} \
.
.PHONY: build-nc
build-nc: ## Build the docker image without reusing the cache
$(call hr)
@docker build -t ${IMAGE_NAME}:${TAG} \
--build-arg BUILD_DATE=${DATE} \
--build-arg VERSION=${TAG} \
--build-arg COMMIT=${COMMIT} \
--no-cache \
.
.PHONY: tag
tag: ## Tag the docker image for a specific registry (ie: make tag ENV=aws)
$(call hr)
docker tag ${IMAGE_NAME}:${TAG} ${REGISTRY}/${IMAGE}:${TAG}
.PHONY: run
run: build ## Run the docker container.
$(call hr)
@docker run --name ${IMAGE_NAME} --rm -d -p 8000:80 ${IMAGE_NAME}
.PHONY: health
health: ## Check application health
$(call hr)
@curl -skiL 127.0.0.1:8000 | jq .
.PHONY: docs
docs: ## Open the application swagger documentation (assuming you are inside WSL2).
$(call hr)
@wslview http://127.0.0.1:8000/docs
.PHONY: redoc
redoc: ## Open the application redoc documentation (assuming you are inside WSL2).
$(call hr)
@wslview http://127.0.0.1:8000/redoc
.PHONY: stop
stop: ## Stop the docker container.
$(call hr)
@docker stop ${IMAGE_NAME}
.PHONY: clean
clean: stop ## Remove the docker container and image.
$(call hr)
@docker rmi -f $$(docker images -q ${IMAGE_NAME})
.PHONY: shell
shell: ## Run and shell into the docker container.
$(call hr)
@docker run --name ${IMAGE_NAME} --rm -it -v $(shell pwd)/app:/app/ ${REGISTRY}/${IMAGE}:${TAG} /bin/sh
.PHONY: exec
exec: ## Exec inside the running docker container.
$(call hr)
@docker exec -ti ${IMAGE_NAME} /bin/sh
.PHONY: login
login: ## Login to a docker registry.
$(call hr)
@echo "Login into '${REGISTRY}' with username '${REGISTRY_USERNAME}'..."
@echo ${REGISTRY_PASSWORD} | docker login --username ${REGISTRY_USERNAME} --password-stdin ${REGISTRY}
.PHONY: push
push: ## Push the docker image to a specific registry (ie: make push ENV=aws).
$(call hr)
@if [ "${ENV}" = "aws" ]; then aws ecr create-repository --repository-name ${IMAGE} || true; fi
@docker push ${REGISTRY}/${IMAGE}:${TAG}
.PHONY: release
release: build tag push ## Build and push image to registry.
.PHONY: logs
logs: ## Show container logs
@docker logs ${IMAGE_NAME}
.PHONY: registry-start
registry-start: ## Start a local docker registry on port 5000.
$(call hr)
@mkdir -p /tmp/docker-registry
@docker run --rm --name httpd --entrypoint htpasswd \
httpd:2 -Bbn ${REGISTRY_USERNAME} ${REGISTRY_PASSWORD} > /tmp/docker-registry/htpasswd
@docker run --rm --name registry -d -p 5000:5000 \
-v /tmp/docker-registry:/var/lib/registry \
-e REGISTRY_AUTH=htpasswd \
-e 'REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm' \
-e REGISTRY_AUTH_HTPASSWD_PATH=/var/lib/registry/htpasswd \
registry:2
.PHONY: registry-stop
registry-stop: ## Delete a local docker registry.
$(call hr)
@docker stop registry
@sudo rm -rfv /tmp/docker-registry
install:
#poetry config virtualenvs.in-project true
@poetry env use python3
@poetry update
@poetry install
test-watch:
@ptw -- --last-failed --new-first
test:
@poetry run pytest \
--strict-markers \
--tb=short \
--cov=app \
--cov=tests \
--cov-branch \
--cov-report=term-missing \
--cov-report=html \
--cov-report=xml \
--no-cov-on-fail \
--cov-fail-under=100 \
--numprocesses=auto \
--asyncio-mode=auto
clean-app:
@rm -rf .pytest_cache .coverage htmlcov coverage.xml
@find $(PACKAGE) tests -name '__pycache__' -exec rm -rv {} +
@rm -rf *.egg-info