-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
94 lines (75 loc) · 2.65 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
PROJECT_ID=debby-hand-knits
ZONE=us-central1-a
run-local:
docker-compose up
###
create-tf-backend-bucket:
gsutil mb -p $(PROJECT_ID) gs://$(PROJECT_ID)-terraform
###
check-env:
ifndef ENV
$(error Please set ENV=[staging|prod])
endif
# This cannot be indented or else make will include spaces in front of secret
define get-secret
$(shell gcloud secrets versions access latest --secret=$(1) --project=$(PROJECT_ID))
endef
###
terraform-create-workspace: check-env
cd terraform && \
terraform workspace new $(ENV)
terraform-init: check-env
cd terraform && \
terraform workspace select $(ENV) && \
terraform init
TF_ACTION?=plan
terraform-action: check-env
@cd terraform && \
terraform workspace select $(ENV) && \
terraform $(TF_ACTION) \
-var-file="./environments/common.tfvars" \
-var-file="./environments/$(ENV)/config.tfvars" \
-var="mongodbatlas_private_key=$(call get-secret,atlas_private_key)" \
-var="atlas_user_password=$(call get-secret,atlas_user_password_$(ENV))" \
-var="cloudflare_api_token=$(call get-secret,cloudflare_api_token)"
###
SSH_STRING=koitoror@debby-hand-knits-vm-$(ENV)
OAUTH_CLIENT_ID=542106262510-8ki8hqgu7kmj2b3arjdqvcth3959kmmv.apps.googleusercontent.com
GITHUB_SHA?=latest
LOCAL_TAG=debby-app:$(GITHUB_SHA)
# REMOTE_TAG=gcr.io/$(PROJECT_ID)/$(LOCAL_TAG)
REMOTE_TAG=ghcr.io/koitoror/$(PROJECT_ID)/$(LOCAL_TAG)
CONTAINER_NAME=debby-hand-knits-api
DB_NAME=debby-hand-knits
ssh: check-env
gcloud compute ssh $(SSH_STRING) \
--project=$(PROJECT_ID) \
--zone=$(ZONE)
ssh-cmd: check-env
@gcloud compute ssh $(SSH_STRING) \
--project=$(PROJECT_ID) \
--zone=$(ZONE) \
--command="$(CMD)"
build:
docker build -t $(LOCAL_TAG) .
push:
docker tag $(LOCAL_TAG) $(REMOTE_TAG)
docker push $(REMOTE_TAG)
deploy: check-env
$(MAKE) ssh-cmd CMD='docker-credential-gcr configure-docker'
@echo "pulling new container image..."
$(MAKE) ssh-cmd CMD='docker pull $(REMOTE_TAG)'
@echo "removing old container..."
-$(MAKE) ssh-cmd CMD='docker container stop $(CONTAINER_NAME)'
-$(MAKE) ssh-cmd CMD='docker container rm $(CONTAINER_NAME)'
@echo "starting new container..."
@$(MAKE) ssh-cmd CMD='\
docker run -d --name=$(CONTAINER_NAME) \
--restart=unless-stopped \
-p 80:3000 \
-e PORT=3000 \
-e \"MONGO_URI=mongodb+srv://debby-hand-knits-user-$(ENV):$(call get-secret,atlas_user_password_$(ENV))@debby-hand-knits-$(ENV)cluster0.gps3l.mongodb.net/$(DB_NAME)?retryWrites=true&w=majority\" \
-e GOOGLE_CLIENT_ID=$(OAUTH_CLIENT_ID) \
-e GOOGLE_CLIENT_SECRET=$(call get-secret,google_oauth_client_secret) \
$(REMOTE_TAG) \
'