forked from mccutchen/go-httpbin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
166 lines (127 loc) · 5.42 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
.PHONY: clean deploy deps gcloud-auth image imagepush lint run stagedeploy test testci testcover
# The version that will be used in docker tags (e.g. to push a
# go-httpbin:latest image use `make imagepush VERSION=latest)`
VERSION ?= $(shell git rev-parse --short HEAD)
# Override these values to deploy to a different Cloud Run project
GCLOUD_PROJECT ?= httpbingo
GCLOUD_ACCOUNT ?= mccutchen@gmail.com
GCLOUD_REGION ?= us-central1
# The version tag for the Cloud Run deployment (override this to adjust
# pre-production URLs)
GCLOUD_TAG ?= "v-$(VERSION)"
# Run gcloud in a container to avoid needing to install the SDK locally
GCLOUD_COMMAND ?= ./bin/gcloud
# We push docker images to both docker hub and gcr.io
DOCKER_TAG_DOCKERHUB ?= mccutchen/go-httpbin:$(VERSION)
DOCKER_TAG_GCLOUD ?= gcr.io/$(GCLOUD_PROJECT)/go-httpbin:$(VERSION)
# Built binaries will be placed here
DIST_PATH ?= dist
# Default flags used by the test, testci, testcover targets
COVERAGE_PATH ?= coverage.txt
COVERAGE_ARGS ?= -covermode=atomic -coverprofile=$(COVERAGE_PATH)
TEST_ARGS ?= -race
# Tool dependencies
TOOL_BIN_DIR ?= $(shell go env GOPATH)/bin
TOOL_GOBINDATA := $(TOOL_BIN_DIR)/go-bindata
TOOL_GOLINT := $(TOOL_BIN_DIR)/golint
TOOL_STATICCHECK := $(TOOL_BIN_DIR)/staticcheck
GO_SOURCES = $(wildcard **/*.go)
GENERATED_ASSETS_PATH := httpbin/assets/assets.go
# =============================================================================
# build
# =============================================================================
build: $(DIST_PATH)/go-httpbin
$(DIST_PATH)/go-httpbin: assets $(GO_SOURCES)
mkdir -p $(DIST_PATH)
CGO_ENABLED=0 go build -ldflags="-s -w" -o $(DIST_PATH)/go-httpbin ./cmd/go-httpbin
assets: $(GENERATED_ASSETS_PATH)
buildtests:
CGO_ENABLED=0 go test -ldflags="-s -w" -v -c -o $(DIST_PATH)/go-httpbin.test ./httpbin
clean:
rm -rf $(DIST_PATH) $(COVERAGE_PATH)
$(GENERATED_ASSETS_PATH): $(TOOL_GOBINDATA) static/*
$(TOOL_GOBINDATA) -o $(GENERATED_ASSETS_PATH) -pkg=assets -prefix=static -modtime=1601471052 static
# reformat generated code
gofmt -s -w $(GENERATED_ASSETS_PATH)
# dumb hack to make generate code lint correctly
sed -i.bak 's/Html/HTML/g' $(GENERATED_ASSETS_PATH)
sed -i.bak 's/Xml/XML/g' $(GENERATED_ASSETS_PATH)
rm $(GENERATED_ASSETS_PATH).bak
# =============================================================================
# test & lint
# =============================================================================
test:
go test $(TEST_ARGS) ./...
# Test command to run for continuous integration, which includes code coverage
# based on codecov.io's documentation:
# https://github.com/codecov/example-go/blob/b85638743b972bd0bd2af63421fe513c6f968930/README.md
testci: build
go test $(TEST_ARGS) $(COVERAGE_ARGS) ./...
git diff --exit-code
testcover: testci
go tool cover -html=$(COVERAGE_PATH)
lint: $(TOOL_GOLINT) $(TOOL_STATICCHECK)
test -z "$$(gofmt -d -s -e .)" || (echo "Error: gofmt failed"; gofmt -d -s -e . ; exit 1)
go vet ./...
$(TOOL_GOLINT) -set_exit_status ./...
$(TOOL_STATICCHECK) ./...
# =============================================================================
# run locally
# =============================================================================
run: build
$(DIST_PATH)/go-httpbin
watch: $(TOOL_REFLEX)
reflex -s -r '\.(go|html)$$' make run
# =============================================================================
# deploy to fly.io
# =============================================================================
deploy:
flyctl deploy --strategy=rolling
# =============================================================================
# deploy to google cloud run
# =============================================================================
deploy-cloud-run: gcloud-auth imagepush
$(GCLOUD_COMMAND) beta run deploy \
$(GCLOUD_PROJECT) \
--image=$(DOCKER_TAG_GCLOUD) \
--revision-suffix=$(VERSION) \
--tag=$(GCLOUD_TAG) \
--project=$(GCLOUD_PROJECT) \
--region=$(GCLOUD_REGION) \
--allow-unauthenticated \
--platform=managed
$(GCLOUD_COMMAND) run services update-traffic --to-latest
stagedeploy-cloud-run: gcloud-auth imagepush
$(GCLOUD_COMMAND) beta run deploy \
$(GCLOUD_PROJECT) \
--image=$(DOCKER_TAG_GCLOUD) \
--revision-suffix=$(VERSION) \
--tag=$(GCLOUD_TAG) \
--project=$(GCLOUD_PROJECT) \
--region=$(GCLOUD_REGION) \
--allow-unauthenticated \
--platform=managed \
--no-traffic
gcloud-auth:
@$(GCLOUD_COMMAND) auth list | grep '^\*' | grep -q $(GCLOUD_ACCOUNT) || $(GCLOUD_COMMAND) auth login $(GCLOUD_ACCOUNT)
@$(GCLOUD_COMMAND) auth print-access-token | docker login -u oauth2accesstoken --password-stdin https://gcr.io
# =============================================================================
# docker images
# =============================================================================
image:
DOCKER_BUILDKIT=1 docker build -t $(DOCKER_TAG_DOCKERHUB) .
imagepush: image
docker push $(DOCKER_TAG_DOCKERHUB)
# =============================================================================
# dependencies
#
# Deps are installed outside of working dir to avoid polluting go modules
# =============================================================================
$(TOOL_GOBINDATA):
cd /tmp && go get -u github.com/kevinburke/go-bindata/...
$(TOOL_GOLINT):
cd /tmp && go get -u golang.org/x/lint/golint
$(TOOL_REFLEX):
cd /tmp && go get -u github.com/cespare/reflex
$(TOOL_STATICCHECK):
cd /tmp && go get -u honnef.co/go/tools/cmd/staticcheck