-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakefile
106 lines (90 loc) · 2.85 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
95
96
97
98
99
100
101
102
103
104
105
106
include .env
PACKAGES := $(shell go list ./...)
BASENAME := $(shell basename ${PWD})
.PHONY: run live build db redis stop cleanI cleanC exec test help
.DEFAULT_GOAL:= run
help: makefile
@echo
@echo " Choose a make command to run"
@echo
@sed -n 's/^##//p' $< | column -t -s ':' | sed -e 's/^/ /'
@echo
## run: Build the Docker image and run the container
run: cleanC create_network connect build
@docker run -d \
--env-file .env \
--restart always \
--name $(APP_NAME) \
--network $(APP_NAME) \
-p $(APP_PORT):$(APP_PORT) \
$(APP_NAME)
## live: Go build and running
live:
# find . -type f \( -name '*.go' -o -name '*.gohtml' \) | entr -r sh -c 'make && docker logs --follow $(APP_NAME)'
find . -type f \( -name '*.go' -o -name '*.gohtml' \) | entr -r sh -c 'go build -o /tmp/build ./cmd && /tmp/build'
## build: Build the Docker image
build:
@docker build -t $(APP_NAME) .
connect:
@if $(APP_ENV) == "prod"; then \
$(MAKE) db
$(MAKE) redis
else \
$(MAKE) db
@echo "Skipping db target since APP_ENV is not 'prod'"
fi
## db: Run Postgres
db: create_volume
@docker run -d --name $(APP_NAME)-postgres \
-p $(DB_PORT):5432 \
--network $(APP_NAME) \
-e POSTGRES_DB=$(DB_NAME) \
-e POSTGRES_PASSWORD=$(DB_PASS) \
-v $(APP_NAME):/var/lib/postgresql/data \
postgres
## redis: Run Redis
redis:
@docker run -d --name $(APP_NAME)-redis -p 6379:6379 --restart always --network $(APP_NAME) redis:latest
## create_network: Create network for this project name
create_network:
@if ! docker network inspect $(APP_NAME) >/dev/null 2>&1; then \
docker network create $(APP_NAME); \
else \
echo "Network '$(APP_NAME)' already exists, using existing network."; \
fi
## create_volume: Create volume for this project name
create_volume:
@if ! docker volume inspect $(APP_NAME) >/dev/null 2>&1; then \
docker volume create $(APP_NAME); \
else \
echo "Volume '$(APP_NAME)' already exists, skipping creation."; \
fi
## stop: Stop and remove the Docker container
stop:
@docker stop --time=600 $(APP_NAME)
@docker rm $(APP_NAME)
## exec: Run the application inside the Docker container
exec:
@docker exec -it $(APP_NAME) $(CMD)
## cleanI: Clean up the Docker image
cleanI:
@docker rmi $(APP_NAME)
@docker builder prune --filter="image=$(APP_NAME)"
@docker rmi $(docker images -f "dangling=true" -q)
## cleanC: Clean up the Docker containers
cleanC:
@CONTAINER_EXISTS=$$(docker ps -aq --filter name=$(APP_NAME)); \
if [ -n "$$CONTAINER_EXISTS" ]; then \
echo "Stopping and removing containers starting with $(APP_NAME)"; \
CONTAINERS=$$(docker ps -aq --filter name=$(APP_NAME)); \
for container in $$CONTAINERS; do \
echo "Stopping and removing container $$container"; \
docker stop $$container; \
docker rm $$container; \
done; \
else \
echo "No such containers starting with: $(APP_NAME)"; \
fi
## test: Run all test
test:
@go test -v ./...