forked from konveyor/move2kube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
187 lines (152 loc) · 6.1 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# Copyright IBM Corporation 2020
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
GO_VERSION ?= $(shell go run ./scripts/detectgoversion/detect.go 2>/dev/null || printf '1.17')
BINNAME ?= move2kube
BINDIR := $(CURDIR)/bin
DISTDIR := $(CURDIR)/_dist
TARGETS := darwin/amd64 darwin/arm64 linux/amd64 linux/arm64 windows/amd64
REGISTRYNS := quay.io/konveyor
GOPATH = $(shell go env GOPATH)
GOX = $(GOPATH)/bin/gox
GOLINT = $(GOPATH)/bin/golint
GOTEST = ${GOPATH}/bin/gotest
GOLANGCILINT = $(GOPATH)/bin/golangci-lint
GOLANGCOVER = $(GOPATH)/bin/goveralls
PKG := ./...
LDFLAGS := -w -s
SRC = $(shell find . -type f -name '*.go' -print)
ASSETS = $(shell find assets -type f -name '*' -print)
ARCH = $(shell uname -p)
GIT_COMMIT = $(shell git rev-parse HEAD)
GIT_SHA = $(shell git rev-parse --short HEAD)
GIT_TAG = $(shell git describe --tags --abbrev=0 --exact-match 2>/dev/null)
GIT_DIRTY = $(shell test -n "`git status --porcelain`" && echo "dirty" || echo "clean")
HAS_UPX = $(shell command -v upx >/dev/null && echo true || echo false)
GOGET := cd / && GO111MODULE=on go get -u
ifdef VERSION
BINARY_VERSION = $(VERSION)
endif
BINARY_VERSION ?= ${GIT_TAG}
ifneq ($(BINARY_VERSION),)
LDFLAGS += -X github.com/konveyor/${BINNAME}/types/info.version=${BINARY_VERSION}
VERSION ?= $(BINARY_VERSION)
endif
VERSION ?= latest
VERSION_METADATA = unreleased
ifneq ($(GIT_TAG),)
VERSION_METADATA =
endif
LDFLAGS += -X github.com/konveyor/${BINNAME}/types/info.buildmetadata=${VERSION_METADATA}
LDFLAGS += -X github.com/konveyor/${BINNAME}/types/info.gitCommit=${GIT_COMMIT}
LDFLAGS += -X github.com/konveyor/${BINNAME}/types/info.gitTreeState=${GIT_DIRTY}
LDFLAGS += -extldflags "-static"
# HELP
# This will output the help for each task
.PHONY: help
help: ## This help.
@awk 'BEGIN {FS = ":.*?## "} /^[0-9a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
# -- Build --
.PHONY: build
build: get $(BINDIR)/$(BINNAME) ## Build go code
@printf "\033[32m-------------------------------------\n BUILD SUCCESS\n-------------------------------------\033[0m\n"
$(BINDIR)/$(BINNAME): $(SRC) $(ASSETS)
go build -ldflags '$(LDFLAGS)' -o $(BINDIR)/$(BINNAME) ./cmd/${BINNAME}
ifeq ($(HAS_UPX),true)
@echo 'upx detected. compressing binary...'
upx $(BINDIR)/$(BINNAME)
else
@echo 'For smaller binaries, please install upx:'
@echo 'MacOS: brew install upx'
@echo 'Linux: sudo apt-get install upx'
endif
mkdir -p $(GOPATH)/bin/
cp $(BINDIR)/$(BINNAME) $(GOPATH)/bin/
.PHONY: get
get: go.mod
go mod download
.PHONY: generate
generate:
go generate ${PKG}
.PHONY: deps
deps:
source scripts/installdeps.sh
# -- Test --
.PHONY: test
test: ## Run tests
go test -run . $(PKG) -race
@printf "\033[32m-------------------------------------\n TESTS PASSED\n-------------------------------------\033[0m\n"
${GOTEST}:
${GOGET} github.com/rakyll/gotest
.PHONY: test-verbose
test-verbose: ${GOTEST}
gotest -run . $(PKG) -race -v
${GOLANGCOVER}:
${GOGET} github.com/mattn/[email protected]
.PHONY: test-coverage
test-coverage: ${GOLANGCOVER} ## Run tests with coverage
go test -run . $(PKG) -coverprofile=coverage.txt -covermode=atomic
${GOLANGCILINT}:
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin v1.31.0
${GOLINT}:
${GOGET} golang.org/x/lint/golint
.PHONY: test-style
test-style: ${GOLANGCILINT} ${GOLINT}
${GOLANGCILINT} run --timeout 3m
${GOLINT} ${PKG}
scripts/licensecheck.sh
@printf "\033[32m-------------------------------------\n STYLE CHECK PASSED\n-------------------------------------\033[0m\n"
# -- CI --
.PHONY: ci
ci: clean build test test-style ## Run CI routine
# -- Release --
$(GOX):
${GOGET} github.com/mitchellh/[email protected]
.PHONY: build-cross
build-cross: $(GOX) clean
CGO_ENABLED=0 $(GOX) -parallel=3 -output="$(DISTDIR)/{{.OS}}-{{.Arch}}/$(BINNAME)" -osarch='$(TARGETS)' -ldflags '$(LDFLAGS)' ./cmd/${BINNAME}
.PHONY: dist
dist: clean build-cross ## Build distribution
ifeq ($(HAS_UPX),true)
@echo 'upx detected. compressing binary...'
upx $(shell find . -type f -name '$(BINNAME)')
else
@echo 'For smaller binaries, please install upx:'
@echo 'MacOS: brew install upx'
@echo 'Linux: sudo apt-get install upx'
endif
mkdir -p $(DISTDIR)/files
cp -r ./LICENSE ./scripts/installdeps.sh ./USAGE.md ./samples $(DISTDIR)/files/
cd $(DISTDIR) && go run ../scripts/dist/builddist.go -b $(BINNAME) -v $(VERSION)
.PHONY: clean
clean:
rm -rf $(BINDIR) $(DISTDIR)
go clean -cache
.PHONY: info
info: ## Get version info
@echo "Version: ${VERSION}"
@echo "Git Tag: ${GIT_TAG}"
@echo "Git Commit: ${GIT_COMMIT}"
@echo "Git Tree State: ${GIT_DIRTY}"
# -- Docker --
.PHONY: cbuild
cbuild: ## Build docker image
docker build -t ${REGISTRYNS}/${BINNAME}-builder:${VERSION} --cache-from ${REGISTRYNS}/${BINNAME}-builder:latest --target build_base --build-arg VERSION=${VERSION} --build-arg GO_VERSION=${GO_VERSION} .
docker build -t ${REGISTRYNS}/${BINNAME}:${VERSION} --cache-from ${REGISTRYNS}/${BINNAME}-builder:latest --cache-from ${REGISTRYNS}/${BINNAME}:latest --build-arg VERSION=${VERSION} --build-arg GO_VERSION=${GO_VERSION} .
docker tag ${REGISTRYNS}/${BINNAME}-builder:${VERSION} ${REGISTRYNS}/${BINNAME}-builder:latest
docker tag ${REGISTRYNS}/${BINNAME}:${VERSION} ${REGISTRYNS}/${BINNAME}:latest
.PHONY: cpush
cpush: ## Push docker image
# To help with reusing layers and hence speeding up build
docker push ${REGISTRYNS}/${BINNAME}-builder:${VERSION}
docker push ${REGISTRYNS}/${BINNAME}:${VERSION}