forked from getanteon/anteon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
159 lines (126 loc) · 5.22 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
ifeq ($(V),1)
Q =
PROGRESS = --progress plain
else
Q = @
PROGRESS =
endif
# The binary to build (just the basename).
BIN ?= ddosify
# This repo's root import path (under GOPATH).
PKG := github.com/danivela/ddosify
# Where to push the docker image.
DOCKER_REGISTRY ?= registry.hub.docker.con
# Which architecture to build - see $(ALL_ARCH) for options.
# if the 'local' rule is being run, detect the ARCH from 'go env'
# if it wasn't specified by the caller.
local : ARCH ?= $(shell go env GOOS)-$(shell go env GOARCH)
ARCH ?= linux-amd64
REVISION:= $(shell echo $$(git rev-parse HEAD) ||echo "Unknown Revision")
BUILDINFO_TAG ?= $(shell echo $$(git describe --long --all | tr '/' '-')$$(git diff-index --quiet HEAD -- || echo '-dirty-'$$(git diff-index -u HEAD | openssl sha1 | cut -c 10-17)))
VCS_TAG := $(shell git describe --tags)
ifeq ($(VCS_TAG),)
VCS_TAG := $(BUILDINFO_TAG)
endif
ifeq ($(VERSION),)
VERSION := $(VCS_TAG)
endif
TAG_LATEST ?= false
platform_temp = $(subst -, ,$(ARCH))
GOOS = $(word 1, $(platform_temp))
GOARCH = $(word 2, $(platform_temp))
BASE64_PASSWORD_DANIVELA = ZHVtbXlWYWx1ZQ==
# timestamp value that is formatted according to the RFC3339 standard
BUILD_DATE=$(shell date -u +'%Y-%m-%dT%H:%M:%SZ')
# Set default base image dynamically for each arch
ifeq ($(GOARCH),amd64)
DOCKERFILE ?= Dockerfile.$(BIN)
endif
GIT_COMMIT=$(shell git rev-parse HEAD)
GIT_SHORT_COMMIT=$(shell git rev-parse --short HEAD)
IMAGE_NAME = $(DOCKER_REGISTRY)/library/$(BIN)
IMAGE_NAME_LC = $(shell echo $(IMAGE_NAME) | tr A-Z a-z)
VCS_USER ?= dummy
VCS_PASSWORD = $(shell echo $(BASE64_PASSWORD_DANIVELA) | base64 --decode)
VCS_PROTOCOL ?= https
VCS_URL ?= github.com/danivela/ddosify.git
VCS_REF:= $(shell git rev-parse HEAD)
PHONY: init
init: ./go.mod ## Initialize the module
./go.mod:
$(Q)go mod init $(PKG); go mod tidy
.PHONY: all
all: test build
# GNU make required targets declared as .PHONY to be explicit
BUILD_PHONY_TARGETS = build-linux-amd64 build-darwin-amd64 build-windows-amd64
.PHONY: $(BUILD_PHONY_TARGETS)
$(BUILD_PHONY_TARGETS): build-%:
$(Q)$(MAKE) --no-print-directory ARCH=$* build
PHONY: build
build: output/$(GOOS)/$(GOARCH)/bin/$(BIN)
output/$(GOOS)/$(GOARCH)/bin/$(BIN): build-dirs
@echo "Building binary: $@ $(VERSION)"
GOOS=$(GOOS) \
GOARCH=$(GOARCH) \
VERSION=$(VERSION) \
PKG=$(PKG) \
BIN=$(BIN) \
OUTPUT_DIR=./output/$(GOOS)/$(GOARCH)/bin \
./scripts/build.sh
PHONY: local
local: build-dirs ## Build application for the local arch
GOOS=$(GOOS) \
GOARCH=$(GOARCH) \
VERSION=$(VERSION) \
PKG=$(PKG) \
BIN=$(BIN) \
OUTPUT_DIR=$$(pwd)/output/$(GOOS)/$(GOARCH)/bin \
scripts/build.sh
PHONY: build-dirs
build-dirs:
@mkdir -p output/$(GOOS)/$(GOARCH)/bin
@mkdir -p .go/src/$(PKG) .go/pkg .go/bin .go/std/$(GOOS)/$(GOARCH) .go/go-build
CLEAN_PHONY_TARGETS = clean-linux-amd64 clean-darwin-amd64 clean-windows-amd64
.PHONY: $(CLEAN_PHONY_TARGETS)
$(CLEAN_PHONY_TARGETS): clean-%:
$(Q)$(MAKE) --no-print-directory ARCH=$* clean
.PHONY: clean
clean-%:
$(Q)$(MAKE) --no-print-directory ARCH=$* clean
clean: ## Clean out all generated items
$(Q)echo "Cleaning binary: $(pwd)/output/$(GOOS)/$(GOARCH)/bin/${BIN}"
$(Q)test -e $$(pwd)/output/$(GOOS)/$(GOARCH)/bin/${BIN} || rm $$(pwd)/output/$(GOOS)/$(GOARCH)/bin/${BIN}
.PHONY: coverage
coverage: ## Generates the total code coverage of the project
$(Q)$(eval COVERAGE_DIR=$(shell mktemp -d))
$(Q)mkdir -p $(COVERAGE_DIR)/tmp
$(Q)for j in $$(go list ./... | grep -v '/vendor/' | grep -v '/ext/'); do go test -covermode=count -coverprofile=$(COVERAGE_DIR)/$$(basename $$j).out $$j > /dev/null 2>&1; done
$(Q)echo 'mode: count' > $(COVERAGE_DIR)/tmp/full.out
$(Q)tail -q -n +2 $(COVERAGE_DIR)/*.out >> $(COVERAGE_DIR)/tmp/full.out
$(Q)@go tool cover -func=$(COVERAGE_DIR)/tmp/full.out | tail -n 1 | sed -e 's/^.*statements)[[:space:]]*//' -e 's/%//'
.PHONY: package
package: ## Create a docker image of the project
@echo "Packaging image: $(VERSION) [$(GIT_COMMIT)]"
$(Q)docker build --no-cache --build-arg VCS_REF=$(GIT_COMMIT) --build-arg VERSION=$(VERSION) \
--build-arg NAME=$(BIN) --build-arg BUILD_DATE=$(BUILD_DATE) $(PROGRESS) \
-t $(IMAGE_NAME_LC):local -f $(DOCKERFILE) .
.PHONY: tag
tag: ## Tag image created by package with latest, git commit and version
@echo "Tagging image: ${VERSION} $(GIT_COMMIT)"
$(Q)docker tag $(IMAGE_NAME_LC):local $(IMAGE_NAME_LC):$(GIT_SHORT_COMMIT)
$(Q)docker tag $(IMAGE_NAME_LC):local $(IMAGE_NAME_LC):${VERSION}
.PHONY: push
push: tag ## Push tagged images to docker registry
@echo "Pushing docker image to registry: ${VERSION} $(GIT_SHORT_COMMIT)"
# $(Q)(echo $(BASE64_PASSWORD) | base64 --decode | docker login -u danifv27 --password-stdin $(DOCKER_REGISTRY))
$(Q)docker push $(IMAGE_NAME_LC):$(GIT_SHORT_COMMIT)
$(Q)docker push $(IMAGE_NAME_LC):${VERSION}
# $(Q)docker logout $(DOCKER_REGISTRY)
.PHONY: help
help: ## Show This Help
@for line in $$(cat Makefile | grep "##" | grep -v "grep" | sed "s/:.*##/:/g" | sed "s/\ /!/g"); do verb=$$(echo $$line | cut -d ":" -f 1); desc=$$(echo $$line | cut -d ":" -f 2 | sed "s/!/\ /g"); printf "%-30s--%s\n" "$$verb" "$$desc"; done
.PHONY: test
test: unit_test ## Run all available tests
.PHONY: unit_test
unit_test: ## Run all available unit tests
# go test -v $(shell go list ./... | grep -v /vendor/)