forked from benc-uk/python-demoapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makefile
94 lines (73 loc) · 3.32 KB
/
makefile
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
# Used by `image`, `push` & `deploy` targets, override as required
IMAGE_REG ?= docker.io
IMAGE_REPO ?= chathu1994/python-demoapp
IMAGE_TAG ?= $(shell date -u '+%Y%m%d')
PORT ?= 5000
# Used by `deploy` target, sets Azure webap defaults, override as required
AZURE_RES_GROUP ?= temp-demoapps
AZURE_REGION ?= uksouth
AZURE_SITE_NAME ?= pythonapp-$(shell git rev-parse --short HEAD)
# Used by `test-api` target
TEST_HOST ?= localhost:$(PORT)
# Don't change
SRC_DIR := src
.PHONY: help lint lint-fix image push run deploy undeploy clean test-api .EXPORT_ALL_VARIABLES
.DEFAULT_GOAL := help
help: ## 💬 This help message
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'
lint: venv ## 🔎 Lint & format, will not fix but sets exit code on error
. $(SRC_DIR)/.venv/bin/activate \
&& black --check $(SRC_DIR) \
&& flake8 src/app/ && flake8 src/run.py
lint-fix: venv ## 📜 Lint & format, will try to fix errors and modify code
. $(SRC_DIR)/.venv/bin/activate \
&& black $(SRC_DIR)
# image: ## 🔨 Build container image from Dockerfile
# docker build . --file build/Dockerfile \
# --tag $(IMAGE_REG)/$(IMAGE_REPO):$(IMAGE_TAG)
image: ## 🔨 Build container image from Dockerfile
docker build . \
--tag $(IMAGE_REG)/$(IMAGE_REPO):$(IMAGE_TAG)
push: ## 📤 Push container image to registry
docker push $(IMAGE_REG)/$(IMAGE_REPO):$(IMAGE_TAG)
run: venv ## 🏃 Run the server locally using Python & Flask
. $(SRC_DIR)/.venv/bin/activate \
&& python src/run.py
deploy: # run as a docker container
docker rm python-demo-app -f
docker run -d -p $(PORT):$(PORT) --name python-demo-app $(IMAGE_REPO):$(IMAGE_TAG)
# deploy: ## 🚀 Deploy to Azure Web App
# az group create --resource-group $(AZURE_RES_GROUP) --location $(AZURE_REGION) -o table
# az deployment group create --template-file deploy/webapp.bicep \
# --resource-group $(AZURE_RES_GROUP) \
# --parameters webappName=$(AZURE_SITE_NAME) \
# --parameters webappImage=$(IMAGE_REG)/$(IMAGE_REPO):$(IMAGE_TAG) -o table
# @echo "### 🚀 Web app deployed to https://$(AZURE_SITE_NAME).azurewebsites.net/"
# undeploy: ## 💀 Remove from Azure
# @echo "### WARNING! Going to delete $(AZURE_RES_GROUP) 😲"
# az group delete -n $(AZURE_RES_GROUP) -o table --no-wait
test: venv ## 🎯 Unit tests for Flask app
. $(SRC_DIR)/.venv/bin/activate \
&& pytest -v
test-report: venv ## 🎯 Unit tests for Flask app (with report output)
. $(SRC_DIR)/.venv/bin/activate \
&& pytest -v --junitxml=test-results.xml
test-api: .EXPORT_ALL_VARIABLES ## 🚦 Run integration API tests, server must be running
cd tests \
&& npm install newman \
&& ./node_modules/.bin/newman run ./postman_collection.json --env-var apphost=$(TEST_HOST)
clean: ## 🧹 Clean up project
rm -rf $(SRC_DIR)/.venv
rm -rf tests/node_modules
rm -rf tests/package*
rm -rf test-results.xml
rm -rf $(SRC_DIR)/app/__pycache__
rm -rf $(SRC_DIR)/app/tests/__pycache__
rm -rf .pytest_cache
rm -rf $(SRC_DIR)/.pytest_cache
# ============================================================================
venv: $(SRC_DIR)/.venv/touchfile
$(SRC_DIR)/.venv/touchfile: $(SRC_DIR)/requirements.txt
python3 -m venv $(SRC_DIR)/.venv
. $(SRC_DIR)/.venv/bin/activate; pip install -Ur $(SRC_DIR)/requirements.txt
touch $(SRC_DIR)/.venv/touchfile