-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
184 lines (151 loc) · 7.45 KB
/
Copy pathMakefile
File metadata and controls
184 lines (151 loc) · 7.45 KB
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
#
# MAKEFILE for Stevedore
# binary output name
BINARY=stevedore
# project name
PROJECT=github.com/gostevedore/stevedore
# folder to store artifacts generated
ARTIFACTS_DIR=dist
# Values Version and Commit
VERSION=`cat version || echo "unknown"`
COMMIT=`git rev-parse --short HEAD || echo "unknown"`
BUILD_DATE=`date +"%c"`
# Go options
GO_TEST_OPTS=-count=1 -parallel=4 -race
# Setup the -ldflags option for go build here, interpolate the variable values
# -s: Omit the symbol table and debug information.
# -w: Omit the DWARF symbol table
LDFLAGS=-ldflags "-s -w -X '${PROJECT}/internal/core/domain/release.BuildDate=${BUILD_DATE}' -X ${PROJECT}/internal/core/domain/release.Version=$(VERSION) -X ${PROJECT}/internal/release.Commit=${COMMIT}"
## Colors
COLOR_GREEN=\033[0;32m
COLOR_RED=\033[0;31m
COLOR_BLUE=\033[0;34m
COLOR_PURPLE=\033[0;35m
COLOR_END=\033[0m
DOCKER_COMPOSE_BINARY := $(shell docker compose version > /dev/null 2>&1 && echo "docker compose" || (which docker-compose > /dev/null 2>&1 && echo "docker-compose" || (echo "docker compose not found. Aborting." >&2; exit 1)))
# dafault target
.DEFAULT_GOAL: help
# define phony targets
.PHONY: tests static-analysis snapshot tag unit-tests functional-tests vet golint staticcheck gosec errcheck golangci-lint
help: ## Lists available targets
@echo
@echo "Makefile usage:"
@grep -E '^[a-zA-Z1-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[1;35m%-20s\033[0m %s\n", $$1, $$2}'
@echo
tests: unit-tests functional-tests ## Executes all tests
static-analysis: vet golint staticcheck gosec errcheck ## Execute static analysis
clean: ## Clears binaries generated by the install and build targets
if [ -f bin/$(BINARY) ] ; then rm -f bin/$(BINARY) ; rm -f $$GOPATH/bin/$(BINARY) ; fi
if [ -f bin/$(BINARY).$(CHECKSUM_EXT) ] ; then rm -f bin/$(BINARY).$(CHECKSUM_EXT) ; fi
if [ -f $(ARTIFACTS_DIR)/$(BINARY)-$(VERSION).tar.gz ] ; then rm -f $(ARTIFACTS_DIR)/$(BINARY)-$(VERSION).tar.gz ; fi
snapshot: ## Creates a GoReleaser snapshot
goreleaser --snapshot --skip=publish --clean --release-notes RELEASE_NOTES.md
tag: ## Creates a tag on main branch which value is the content of Version file
git checkout main
git pull origin main
git tag -a v$(VERSION) -m "Version v$(VERSION)"
git push origin v$(VERSION)
unit-tests: ## Executes the unit tests found in the folder ./internal
@echo
@echo "$(COLOR_GREEN) Executing unit tests $(COLOR_END)"
@echo
@$(DOCKER_COMPOSE_BINARY) --project-name stevedore-unit-tests run --build --entrypoint go ci test $(GO_TEST_OPTS) ./internal/...
functional-tests: ## Executes the functional tests found in the folder ./test/functional
@echo
@echo "$(COLOR_GREEN) Executing functional tests $(COLOR_END)"
@echo
@echo "$(COLOR_GREEN) cleaning up the testing stack...$(COLOR_END)"
@$(DOCKER_COMPOSE_BINARY) --project-name stevedore-functional-tests down --volumes --remove-orphans --timeout 3
@echo
@echo "$(COLOR_GREEN) starting the testing stack...$(COLOR_END)"
@$(DOCKER_COMPOSE_BINARY) --project-name stevedore-functional-tests up --build --detach ci
@echo
@echo "$(COLOR_GREEN) waiting for dockerd...$(COLOR_END)"
@RC=0; \
for i in $$(seq 1 5); \
do $(DOCKER_COMPOSE_BINARY) --project-name stevedore-functional-tests exec ci /usr/local/bin/wait-for-dockerd.sh && RC=0 && break || RC=$$? && $(DOCKER_COMPOSE_BINARY) --project-name stevedore-functional-tests restart ci --timeout 3; \
done || ( echo " Error starting the CI container"; exit $$RC )
@echo
@echo "$(COLOR_GREEN) executing build functional tests...$(COLOR_END)"; \
RC=0; \
$(DOCKER_COMPOSE_BINARY) --project-name stevedore-functional-tests exec ci go test $(GO_TEST_OPTS) -v -run TestBuildFunctionalTests ./test/functional || RC=$$(($$RC + 1)); \
echo; \
echo "$(COLOR_GREEN) executing promote functional tests...$(COLOR_END)"; \
$(DOCKER_COMPOSE_BINARY) --project-name stevedore-functional-tests exec ci go test $(GO_TEST_OPTS) -v -run TestPromoteFunctionalTests ./test/functional || RC=$$(($$RC + 1)); \
echo; \
echo "$(COLOR_GREEN) executing initialize functional tests...$(COLOR_END)"; \
$(DOCKER_COMPOSE_BINARY) --project-name stevedore-functional-tests exec ci go test $(GO_TEST_OPTS) -v -run TestInitializeFunctionalTests ./test/functional || RC=$$(($$RC + 1)); \
echo; \
echo "$(COLOR_GREEN) stopping up the testing stack...$(COLOR_END)"; \
$(DOCKER_COMPOSE_BINARY) --project-name stevedore-functional-tests down --volumes --remove-orphans --timeout 3 || RC=$$(($$RC + 1)); \
exit $$RC
vet: ## Executes the go vet
@echo
@echo "$(COLOR_GREEN) Executing go vet $(COLOR_END)"
@echo
@$(DOCKER_COMPOSE_BINARY) --project-name stevedore-go-vet run --build --entrypoint go ci vet $(LDFLAGS) ./...
golint: ## Executes golint
@echo
@echo "$(COLOR_GREEN) Executing golint$(COLOR_END)"
@echo
@$(DOCKER_COMPOSE_BINARY) --project-name stevedore-golint run --build ci golint ./internal/...
staticcheck: ## Executes staticcheck
@echo
@echo "$(COLOR_GREEN) Executing staticcheck$(COLOR_END)"
@echo
@$(DOCKER_COMPOSE_BINARY) --project-name stevedore-staticcheck run --build ci staticcheck ./internal/...
gosec: ## Executes gosec
@echo
@echo "$(COLOR_GREEN) Executing gosec$(COLOR_END)"
@echo
@$(DOCKER_COMPOSE_BINARY) --project-name stevedore-gosec run --build ci gosec ./internal/...
errcheck: ## Executes errcheck
@echo
@echo "$(COLOR_GREEN) Executing errcheck$(COLOR_END)"
@echo
@$(DOCKER_COMPOSE_BINARY) --project-name stevedore-errcheck run --build --entrypoint errcheck ci -ignoretests -ignorepkg fmt ./internal/...
golangci-lint: ## Executes golangci-lint
@echo
@echo "$(COLOR_GREEN) Executing golangci-lint$(COLOR_END)"
@echo
@$(DOCKER_COMPOSE_BINARY) --project-name stevedore-golangci-lint run --build ci golangci-lint run ./internal
attach-stevedore:
$(DOCKER_COMPOSE_BINARY) run --build --volumes ${PWD}:/app --workdir /app ci sh
#
# build the binary
#
# "...We’re disabling cgo which gives us a static binary.
# We’re also setting the OS to Linux (in case someone builds this on a Mac or Windows)
# and the -a flag means to rebuild all the packages we’re using,
# which means all the imports will be rebuilt with cgo disabled..."
#
# reference: https://blog.codeship.com/building-minimal-docker-containers-for-go-applications/
#
#
# checksum
# CHECKSUM=md5sum
# CHECKSUM_EXT=md5
# dependencies: ## Downloads dependencies
# go mod download
# build: clean ## run a golang build (it is recommend to use 'snapshot' target)
# CGO_ENABLED=0 GOOS=linux go build $(LDFLAGS) -a -o bin/$(BINARY) cmd/$(BINARY).go
# checksum: build ## Generates binary checksum
# $(CHECKSUM) bin/$(BINARY) > bin/$(BINARY).$(CHECKSUM_EXT)
# install: clean test ## Installs the compiled dependencies in $GOPATH/pkg and put the binary in $GOPATH/bin
# go install $(LDFLAGS) ./...
# notes: ## Generates release notes from commits since last tag
# echo "# RELEASE NOTES" > aux && \
# echo "" >> aux & \
# echo "## $(VERSION)" >> aux && \
# echo "" >> aux & \
# git log --format="- %h: %s" `git describe --tags --abbrev=0 @^`..@ >> aux && \
# LINES=`cat RELEASE_NOTES.md | wc -l` && \
# tail -n `expr $$LINES - 1` RELEASE_NOTES.md >> aux && \
# mv aux RELEASE_NOTES.md
# tar: checksum ## Creates a tarred artifact (it is recommend to use 'snapshot' target)
# @echo
# @echo "$(COLOR_GREEN) Executing unit tests $(COLOR_END)"
# @echo
# mkdir -p $(ARTIFACTS_DIR)
# tar cvzf $(ARTIFACTS_DIR)/$(BINARY)-$(VERSION).tar.gz bin/$(BINARY) bin/$(BINARY).$(CHECKSUM_EXT)
# rm -rf bin/$(BINARY) bin/$(BINARY).$(CHECKSUM_EXT)