|
| 1 | +SHELL := bash |
| 2 | +.ONESHELL: |
| 3 | +.SHELLFLAGS := -eu -o pipefail -c |
| 4 | +.DELETE_ON_ERROR: |
| 5 | +MAKEFLAGS += --warn-undefined-variables |
| 6 | +MAKEFLAGS += --no-builtin-rules |
| 7 | + |
1 | 8 | GO11MODULES=on
|
2 | 9 | APP?=application
|
3 |
| -REGISTRY?=gcr.io/images |
| 10 | +REGISTRY?=stevenacoffman |
4 | 11 | COMMIT_SHA=$(shell git rev-parse --short HEAD)
|
5 |
| - |
6 |
| - |
| 12 | +VERSION=$(shell git rev-parse HEAD) |
| 13 | +BUILD=$(shell date +%FT%T%z) |
| 14 | +LDFLAGS=-ldflags "-X main.Version=${VERSION} -X main.Build=${BUILD}" |
7 | 15 |
|
8 | 16 | .PHONY: build
|
9 |
| -## build: build the application |
10 |
| -build: clean |
11 |
| - @echo "Building..." |
12 |
| - @go build -o ${APP} main.go |
| 17 | +build: clean ## - Build the application |
| 18 | + @printf "\033[32m\xE2\x9c\x93 Building your code\n\033[0m" |
| 19 | + go build -o ${APP} main.go |
13 | 20 |
|
14 | 21 | .PHONY: run
|
15 |
| -## run: runs go run main.go |
16 |
| -run: |
| 22 | +run: build ## - Runs go run main.go |
| 23 | + @printf "\033[32m\xE2\x9c\x93 Running your code\n\033[0m" |
17 | 24 | go run -race main.go
|
18 | 25 |
|
19 | 26 | .PHONY: clean
|
20 |
| -## clean: cleans the binary |
21 |
| -clean: |
22 |
| - @echo "Cleaning" |
23 |
| - @rm -rf ${APP} |
24 |
| - @go get -u -v golang.org/x/tools/cmd/goimports |
25 |
| - @gofmt -l -w -s . |
26 |
| - @goimports -l -w . |
| 27 | +clean: ## - Cleans the binary |
| 28 | + @printf "\033[32m\xE2\x9c\x93 Cleaning your code\n\033[0m" |
| 29 | + rm -rf ${APP} |
| 30 | + go get -u -v golang.org/x/tools/cmd/goimports |
| 31 | + gofmt -l -w -s . |
| 32 | + goimports -l -w . |
| 33 | + go mod tidy |
27 | 34 |
|
28 | 35 | .PHONY: test
|
29 |
| -## test: runs go test with default values |
30 |
| -test: |
| 36 | +test: ## - Runs go test with default values |
| 37 | + @printf "\033[32m\xE2\x9c\x93 Testing your code to find potential problems\n\033[0m" |
31 | 38 | go test -v -count=1 -race ./...
|
32 | 39 |
|
| 40 | +.PHONY: cover |
| 41 | +cover: test ## - Runs test coverage report |
| 42 | + @printf "\033[32m\xE2\x9c\x93 Running Code Test Coverage Report\n\033[0m" |
| 43 | + go test -count=1 -coverprofile=coverage.out |
| 44 | + go tool cover -html=coverage.out |
| 45 | + |
33 | 46 | .PHONY: lint
|
34 |
| -## lint: lint the application code for problems |
35 |
| -lint: |
36 |
| - @golangci-lint run |
| 47 | +lint: clean ## - Lint the application code for problems and nits |
| 48 | + @printf "\033[32m\xE2\x9c\x93 Linting your code to find potential problems\n\033[0m" |
| 49 | + golangci-lint run |
37 | 50 |
|
38 |
| -.PHONY: setup |
39 |
| -## setup: setup go modules |
40 |
| -setup: |
41 |
| - @go mod init \ |
42 |
| - && go mod tidy \ |
43 |
| - && go mod vendor |
| 51 | +.PHONY: docker-build |
| 52 | +docker-build: ## - Build the smallest secure golang docker image based on distroless static |
| 53 | + @printf "\033[32m\xE2\x9c\x93 Build the smallest and secured golang docker image based on distroless static\n\033[0m" |
| 54 | + docker build -f Dockerfile -t ${REGISTRY}/${APP}:${COMMIT_SHA} . |
44 | 55 |
|
45 |
| -# helper rule for deployment |
46 |
| -check-environment: |
47 |
| -ifndef ENV |
48 |
| - $(error ENV not set, allowed values - `staging` or `production`) |
49 |
| -endif |
| 56 | +.PHONY: docker-build-no-cache |
| 57 | +docker-build-no-cache: ## - Build the smallest secure golang docker image based on distroless static with no cache |
| 58 | + @printf "\033[32m\xE2\x9c\x93 Build the smallest and secured golang docker image based on scratch\n\033[0m" |
| 59 | + docker build --no-cache -f Dockerfile -t ${REGISTRY}/${APP}:${COMMIT_SHA} . |
50 | 60 |
|
51 |
| -.PHONY: docker-build |
52 |
| -## docker-build: builds the stringifier docker image to registry |
53 |
| -docker-push: build |
54 |
| - docker build -t ${APP}:${COMMIT_SHA} . |
| 61 | +.PHONY: ls |
| 62 | +ls: ## - List size docker images |
| 63 | + @printf "\033[32m\xE2\x9c\x93 Look at the size dude !\n\033[0m" |
| 64 | + @echo image ls ${REGISTRY}/${APP} |
| 65 | + docker image ls ${REGISTRY}/${APP} |
| 66 | + |
| 67 | +.PHONY: docker-run |
| 68 | +docker-run: docker-build ## - Run the smallest and secured golang docker image based on distroless static nonroot |
| 69 | + @printf "\033[32m\xE2\x9c\x93 Run the smallest and secured golang docker image based on scratch\n\033[0m" |
| 70 | + docker run -p 127.0.0.1:8080:8080/tcp ${REGISTRY}/${APP}:${COMMIT_SHA} |
55 | 71 |
|
56 | 72 | .PHONY: docker-push
|
57 |
| -## docker-push: pushes the stringifier docker image to registry |
58 |
| -docker-push: check-environment docker-build |
59 |
| - docker push ${REGISTRY}/${ENV}/${APP}:${COMMIT_SHA} |
| 73 | +docker-push: docker-build ## - Pushes the docker image to registry |
| 74 | + docker push ${REGISTRY}/${APP}:${COMMIT_SHA} |
60 | 75 |
|
61 | 76 | .PHONY: help
|
62 | 77 | ## help: Prints this help message
|
63 |
| -help: |
64 |
| - @echo "Usage: \n" |
65 |
| - @sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/ /' |
| 78 | +help: ## - Show help message |
| 79 | + @printf "\033[32m\xE2\x9c\x93 usage: make [target]\n\n\033[0m" |
| 80 | + grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' |
0 commit comments