-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMakefile
More file actions
115 lines (98 loc) · 3.73 KB
/
Makefile
File metadata and controls
115 lines (98 loc) · 3.73 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
# Include variables and help modules
include Makefile.variables
include Makefile.help
# Default target
.DEFAULT_GOAL := help
# Phony targets ensure Make doesn't get confused by filenames
.PHONY: build run deploy run-it clean exec log test dev-pipeline run-all-tests run-test wait-container-ready
# Docker Commands for Reusability
DOCKER_RUN_BASE := docker run --rm --name $(CONTAINER_NAME) \
-e NODE_ENV=$(NODE_ENV) \
-p $(HOST_PORT):$(CONTAINER_PORT)
# Determine if we should run in detached mode or interactively
ifeq ($(INTERACTIVE),true)
DOCKER_RUN := $(DOCKER_RUN_BASE) -it --entrypoint $(CMD)
else
DOCKER_RUN := $(DOCKER_RUN_BASE) -d
endif
DOCKER_EXEC := docker exec -it $(CONTAINER_NAME)
DOCKER_CLEAN := docker rm -f $(CONTAINER_NAME) || true
TEST_VOLUMES := \
-v $(PWD)/src/tests:/usr/src/app/tests \
-v $(PWD)/src/examples/simple-server/index.js:/usr/src/app/index.js \
-v $(PWD)/src/examples/simple-server/.config/worker/services.yaml:/home/udx/.config/worker/services.yaml
TEST_ENV := -e NODE_ENV=test
TEST_RUN := docker run --rm $(TEST_VOLUMES) $(TEST_ENV) $(DOCKER_IMAGE)
# Build the Docker image with multi-platform support
build:
@echo "Building Docker image..."
@if [ "$(MULTIPLATFORM)" = "true" ]; then \
echo "Building Docker image for multiple platforms..."; \
docker buildx build --platform $(BUILD_PLATFORMS) -t $(DOCKER_IMAGE) .; \
else \
echo "Building Docker image for the local platform..."; \
docker build -t $(DOCKER_IMAGE) .; \
fi
@echo "Docker image build completed."
# Run Docker container in specified environment with readiness check
run: clean
@echo "Running Docker container..."
@$(DOCKER_RUN) $(DOCKER_IMAGE)
@$(MAKE) wait-container-ready
@echo "Container started successfully."
# Deploy Docker container (production mode)
deploy: clean
@echo "Deploying Docker container (production mode)..."
@$(MAKE) run NODE_ENV=production
# Run Docker container in interactive mode
run-it:
$(MAKE) run INTERACTIVE=true CMD="/bin/bash"
# Execute a command inside the running container
exec:
@echo "Executing command in Docker container..."
@$(DOCKER_EXEC) $(if $(CMD),$(CMD),/bin/bash)
# View logs of the running container
log:
@echo "Viewing Docker container logs..."
@docker logs $(CONTAINER_NAME) || echo "No running container to log."
# Clean up by stopping and removing the container if it exists
clean:
@echo "Stopping and removing Docker container if it exists..."
@$(DOCKER_CLEAN)
# Wait for container to be ready by checking application readiness
wait-container-ready:
@echo "Waiting for the container to be ready..."
@counter=0; \
while ! curl -s -o /dev/null -w "%{http_code}" http://localhost:$(HOST_PORT) | grep -q "200"; do \
if [ $$counter -ge 30 ]; then \
echo "Timeout: Application did not start"; \
exit 1; \
fi; \
echo "Waiting for application to be ready..."; \
sleep 1; \
counter=$$((counter + 1)); \
done
@echo "Container is ready."
# Run all tests in the tests directory
run-all-tests: build
@echo "Running all tests in Docker container..."
@$(TEST_RUN) \
/bin/sh -c 'cd /usr/src/app/tests && for test_script in *.sh; do \
echo "Running $$test_script..."; \
sh ./$$test_script || { echo "Test $$test_script failed"; exit 1; }; \
done'
# Run a specific test script
run-test: build
@if [ -z "$(TEST_SCRIPT)" ]; then \
echo "TEST_SCRIPT is required (e.g., make run-test TEST_SCRIPT=10_validate_environment.sh)"; \
exit 1; \
fi
@echo "Running test $(TEST_SCRIPT) in Docker container..."
@$(TEST_RUN) \
/bin/sh -c 'cd /usr/src/app/tests && sh ./$(TEST_SCRIPT)'
# Run validation tests (build and run-all-tests)
test: run-all-tests
@echo "Validation tests completed."
# Development pipeline (build and test)
dev-pipeline: test
@echo "Development pipeline completed successfully."