generated from NHSDigital/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
183 lines (144 loc) · 7.09 KB
/
Makefile
File metadata and controls
183 lines (144 loc) · 7.09 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# This file is for you! Edit it to implement your own hooks (make targets) into
# the project as automated steps to be executed on locally and in the CD pipeline.
include scripts/init.mk
# Within the build container the `doas` command is required when running docker commands as we're running as a non-root user.
ifeq (${IN_BUILD_CONTAINER}, true)
docker := doas docker
else
docker := docker
endif
dockerNetwork := pathology-local
# ==============================================================================
# Example CI/CD targets are: dependencies, build, publish, deploy, clean, etc.
.PHONY: dependencies
.ONESHELL:
dependencies: # Install dependencies needed to build and test the project @Pipeline
if [[ "$${IN_BUILD_CONTAINER}" == "true" ]]; then \
eval "$$(pyenv init -)"; \
pyenv activate pathology; \
fi
cd pathology-api && poetry sync
cd ../
if [[ "$${IN_BUILD_CONTAINER}" == "true" ]]; then \
pyenv deactivate pathology; \
fi
if [[ "$${IN_BUILD_CONTAINER}" == "true" ]]; then \
pyenv activate pathology-mocks; \
fi
cd mocks && poetry sync
cd ../
if [[ "$${IN_BUILD_CONTAINER}" == "true" ]]; then \
pyenv deactivate pathology-mocks; \
fi
.PHONY: build-pathology
.ONESHELL:
build-pathology:
@if [[ "$${IN_BUILD_CONTAINER}" == "true" ]]; then \
eval "$$(pyenv init -)"; \
pyenv activate pathology; \
fi
@cd pathology-api
@echo "Starting build for pathology API..."
@echo "Running type checks..."
@rm -rf target && rm -rf dist
@poetry run mypy --no-namespace-packages .
@echo "Packaging dependencies..."
@poetry build --format=wheel
VERSION=$$(poetry version -s)
@pip install "dist/pathology_api-$$VERSION-py3-none-any.whl" --target "./target/pathology-api" --platform manylinux2014_x86_64 --only-binary=:all:
# Copy lambda_handler file separately as it is not included within the package.
@cp lambda_handler.py ./target/pathology-api/
@cd ./target/pathology-api
@zip -r "../artifact.zip" .
@if [[ "$${IN_BUILD_CONTAINER}" == "true" ]]; then \
pyenv deactivate pathology; \
fi
.PHONY: build-mocks
.ONESHELL:
build-mocks:
@if [[ "$${IN_BUILD_CONTAINER}" == "true" ]]; then \
eval "$$(pyenv init -)"; \
pyenv activate pathology-mocks; \
fi
@cd mocks
@echo "Starting build for mocks..."
@echo "Running type checks..."
@rm -rf target && rm -rf dist
@poetry run mypy --no-namespace-packages .
@echo "Packaging dependencies..."
@poetry build --format=wheel
VERSION=$$(poetry version -s)
@pip install "dist/pathology_api_mocks-$$VERSION-py3-none-any.whl" --target "./target/mocks" --platform manylinux2014_x86_64 --only-binary=:all:
# Copy lambda_handler file separately as it is not included within the package.
@cp lambda_handler.py ./target/mocks/
@cd ./target/mocks
@zip -r "../artifact.zip" .
@if [[ "$${IN_BUILD_CONTAINER}" == "true" ]]; then \
pyenv deactivate pathology-mocks; \
fi
.PHONY: build
build: clean-artifacts dependencies build-pathology build-mocks
@echo "Built artifacts for both pathology and mocks"
.PHONY: build-images
build-images: build # Build the project artefact @Pipeline
@mkdir -p infrastructure/images/pathology-api/resources/build
@cp -r pathology-api/target/pathology-api infrastructure/images/pathology-api/resources/build
@mkdir -p infrastructure/images/mocks/resources/build
@cp -r mocks/target/mocks infrastructure/images/mocks/resources/build
@echo "Building Docker image using Docker. Utilising python version: ${PYTHON_VERSION} ..."
@$(docker) buildx build --load --platform=linux/amd64 --provenance=false --build-arg PYTHON_VERSION=${PYTHON_VERSION} -t localhost/pathology-api-image infrastructure/images/pathology-api
@echo "Docker image 'pathology-api-image' built successfully!"
@echo "Building api gateway image using Docker. Utilising python version: ${PYTHON_VERSION} ..."
@$(docker) buildx build --load --build-arg PYTHON_VERSION=${PYTHON_VERSION} -t localhost/api-gateway-mock-image infrastructure/images/api-gateway-mock
@echo "Docker image 'api-gateway-mock-image' built successfully!"
@echo "Building mocks Docker image using Docker. Utilising python version: ${PYTHON_VERSION} ..."
@$(docker) buildx build --load --platform=linux/amd64 --provenance=false --build-arg PYTHON_VERSION=${PYTHON_VERSION} -t localhost/mocks-image infrastructure/images/mocks
@echo "Docker image 'mocks-image' built successfully!"
publish: # Publish the project artefact @Pipeline
# TODO: Implement the artefact publishing step
deploy: clean-docker build-images # Deploy the project artefact to the target environment @Pipeline
$(docker) network create $(dockerNetwork) || echo "Docker network '$(dockerNetwork)' already exists."
$(docker) run --platform linux/amd64 --name pathology-api -p 5001:8080 --network $(dockerNetwork) -d localhost/pathology-api-image
$(docker) run --platform linux/amd64 --name mocks -p 5003:8080 --network $(dockerNetwork) -d localhost/mocks-image
$(docker) run --name pathology-api-gateway -p 5002:5000 -e TARGET_CONTAINER='PATHOLOGY_API' -e TARGET_URL='http://pathology-api:8080' --network $(dockerNetwork) -d localhost/api-gateway-mock-image
$(docker) run --name mocks-api-gateway -p 5005:5000 -e TARGET_CONTAINER='MOCKS' -e TARGET_URL='http://mocks:8080' --network $(dockerNetwork) -d localhost/api-gateway-mock-image
clean-artifacts:
@echo "Removing build artefacts..."
@rm -rf infrastructure/images/pathology-api/resources/build/
@rm -rf pathology-api/target && rm -rf pathology-api/dist
@rm -rf infrastructure/images/mocks/resources/build/
@rm -rf mocks/target && rm -rf mocks/dist
clean-docker: stop
@echo "Removing pathology API container..."
@$(docker) rm pathology-api || echo "No pathology API container currently exists."
@echo "Removing pathology-api api-gateway container..."
@$(docker) rm pathology-api-gateway || echo "No pathology-api-gateway container currently exists."
@echo "Removing mocks container..."
@$(docker) rm mocks || echo "No mocks container currently exists."
@echo "Removing mocks api-gateway container..."
@$(docker) rm mocks-api-gateway || echo "No mocks-api-gateway container currently exists."
clean:: clean-artifacts clean-docker # Clean-up project resources (main) @Operations
.PHONY: stop
stop:
@echo "Stopping pathology API container..."
@$(docker) stop pathology-api || echo "No pathology API container currently running."
@echo "Stopping pathology-api-gateway container..."
@$(docker) stop pathology-api-gateway || echo "No pathology-api-gateway container currently running."
@echo "Stopping mocks container..."
@$(docker) stop mocks || echo "No mocks container currently running."
@echo "Stopping mocks-api-gateway container..."
@$(docker) stop mocks-api-gateway || echo "No mocks-api-gateway container currently running."
config:: # Configure development environment (main) @Configuration
# Configure poetry to trust dev certificate if specified
@if [[ -n "$${DEV_CERTS_INCLUDED}" ]]; then \
echo "Configuring poetry to trust the dev certificate..." ; \
poetry config certificates.PyPI.cert /etc/ssl/cert.pem ; \
fi
make _install-dependencies
# ==============================================================================
${VERBOSE}.SILENT: \
build \
clean \
config \
dependencies \
deploy \