-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
122 lines (94 loc) · 4.01 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
# Include variable form the .envrc file
include .envrc
# --------------------------------------------------------------------------------------------------------------#
# HELPERS -------------------------------------------------------------------------------------------------------------#
## help:print this help message
.PHONY: help
help:
@echo 'Usage:'
@sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/ /'
.PHONY: confirm
confirm:
@echo -n "Are you Sure? [y/N]" && read ans && [ $${ans:-N} = y ]
# --------------------------------------------------------------------------------------------------------------#
# Development
# --------------------------------------------------------------------------------------------------------------#
## run/api: run the cmd/api application
.PHONY: run/api
run/api:
go run ./cmd/api/ -db-dsn=${CINLIM_DB_DSN}
## db/psql: connect to the postgresql database using psql
.PHONY : db/psql
db/psql:
psql ${CINLIM_DB_DSN}
## db/migration/new: create a new set of database migration
.PHONY: db/migration/new
db/migrations/new:
@echo 'Creating migration file for ${name}'
migrate create -ext sql -dir ./migration -seq ${name}
## db/migration/up : apply all up database migration
.PHONY: db/migration/up
db/migration/up: confirm
@echo 'Running up migrations...'
migrate -path ./migration -database ${CINLIM_DB_DSN} up
#
# --------------------------------------------------------------------------------------------------------------#
# Quality CONTROL
# --------------------------------------------------------------------------------------------------------------#
.PHONY: audit
audit: vendor
@echo 'Formatting code...'
go fmt ./...
@echo 'Vetting code...'
go vet ./...
staticcheck ./...
@echo 'Running tests...'
go test -race -vet=off ./...
.PHONY: vendor
vendor:
@echo "Tidying and veriying module dependencies"
go mod tidy
go mod verify
@echo "Vendoring Dependencies"
go mod vendor
# --------------------------------------------------------------------------------------------------------------#
# BUILD
# --------------------------------------------------------------------------------------------------------------#
current_time = $(shell date +%Y-%m-%dT%H:%M:%S )
git_description = $(shell git describe --always --dirty --tags --long)
linker_flags = '-s -X main.buildTime=${current_time} -X main.version=${git_description}'
.PHONY: build/api
build/api:
@echo 'Building cmd/api'
go build -a -ldflags=${linker_flags} -o=./bin/api ./cmd/api
GOOS=linux GOARCH=amd64 go build -ldflags=${linker_flags} -o=./bin/linux_amd64/api ./cmd/api
# --------------------------------------------------------------------------------------------------------------#
# PRODUCTION
# --------------------------------------------------------------------------------------------------------------#
production_host_ip = '20.244.47.212'
## productin/connect : connect to the production server
.PHONY : production/connect
production/connect:
ssh azureuser@${production_host_ip}
## production/deploy/api :deploy api to production
.PHONY: production/deploy/api
production/deploy/api:
rsync -rP ./bin/linux_amd64/api ./migration/ azureuser@${production_host_ip}:~/application/cinlim
ssh -t azureuser@${production_host_ip} 'migrate -path ./migration -database $$CINLIM_DB_DSN up'
## production/configure/api.service: configure the production systemd api.service file
.PHONY: production/configure/api.service
production/configure/api.service:
rsync -P ./remote/production/api.service azureuser@${production_host_ip}:~
ssh -t azureuser@${production_host_ip} '\
sudo mv ~/api.service /etc/systemd/system/ \
&& sudo systemctl enable api \
&& sudo systemctl restart api \
'
## production/configure/caddyfile : configure the production caddyfile
.PHONY: production/configure/caddyfile
production/configure/caddyfile:
rsync -P ./remote/production/Caddyfile azureuser@${production_host_ip}:~
ssh -t azureuser@${production_host_ip} '\
sudo mv ~/Caddyfile /etc/caddy/ \
&& sudo systemctl reload caddy\
'