-
Notifications
You must be signed in to change notification settings - Fork 4
/
Makefile
173 lines (141 loc) · 5.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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
VERSION=$(shell cat version)
# Docker options
TARGET_DOCKER_REGISTRY ?= $$USER
# 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_GENERATE=$(GO_CMD) generate
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: generate
# Generate the code objects.
generate:
@echo "Generating golang extra files"
@$(GO_GENERATE) -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 "Building local 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 "Building 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: artifacts
artifacts: clean $(addsuffix .pkg-darwin,$(BUILD_TARGETS)) $(addsuffix .pkg-linux,$(BUILD_TARGETS))
%.pkg-darwin: %.darwin
@echo "Packaging darwin binary"
@mkdir -p build/compressed/darwin/$(basename $@)
@mv build/bin/darwin/* build/compressed/darwin/$(basename $@)/.
@cp README_public.md build/compressed/darwin/$(basename $@)/README.md
@cd build/compressed/darwin && tar cvzf playground_$(VERSION).tgz playground
%.pkg-linux: %.linux
@echo "Packaging linux binary"
@mkdir -p build/compressed/linux/$(basename $@)
@mv build/bin/linux/* build/compressed/linux/$(basename $@)/.
@cp README_public.md build/compressed/linux/$(basename $@)/README.md
@cd build/compressed/linux && tar cvzf playground_$(VERSION).tgz playground
.PHONY: docker-prep
docker-prep: $(addsuffix .docker-prep, $(BUILD_TARGETS))
%.docker-prep: %.linux
@echo "Preparing docker file for "$(basename $@);
@rm -r $(DOCKER_FOLDER)/$(basename $@) || true;
@mkdir -p $(DOCKER_FOLDER)/$(basename $@);
@cp docker/$(basename $@)/* $(DOCKER_FOLDER)/$(basename $@)/.;
@mkdir -p $(DOCKER_FOLDER)/$(basename $@)/assets;
@cp -R assets/* $(DOCKER_FOLDER)/$(basename $@)/assets;
@cp -R cmd/* $(DOCKER_FOLDER)/$(basename $@)/cmd;
@cp go.* $(DOCKER_FOLDER)/$(basename $@)/;
@cp Makefile* $(DOCKER_FOLDER)/$(basename $@)/;
@cp version* $(DOCKER_FOLDER)/$(basename $@)/;
.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 --build-arg VERSION=$(VERSION) $(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)/. ; \
cp deployments/README.md $(K8S_FOLDER)/. ; \
$(SED) -i 's/TARGET_DOCKER_REGISTRY/vkunal/' $(K8S_FOLDER)/*.yaml ;\
$(SED) -i 's/VERSION/$(VERSION)/' $(K8S_FOLDER)/*.yaml ;\
echo "Kubernetes OAM files ready at $(K8S_FOLDER)/"; \
fi
.PHONY: release
release: clean build-darwin build-linux
@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"