-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
145 lines (122 loc) · 4.89 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
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
USER=$(shell id -u):$(shell id -g)
PROJECT_NAME=skillect
BACKEND_DIR=backend
FRONTEND_DIR=frontend
MAKE_BACKEND=make --directory=$(BACKEND_DIR)
MAKE_FRONTEND=make --directory=$(FRONTEND_DIR)
DOCKER_USER=antoinemdn
DOCKER_REPO_FRONTEND=$(PROJECT_NAME)-frontend
DOCKER_REPO_BACKEND=$(PROJECT_NAME)-backend
DOCKER_IMAGE_FRONTEND=$(DOCKER_USER)/$(DOCKER_REPO_FRONTEND)
DOCKER_IMAGE_BACKEND=$(DOCKER_USER)/$(DOCKER_REPO_BACKEND)
DOCKER_COMPOSE_DEVELOPMENT=docker-compose -f docker-compose.yml -f docker-compose.development.yml
DOCKER_COMPOSE_PRODUCTION=docker-compose -f docker-compose.yml -f docker-compose.production.yml
TRAEFIK_PUBLIC_NETWORK=traefik-public
DOMAIN=mandin.dev
DEFAULT_SUPERUSER=admin@$(DOMAIN)
FIRST_SUPERUSER=$(DEFAULT_SUPERUSER)
SSH_USER=deployer
DEPLOYEMENT_REF=main
all: deploy
help: ## Display help message
help:
@echo Make targets:
@echo
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'
.env.dev: # Generate development .env
@cp env/base.env .env
@echo "" >> .env
@cat env/development.env >> .env
_needed_env_var: # Check needed environment variables
ifndef POSTGRES_PASSWORD
$(error POSTGRES_PASSWORD environment variable not set)
endif
ifndef FIRST_SUPERUSER_PASSWORD
$(error FIRST_SUPERUSER_PASSWORD environment variable not set)
endif
ifeq ($(FIRST_SUPERUSER),$(DEFAULT_SUPERUSER))
$(warning FIRST_SUPERUSER is set to '$(DEFAULT_SUPERUSER)', consider changing it.)
endif
.env.prod: # Generate production .env
.env.prod: _needed_env_var
@cp env/base.env .env
@echo "" >> .env
@echo "# Production environment variables" >> .env
@echo "DOMAIN=${DOMAIN}" >> .env
@echo "DOCKER_IMAGE_BACKEND=${DOCKER_IMAGE_BACKEND}" >> .env
@echo "DOCKER_IMAGE_FRONTEND=${DOCKER_IMAGE_FRONTEND}" >> .env
@echo "POSTGRES_PASSWORD=${POSTGRES_PASSWORD}" >> .env
@echo "BACKEND_CORS_ORIGINS=["https://${DOMAIN}"]" >> .env
@echo "FIRST_SUPERUSER=${FIRST_SUPERUSER}" >> .env
@echo "FIRST_SUPERUSER_PASSWORD=${FIRST_SUPERUSER_PASSWORD}" >> .env
dev: ## Start the application in development mode using docker-compose
dev: .env.dev
$(DOCKER_COMPOSE_DEVELOPMENT) \
up --build --detach
$(TRAEFIK_PUBLIC_NETWORK): # Create the network
@docker network create $(TRAEFIK_PUBLIC_NETWORK) || \
echo "> $(TRAEFIK_PUBLIC_NETWORK) network already exist."
deploy: ## Start the app in production mode
deploy: .env.prod $(TRAEFIK_PUBLIC_NETWORK) stop
$(DOCKER_COMPOSE_PRODUCTION) pull
$(DOCKER_COMPOSE_PRODUCTION) up --detach
known_hosts:
ssh-keyscan -H $(DOMAIN) > known_hosts
ssh_key: # Note: SSH_KEY must be the rsa ssh key with new lines (\n) replaced by #
ifndef SSH_KEY
$(error SSH_KEY must be defined. It is the private key to use in order to login via SSH)
endif
touch ssh_key
chmod 600 ssh_key
echo "$(SSH_KEY)" | sed -r 's/#/\n/g' >> ssh_key
chmod 400 ssh_key
ssh_deploy: ## Connect via SSH to the deployment device and deploy the latest version of the app
ssh_deploy: _needed_env_var known_hosts ssh_key
ssh -o UserKnownHostsFile=known_hosts \
-i ssh_key \
$(SSH_USER)@$(DOMAIN) \
"cd /home/${SSH_USER}/$(PROJECT_NAME) \
&& git fetch -p \
&& git reset --hard origin/$(DEPLOYEMENT_REF) \
&& make deploy \
POSTGRES_PASSWORD=$(POSTGRES_PASSWORD) \
FIRST_SUPERUSER=$(FIRST_SUPERUSER) \
FIRST_SUPERUSER_PASSWORD=$(FIRST_SUPERUSER_PASSWORD)"
follow: ## Start following the logs of frontend and backend services
docker-compose logs --follow backend frontend
push: ## Push docker images to docker hub - Needs DOCKER_PASSWORD environment variable
ifndef DOCKER_PASSWORD
$(error DOCKER_PASSWORD environment variable has to be set. It is $(DOCKER_USER)'s password)
endif
@echo Logging in
@echo "$(DOCKER_PASSWORD)" | docker login --username $(DOCKER_USER) --password-stdin
@echo Building images
cd $(FRONTEND_DIR) && docker build \
--build-arg URL=https://${DOMAIN} -t $(DOCKER_IMAGE_FRONTEND):latest .
cd $(BACKEND_DIR) && docker build -t $(DOCKER_IMAGE_BACKEND):latest .
@echo Tagging the images
$(eval TODAY=$(shell date +%Y-%m-%d-%H-%M)) \
docker tag $(DOCKER_IMAGE_FRONTEND):latest $(DOCKER_IMAGE_FRONTEND):$(TODAY) \
&& docker tag $(DOCKER_IMAGE_BACKEND):latest $(DOCKER_IMAGE_BACKEND):$(TODAY)
@echo Pushing the images
docker push $(DOCKER_IMAGE_FRONTEND):$(TODAY)
docker push $(DOCKER_IMAGE_FRONTEND):latest
docker push $(DOCKER_IMAGE_BACKEND):$(TODAY)
docker push $(DOCKER_IMAGE_BACKEND):latest
@echo Docker images are available at:
@echo " * https://hub.docker.com/repository/docker/$(DOCKER_IMAGE_FRONTEND)"
@echo " * https://hub.docker.com/repository/docker/$(DOCKER_IMAGE_BACKEND)"
stop: ## Stop the application
docker-compose down --remove-orphans
clean: ## Clean the application
clean:
$(MAKE_BACKEND) clean
$(MAKE_FRONTEND) clean
lint: ## Check the format of the whole application
lint:
$(MAKE_BACKEND) make_lint
$(MAKE_FRONTEND) lint
format: ## Reformat the code of the whole application
format:
$(MAKE_BACKEND) make_format
$(MAKE_FRONTEND) format