Simple Python Flask web application designed for running in containers for demos
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.8 KiB

5 years ago
# Used by `image`, `push` & `deploy` targets, override as required
IMAGE_REG ?= ghcr.io
IMAGE_REPO ?= benc-uk/python-demoapp
IMAGE_TAG ?= latest
# 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:5000
# 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)
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: ## 🚀 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 $(DEPLOY_RES_GROUP) 😲"
az group delete -n $(DEPLOY_RES_GROUP) -o table --no-wait
test: ## 🎯 Unit tests for server and frontend
cd $(SRC_DIR); go test -v | tee server_tests.txt
cd $(SPA_DIR); npm run test
test-report: test ## 🎯 Unit tests for server and frontend (with report output)
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*
# ============================================================================
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