-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
154 lines (119 loc) · 4.63 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
148
149
150
151
152
153
154
include Makefile.const
#Config variables
#Override this varible if you want to work with a specific target.
BUILD_TARGETS?=$(CURRENT_BIN_TARGETS)
#Version must be overrided in the CI
VERSION?=local
# Docker options
TARGET_DOCKER_REGISTRY ?= $$USER
# Kubernetes options
TARGET_K8S_NAMESPACE ?= napptive
# Variables
BUILD_FOLDER=$(CURDIR)/build
BIN_FOLDER=$(BUILD_FOLDER)/bin
DOCKER_FOLDER=$(BUILD_FOLDER)/docker
K8S_FOLDER=$(BUILD_FOLDER)/k8s
# Obtain the last commit hash
COMMIT=$(shell git log -1 --pretty=format:"%H")
# Tools
GO_CMD=go
GO_BUILD=$(GO_CMD) build
GO_TEST=$(GO_CMD) test
GO_LDFLAGS=-ldflags "-X main.Version=$(VERSION) -X=main.Commit=$(COMMIT)"
UNAME := $(shell uname)
ifeq ($(UNAME), Darwin)
SED := gsed
else
SED := sed
endif
#Docker command
DOCKERCMD?=docker
.PHONY: all
all: clean test build
.PHONY: clean
# Remove build files
clean:
@echo "Cleaining build folder: $(BUILD_FOLDER)"
@rm -rf $(BUILD_FOLDER)
.PHONY: test
# Test all golang files in the curdir
test:
@echo "Executing golang tests"
@$(GO_TEST) -v ./...
.PHONY: coverage
# Create a coverage report for all golang files in the curdir
coverage:
@echo "Creating golang test coverage report: $(BUILD_FOLDER)/coverage.out"
@mkdir -p $(BUILD_FOLDER)
@$(GO_TEST) -v ./... -coverprofile=$(BUILD_FOLDER)/cover.out
.PHONY: build
# Build target for local environment default
build: $(addsuffix .local,$(BUILD_TARGETS))
.PHONY: build-darwin
# Build target for darwin
build-darwin: $(addsuffix .darwin,$(BUILD_TARGETS))
.PHONY: build-linux
# Build target for linux
build-linux: $(addsuffix .linux,$(BUILD_TARGETS))
# Trigger the build operation for the local environment. Notice that the suffix is removed.
%.local:
@echo "Build darwin binary $@"
@$(GO_BUILD) $(GO_LDFLAGS) -o $(BIN_FOLDER)/local/$(basename $@) ./cmd/$(basename $@)/main.go
# Trigger the build operation for darwin. Notice that the suffix is removed as it is only used for Makefile expansion purposes.
%.darwin:
@echo "Build darwin binary $@"
@GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 $(GO_BUILD) $(GO_LDFLAGS) -o $(BIN_FOLDER)/darwin/$(basename $@) ./cmd/$(basename $@)/main.go
# Trigger the build operation for linux. Notice that the suffix is removed as it is only used for Makefile expansion purposes.
%.linux:
@ echo "Building linux binary $@"
@GOOS=linux GOARCH=amd64 CGO_ENABLED=0 $(GO_BUILD) $(GO_LDFLAGS) -o $(BIN_FOLDER)/linux/$(basename $@) ./cmd/$(basename $@)/main.go
.PHONY: docker-prep
docker-prep: $(addsuffix .docker-prep, $(BUILD_TARGETS))
%.docker-prep: %.linux
@if [ -f docker/$(basename $@)/Dockerfile ]; then\
echo "Preparing docker file for "$(basename $@);\
rm -r $(DOCKER_FOLDER)/$(basename $@) || true;\
mkdir -p $(DOCKER_FOLDER)/$(basename $@);\
cp docker/$(basename $@)/* $(DOCKER_FOLDER)/$(basename $@)/.;\
cp $(BIN_FOLDER)/linux/$(basename $@) $(DOCKER_FOLDER)/$(basename $@)/.;\
fi
.PHONY: docker-build
docker-build: $(addsuffix .docker-build, $(BUILD_TARGETS))
%.docker-build: %.docker-prep
@if [ -f $(DOCKER_FOLDER)/$(basename $@)/Dockerfile ]; then\
echo "Building docker file for "$(basename $@);\
$(DOCKERCMD) build $(DOCKER_FOLDER)/$(basename $@) -t $(TARGET_DOCKER_REGISTRY)/$(basename $@):$(VERSION);\
fi
.PHONY: docker-push
docker-push: $(addsuffix .docker-push, $(BUILD_TARGETS))
%.docker-push: %.docker-build
@echo Pushing $(basename $@) Docker image to DockerHub
@if [ -f $(DOCKER_FOLDER)/$(basename $@)/Dockerfile ]; then\
{ $(DOCKERCMD) push $(TARGET_DOCKER_REGISTRY)/$(basename $@):$(VERSION) || exit 1; } ; \
fi
.PHONY: k8s
k8s:
@if [ ! -d "deployments" ]; then \
echo "Skipping k8s, no deployments found"; exit 0;\
else \
rm -r $(K8S_FOLDER) || true ; \
mkdir -p $(K8S_FOLDER); \
cp deployments/*.yaml $(K8S_FOLDER)/. ; \
$(SED) -i 's/TARGET_K8S_NAMESPACE/$(TARGET_K8S_NAMESPACE)/' $(K8S_FOLDER)/*.yaml ;\
$(SED) -i 's/TARGET_DOCKER_REGISTRY/'$(TARGET_DOCKER_REGISTRY)'/' $(K8S_FOLDER)/*.yaml ;\
$(SED) -i 's/VERSION/$(VERSION)/' $(K8S_FOLDER)/*.yaml ;\
echo "Kubernetes files ready at $(K8S_FOLDER)/"; \
fi
.PHONY: release
release: clean build-darwin build-linux k8s
@mkdir -p $(BUILD_FOLDER)
@cp README.md $(BUILD_FOLDER)
@if [ -d "deployments" ]; then \
tar -czvf $(BUILD_FOLDER)/$(PROJECT_NAME)_$(VERSION).tar.gz -C $(BUILD_FOLDER) bin k8s README.md; \
elif [ -d $(BIN_FOLDER) ]; then \
tar -czvf $(BUILD_FOLDER)/$(PROJECT_NAME)_$(VERSION).tar.gz -C $(BUILD_FOLDER) bin README.md; \
else \
tar -czvf $(BUILD_FOLDER)/$(PROJECT_NAME)_$(VERSION).tar.gz -C $(BUILD_FOLDER) README.md; \
fi
@echo "::set-output name=release_file::$(BUILD_FOLDER)/$(PROJECT_NAME)_$(VERSION).tar.gz"
@echo "::set-output name=release_name::$(PROJECT_NAME)_$(VERSION).tar.gz"