forked from bank-vaults/bank-vaults
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
226 lines (174 loc) · 6.6 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# A Self-Documenting Makefile: http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
OS = $(shell uname)
export PATH := $(abspath bin/):${PATH}
# Docker variables
DOCKER_TAG ?= ${VERSION}
DOCKER_BUILD_EXTRA_ARGS ?=
# Export HOST_NETWORK=1 if you want to build the docker images with host network (useful when using some VPNs)
ifeq (${HOST_NETWORK}, 1)
DOCKER_BUILD_EXTRA_ARGS += --network host
endif
# Project variables
PACKAGE = github.com/bank-vaults/bank-vaults
BINARY_NAME ?= bank-vaults
DOCKER_REGISTRY ?= ghcr.io/bank-vaults
DOCKER_IMAGE = ${DOCKER_REGISTRY}/bank-vaults
WEBHOOK_DOCKER_IMAGE = ${DOCKER_REGISTRY}/vault-secrets-webhook
CONTAINER_IMAGE_REF = ghcr.io/bank-vaults/bank-vaults:dev
# Build variables
BUILD_DIR ?= build
BUILD_PACKAGE = ${PACKAGE}/cmd/...
VERSION ?= $(shell echo `git symbolic-ref -q --short HEAD || git describe --tags --exact-match` | tr '[/]' '-')
COMMIT_HASH ?= $(shell git rev-parse --short HEAD 2>/dev/null)
BUILD_DATE ?= $(shell date +%FT%T%z)
LDFLAGS += -X main.version=${VERSION} -X main.commitHash=${COMMIT_HASH} -X main.buildDate=${BUILD_DATE}
export CGO_ENABLED ?= 1
export GOOS = $(shell go env GOOS)
ifeq (${VERBOSE}, 1)
GOARGS += -v
endif
############################
##@ General
# Targets commented with ## will be visible in "make help" info.
# Comments marked with ##@ will be used as categories for a group of targets.
.PHONY: help
default: help
help: ## Display this help
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
##@ Build
.PHONY: build
build: ## Build binary
@mkdir -p build
go build -race -o build/ ./cmd/bank-vaults
.PHONY: build-debug
build-debug: GOARGS += -gcflags "-N -l"
build-debug: BINARY_NAME := ${BINARY_NAME}-debug
build-debug: build ## Build binary in debug mode
.PHONY: build-docker
build-docker: ## Build binary using Docker
docker run -it -v $(PWD):/go/src/${PACKAGE} -w /go/src/${PACKAGE} golang:${GOLANG_VERSION}-alpine go build -o ${BINARY_NAME}_linux ${BUILD_PACKAGE}
.PHONY: docker
docker: ## Build a Docker image
docker build ${DOCKER_BUILD_EXTRA_ARGS} -t ${DOCKER_IMAGE}:${DOCKER_TAG} -f Dockerfile .
ifeq (${DOCKER_LATEST}, 1)
docker tag ${DOCKER_IMAGE}:${DOCKER_TAG} ${DOCKER_IMAGE}:latest
endif
.PHONY: docker-push
docker-push: ## Push a Docker image
docker push ${DOCKER_IMAGE}:${DOCKER_TAG}
ifeq (${DOCKER_LATEST}, 1)
docker push ${DOCKER_IMAGE}:latest
endif
.PHONY: artifacts
artifacts: ## Build binary and Docker image
artifacts: build docker
.PHONY: clean
clean: ## Clear the working area and the project
rm -rf ${BUILD_DIR}/ bin/ vendor/
##@ Checks
.PHONY: check
check: lint test-integration ## Run tests and linters
.PHONY: fmt
fmt: ## Format code
$(GOLANGCI_LINT_BIN) run --fix
.PHONY: lint
lint: lint-go lint-docker lint-yaml
lint: ## Run linters
.PHONY: lint-go
lint-go:
$(GOLANGCI_LINT_BIN) run $(if ${CI},--out-format github-actions,)
.PHONY: lint-docker
lint-docker:
$(HADOLINT_BIN) Dockerfile
.PHONY: lint-yaml
lint-yaml:
$(YAMLLINT_BIN) $(if ${CI},-f github,) --no-warnings .
.PHONY: license-check
license-check: ## Run license check
$(LICENSEI_BIN) check
$(LICENSEI_BIN) header
##@ Tests
TEST_PKGS ?= ./...
TEST_REPORT_NAME ?= results.xml
# Dependency versions
GOTESTSUM_VERSION = 0.4.0
GOLANG_VERSION = 1.21.1
.PHONY: test
test: TEST_REPORT ?= main
test: TEST_FORMAT ?= short
test: SHELL = /bin/bash
test: bin/gotestsum ## Run tests
@mkdir -p ${BUILD_DIR}/test_results/${TEST_REPORT}
bin/gotestsum --no-summary=skipped --junitfile ${BUILD_DIR}/test_results/${TEST_REPORT}/${TEST_REPORT_NAME} --format ${TEST_FORMAT} -- $(filter-out -v,${GOARGS}) $(if ${TEST_PKGS},${TEST_PKGS},./...)
.PHONY: test-%
test-%: ## Run a specific test suite
@${MAKE} VERBOSE=0 GOTAGS=$* test
.PHONY: test-all
test-all: ## Run all tests
@${MAKE} GOARGS="${GOARGS} -run .\*" TEST_REPORT=all test
.PHONY: test-integration
test-integration: ## Run integration tests
@${MAKE} GOARGS="${GOARGS} -tags=integration" TEST_REPORT=integration test
bin/gotestsum: bin/gotestsum-${GOTESTSUM_VERSION}
@ln -sf gotestsum-${GOTESTSUM_VERSION} bin/gotestsum
bin/gotestsum-${GOTESTSUM_VERSION}:
@mkdir -p bin
curl -L https://github.com/gotestyourself/gotestsum/releases/download/v${GOTESTSUM_VERSION}/gotestsum_${GOTESTSUM_VERSION}_${OS}_amd64.tar.gz | tar -zOxf - gotestsum > ./bin/gotestsum-${GOTESTSUM_VERSION} && chmod +x ./bin/gotestsum-${GOTESTSUM_VERSION}
bin/jq: bin/jq-${JQ_VERSION}
@ln -sf jq-${JQ_VERSION} bin/jq
bin/jq-${JQ_VERSION}:
@mkdir -p bin
ifeq (${OS}, Darwin)
curl -L https://github.com/stedolan/jq/releases/download/jq-${JQ_VERSION}/jq-osx-amd64 > ./bin/jq-${JQ_VERSION} && chmod +x ./bin/jq-${JQ_VERSION}
endif
ifeq (${OS}, Linux)
curl -L https://github.com/stedolan/jq/releases/download/jq-${JQ_VERSION}/jq-linux64 > ./bin/jq-${JQ_VERSION} && chmod +x ./bin/jq-${JQ_VERSION}
endif
# Variable outputting/exporting rules
var-%: ; @echo $($*)
varexport-%: ; @echo $*=$($*)
##@ Release
release-%: ## Tag a new release
git tag -m 'Release $*' $*
@echo "Version updated to $*!"
@echo
@echo "To push the changes execute the following:"
@echo
@echo "git push; git push origin $*"
.PHONY: patch
patch: ## Tag a new patch version
@${MAKE} release-$(shell git describe --abbrev=0 --tags | awk -F'[ .]' '{print $$1"."$$2"."$$3+1}')
.PHONY: minor
minor: ## Tag a new minor version
@${MAKE} release-$(shell git describe --abbrev=0 --tags | awk -F'[ .]' '{print $$1"."$$2+1".0"}')
.PHONY: major
major: ## Tag a new major version
@${MAKE} release-$(shell git describe --abbrev=0 --tags | awk -F'[ .]' '{print $$1+1".0.0"}')
##@ Autogeneration
.PHONY: gen-docs
gen-docs: ## Generate CLI documentation
@mkdir -p "build/docs"
go run -tags=gen_docs ./cmd/bank-vaults gen-docs "build/docs"
##@ Dependencies
deps: bin/golangci-lint bin/licensei
deps: ## Install dependencies
# Dependency versions
GOLANGCI_VERSION = 1.53.3
LICENSEI_VERSION = 0.8.0
# Dependency binaries
GOLANGCI_LINT_BIN := golangci-lint
LICENSEI_BIN := licensei
# TODO: add support for hadolint and yamllint dependencies
HADOLINT_BIN := hadolint
YAMLLINT_BIN := yamllint
# If we have "bin" dir, use those binaries instead
ifneq ($(wildcard ./bin/.),)
GOLANGCI_LINT_BIN := bin/$(GOLANGCI_LINT_BIN)
LICENSEI_BIN := bin/$(LICENSEI_BIN)
endif
bin/golangci-lint:
@mkdir -p bin
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | bash -s -- v${GOLANGCI_VERSION}
bin/licensei:
@mkdir -p bin
curl -sfL https://raw.githubusercontent.com/goph/licensei/master/install.sh | bash -s -- v${LICENSEI_VERSION}