-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
102 lines (91 loc) · 3.08 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
.PHONY: all build test clean
# Use Gitlab CI/CD environment variables
CI_COMMIT_SHORT_SHA?=$(shell git log -1 --pretty=format:"%h")
CI_REGISTRY_IMAGE?=registry.gitlab.com/samtx/passpredict-api
COMMIT_TAG=passpredict:$(CI_COMMIT_SHORT_SHA)
LOCAL_TAG=passpredict:latest
REMOTE_TAG=$(CI_REGISTRY_IMAGE)/$(LOCAL_TAG)
CONTAINER_NAME=passpredict
TOKEN=GITLAB_PAT_PASSPREDICT_REGISTRY
login:
printf '%s\n' "$$$(TOKEN)" | docker login -u samtx --password-stdin registry.gitlab.com
ssh-cmd:
ssh [email protected] "$(CMD)"
build:
@echo "Build docker image"
docker build -t $(LOCAL_TAG) -t $(COMMIT_TAG) --label git-commit=$(CI_COMMIT_SHORT_SHA) .
@echo "View docker image size"
docker image ls passpredict:latest
push:
docker tag $(LOCAL_TAG) $(REMOTE_TAG)
docker push $(REMOTE_TAG)
# create initial docker volume. see link: https://github.com/moby/moby/issues/25245#issuecomment-365970076
# docker create volume passpredict-api
deploy-local:
@echo "Removing old container..."
-docker container stop $(CONTAINER_NAME)
-docker container rm $(CONTAINER_NAME)
@echo "starting new container..."
docker run -d --name $(CONTAINER_NAME) \
-p 8000:8000 \
-e REDIS_HOST=redis \
-e DT_SECONDS=5 \
-e DATABASE_URI=sqlite:////db/passpredict.sqlite \
--link=redis:redis \
-v passpredict-api-db:/db \
$(LOCAL_TAG)
deploy-local-foreground:
@echo "Removing old container..."
-docker container stop $(CONTAINER_NAME)
-docker container rm $(CONTAINER_NAME)
@echo "starting new container in foreground..."
docker run --name $(CONTAINER_NAME) \
-p 8000:8000 \
--network host \
--env-file=/home/sam/passpredict-api/.env-docker \
$(LOCAL_TAG)
migrate:
@echo "Migrating production database..."
@$(MAKE) ssh-cmd CMD='\
docker run \
--env-file=/opt/passpredict/.env-docker \
$(REMOTE_TAG) \
alembic upgrade head \
'
@echo "Current database revision:"
@$(MAKE) ssh-cmd CMD='\
docker run \
--env-file=/opt/passpredict/.env-docker \
$(REMOTE_TAG) \
alembic current \
'
deploy:
@echo "Login to container registry on server..."
@$(MAKE) ssh-cmd CMD='docker login \
-u $(PASSPREDICTAPI_GITLAB_DEPLOY_USER) \
-p $(PASSPREDICTAPI_GITLAB_DEPLOY_PASSWORD) \
registry.gitlab.com \
'
@echo "pulling new container image..."
$(MAKE) ssh-cmd CMD='docker pull $(REMOTE_TAG)'
@echo "Build static files locally"
npm run build
@echo "Upload static files to remote server"
rsync -vzrgo --chown=root:web --delete static/ [email protected]:/var/www/passpredict.com/
@echo "Stopping old container..."
-$(MAKE) ssh-cmd CMD='docker container stop $(CONTAINER_NAME)'
@echo "Remove old container..."
-$(MAKE) ssh-cmd CMD='docker container rm $(CONTAINER_NAME)'
@echo "starting new container..."
@$(MAKE) ssh-cmd CMD='\
docker run -d --name $(CONTAINER_NAME) \
-p 8001:8000 \
--restart=always \
--add-host host.docker.internal:host-gateway \
--env-file=/opt/passpredict/.env-docker \
--log-driver=journald \
$(REMOTE_TAG) \
'
@echo "Remove old images and containers..."
@$(MAKE) ssh-cmd CMD='docker image prune -a -f'
@$(MAKE) ssh-cmd CMD='docker container prune -f --filter "until=24h"'