Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
148 lines (131 loc) · 6.14 KB
/
Copy pathMakefile
File metadata and controls
148 lines (131 loc) · 6.14 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
SHELL=/bin/bash -o pipefail
BIN := restic
REGISTRY ?= stashed
GO_PKG := stash.appscode.dev
REPO := $(notdir $(shell pwd))
SRC_DIRS := *.go cmd helpers internal
# This version-strategy uses git tags to set the version string
git_branch := $(shell git rev-parse --abbrev-ref HEAD)
git_tag := $(shell git describe --exact-match --abbrev=0 2>/dev/null || echo "")
commit_hash := $(shell git rev-parse --verify HEAD)
commit_timestamp := $(shell date --date="@$$(git show -s --format=%ct)" --utc +%FT%T)
VERSION := $(shell git describe --tags --always --dirty)
version_strategy := commit_hash
ifdef git_tag
VERSION := $(git_tag)
version_strategy := tag
else
ifeq (,$(findstring $(git_branch),master HEAD))
ifneq (,$(patsubst release-%,,$(git_branch)))
VERSION := $(git_branch)
version_strategy := branch
endif
endif
endif
IMAGE := $(REGISTRY)/$(BIN)
TAG := $(VERSION)
GO_VERSION ?= 1.16
BUILD_IMAGE ?= appscode/golang-dev:$(GO_VERSION)
DOCKERFILE := $(shell pwd)/docker/Dockerfile
# Directories that we need created to build/test.
BUILD_DIRS := .go/bin \
.go/cache \
.PHONY: all clean test restic
all: fmt build restic test
# run gofmt, goimports etc.
.PHONY: fmt
fmt: $(BUILD_DIRS)
@docker run \
-i \
--rm \
-u $$(id -u):$$(id -g) \
-v $$(pwd):/src \
-w /src \
-v $$(pwd)/.go/bin:/go/bin \
-v $$(pwd)/.go/cache:/.cache \
--env HTTP_PROXY=$(HTTP_PROXY) \
--env HTTPS_PROXY=$(HTTPS_PROXY) \
$(BUILD_IMAGE) \
/bin/bash -c " \
REPO_PKG=$(GO_PKG) \
./helpers/fmt.sh $(SRC_DIRS) \
"
# build release binaries
.PHONY: build
build: $(BUILD_DIRS)
@echo "Building release binaries"
@docker run \
-i \
--rm \
-u $$(id -u):$$(id -g) \
-v $$(pwd):/src \
-w /src \
-v $$(pwd)/.go/bin:/go/bin \
-v $$(pwd)/.go/cache:/.cache \
--env HTTP_PROXY=$(HTTP_PROXY) \
--env HTTPS_PROXY=$(HTTPS_PROXY) \
--env GO111MODULE=on \
$(BUILD_IMAGE) \
go run /src/helpers/build-release-binaries/main.go --version $(TAG:v%=%) --source="/src" --output="/go/bin/" #ref: https://stackoverflow.com/a/19571504
restic: $(BUILD_DIRS)
@echo "Building restic binary"
@docker run \
-i \
--rm \
-u $$(id -u):$$(id -g) \
-v $$(pwd):/src \
-w /src \
-v $$(pwd)/.go/bin:/go/bin \
-v $$(pwd)/.go/cache:/.cache \
--env HTTP_PROXY=$(HTTP_PROXY) \
--env HTTPS_PROXY=$(HTTPS_PROXY) \
--env GO111MODULE=on \
golang:${GO_VERSION}-alpine \
go run build.go
.PHONY: container
container: restic
@echo "Building restic docker image"
docker build --rm -t ${REGISTRY}/${BIN}:${TAG} -f ${DOCKERFILE} .
.PHONY: push
push: container
@echo "Pushing docker image: ${REGISTRY}/${BIN}:${TAG}"
docker push ${REGISTRY}/${BIN}:${TAG}
ADDTL_LINTERS := goconst,gofmt,goimports,unparam
.PHONY: lint
lint: $(BUILD_DIRS)
@echo "running linter"
@docker run \
-i \
--rm \
-u $$(id -u):$$(id -g) \
-v $$(pwd):/src \
-w /src \
-v $$(pwd)/.go/bin:/go/bin \
-v $$(pwd)/.go/cache:/.cache \
--env HTTP_PROXY=$(HTTP_PROXY) \
--env HTTPS_PROXY=$(HTTPS_PROXY) \
--env GO111MODULE=on \
--env GOFLAGS="-mod=vendor" \
$(BUILD_IMAGE) \
golangci-lint run --enable $(ADDTL_LINTERS) --timeout=10m --skip-dirs-use-default --skip-dirs=vendor
$(BUILD_DIRS):
@mkdir -p $@
clean:
rm -rf restic .go
.PHONY: test
test: $(BUILD_DIRS)
@docker run \
-i \
--rm \
-u $$(id -u):$$(id -g) \
-v $$(pwd):/src \
-w /src \
--net=host \
-v $(HOME)/.credentials:$(HOME)/.credentials \
-v $$(pwd)/.go/bin:/go/bin \
-v $$(pwd)/.go/cache:/.cache \
--env HTTP_PROXY=$(HTTP_PROXY) \
--env HTTPS_PROXY=$(HTTPS_PROXY) \
--env GOFLAGS="-mod=vendor" \
$(BUILD_IMAGE) \
go test ./cmd/... ./internal/...