-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakefile
147 lines (125 loc) · 5.68 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
146
147
PROJECT_NAME := $(notdir $(CURDIR))
BINARY_NAME := $(PROJECT_NAME)
PROJECT_ROOT := $(CURDIR)
BACKEND_SOURCE_DIR := $(PROJECT_ROOT)/app
FRONTEND_SOURCE_DIR := $(PROJECT_ROOT)/web/prod
BUILD_DIR := $(PROJECT_ROOT)/build
CURRENT_BUILD_DIR := $(BUILD_DIR)/Goptivum
DIST_DIR := $(CURRENT_BUILD_DIR)/dist
DEPLOY_HOST ?= razul-server.local
DEPLOY_USER ?= goptivum
DEPLOY_DIR ?= ~/$(PROJECT_NAME)
DEPLOY_SERVICE_NAME ?= goptivum.service
VERSION := $(shell grep '^VERSION=' $(PROJECT_ROOT)/.env | cut -d '=' -f 2)
ARCHS := amd64 arm
OS := linux
DOCKER_IMAGE_NAME := $(shell echo $(PROJECT_NAME) | tr '[:upper:]' '[:lower:]'):$(VERSION)
DOCKER_COMPOSE := docker-compose
DOCKER_COMPOSE_FILE := $(PROJECT_ROOT)/docker-compose.yml
DOCKER_PACKAGE_NAME := goptivum
DOCKERHUB_USERNAME ?= smeggmann99
.PHONY: all
all: build build-web-install
.PHONY: build
build: | $(CURRENT_BUILD_DIR)
@echo "Building backend..."
@CGO_ENABLED=0 GOOS=$(OS) GOARCH=amd64 go build -a -installsuffix cgo -o $(CURRENT_BUILD_DIR)/$(BINARY_NAME) $(BACKEND_SOURCE_DIR)
@cp -r $(BACKEND_SOURCE_DIR)/config.json $(CURRENT_BUILD_DIR)/config.json
@cp -r $(BACKEND_SOURCE_DIR)/.env $(CURRENT_BUILD_DIR)/.env
.PHONY: build-web
build-web: | $(CURRENT_BUILD_DIR)
@echo "Building frontend..."
@cd $(FRONTEND_SOURCE_DIR) && npm run build
@mkdir -p $(DIST_DIR)
@cp -r $(FRONTEND_SOURCE_DIR)/dist/* $(DIST_DIR)
.PHONY: build-web-install
build-web-install: | $(CURRENT_BUILD_DIR)
@echo "Building frontend and installing dependencies..."
@cd $(FRONTEND_SOURCE_DIR) && npm install && npm run build
@mkdir -p $(DIST_DIR)
@cp -r $(FRONTEND_SOURCE_DIR)/dist/* $(DIST_DIR)
.PHONY: package
package: all
@echo "Packaging tarballs for version $(VERSION)..."
@for arch in $(ARCHS); do \
BUILD_ARCHIVE=$(BUILD_DIR)/$(shell echo $(PROJECT_NAME) | tr '[:upper:]' '[:lower:]')-$(VERSION)-$(OS)-static-$$arch.tar.gz; \
echo "Building for $$arch..."; \
TEMP_DIR=$(BUILD_DIR)/temp_$$arch; \
mkdir -p $$TEMP_DIR/Goptivum; \
GOOS=$(OS) GOARCH=$$arch CGO_ENABLED=0 go build -a -installsuffix cgo -o $$TEMP_DIR/Goptivum/$(BINARY_NAME) $(BACKEND_SOURCE_DIR); \
cp -r $(CURRENT_BUILD_DIR)/config.json $$TEMP_DIR/Goptivum/config.json; \
sed 's/^OPENWEATHER_API_KEY=.*/OPENWEATHER_API_KEY=""/' $(CURRENT_BUILD_DIR)/.env > $$TEMP_DIR/Goptivum/.env; \
cp -r $(DIST_DIR) $$TEMP_DIR/Goptivum/dist; \
echo "Creating archive $$BUILD_ARCHIVE..."; \
tar -czvf $$BUILD_ARCHIVE -C $$TEMP_DIR Goptivum; \
rm -rf $$TEMP_DIR; \
done
@echo "Packaging complete. Tarballs are in $(BUILD_DIR)"
.PHONY: docker-build
docker-build:
@echo "Building Docker images with version $(VERSION)..."
@VERSION=$(VERSION) $(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) build
.PHONY: docker-up
docker-up:
@echo "Starting Docker containers..."
@$(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) up -d
.PHONY: docker-down
docker-down:
@echo "Stopping Docker containers..."
@$(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) down
.PHONY: docker-clean
docker-clean:
@echo "Removing Docker containers, images, and volumes..."
@$(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) down --rmi all --volumes --remove-orphans
.PHONY: docker-publish
docker-publish: docker-build
@echo "Tagging Docker image for publishing..."
@docker tag $(DOCKERHUB_USERNAME)/$(DOCKER_PACKAGE_NAME):$(VERSION) $(DOCKERHUB_USERNAME)/$(DOCKER_PACKAGE_NAME):$(VERSION)
@docker tag $(DOCKERHUB_USERNAME)/$(DOCKER_PACKAGE_NAME):$(VERSION) $(DOCKERHUB_USERNAME)/$(DOCKER_PACKAGE_NAME):latest
@echo "Pushing Docker image to Docker Hub..."
@docker push $(DOCKERHUB_USERNAME)/$(DOCKER_PACKAGE_NAME):$(VERSION)
@docker push $(DOCKERHUB_USERNAME)/$(DOCKER_PACKAGE_NAME):latest
.PHONY: docker-build-publish
docker-build-publish:
@echo "Building and publishing multi-architecture Docker images..."
@docker buildx build \
--platform linux/amd64,linux/arm64,linux/arm \
--build-arg VERSION=$(VERSION) \
--tag $(DOCKERHUB_USERNAME)/$(DOCKER_PACKAGE_NAME):$(VERSION) \
--tag $(DOCKERHUB_USERNAME)/$(DOCKER_PACKAGE_NAME):latest \
--push \
-f $(PROJECT_ROOT)/dockerfile $(PROJECT_ROOT)
@echo "Docker images built and published for architectures: amd64, arm64, arm."
.PHONY: clean
clean:
@echo "Cleaning up build files..."
@rm -rf $(BUILD_DIR)
.PHONY: clean-tarballs
clean-tarballs:
@echo "Cleaning up tarballs..."
@find $(BUILD_DIR) -type f -name '*.tar.gz' -delete
.PHONY: deploy
deploy: all
@echo "Deploying to $(DEPLOY_HOST)..."
@rsync -avz --delete $(BUILD_DIR)/ $(DEPLOY_USER)@$(DEPLOY_HOST):$(DEPLOY_DIR)
@ssh -t $(DEPLOY_USER)@$(DEPLOY_HOST) 'sudo systemctl restart $(DEPLOY_SERVICE_NAME) && sudo systemctl reload nginx'
@echo "Deployment complete."
.PHONY: help
help:
@echo "Usage:"
@echo " make Build the Go backend and Vue.js frontend and install NPM dependencies"
@echo " make build Build the Go backend"
@echo " make build-web Build the Vue.js frontend without installing NPM dependencies"
@echo " make build-web-install Build the Vue.js frontend and install NPM dependencies"
@echo " make package Build and package the application for all architectures"
@echo " make docker-build Build Docker images"
@echo " make docker-up Start Docker containers"
@echo " make docker-down Stop Docker containers"
@echo " make docker-clean Clean up Docker containers, images, and volumes"
@echo " make docker-publish Build and publish Docker images to Docker Hub"
@echo " make deploy Deploy the application to the remote server"
@echo " make clean Clean up build files"
@echo " make clean-tarballs Clean up tarball archives"
@echo " make help Show this help message"
$(CURRENT_BUILD_DIR):
@mkdir -p $(CURRENT_BUILD_DIR)