Skip to content

Commit c52b4da

Browse files
authored
Add variables to makefile help and build-goreleaser target (#740)
Adds the list of variables when printing the help and a new target for building the binary with GoReleaser
1 parent 010f1ed commit c52b4da

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ cover.html
2525

2626
# Binary and Artifacts
2727
build/.out
28+
build/out
2829
dist/
2930

3031
# Node modules

Makefile

+20-12
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
1+
# variables that should not be overridden by the user
12
VERSION = edge
2-
TAG = $(VERSION)
3-
PREFIX ?= nginx-kubernetes-gateway
4-
5-
GIT_COMMIT = $(shell git rev-parse HEAD)
3+
GIT_COMMIT = $(shell git rev-parse HEAD || echo "unknown")
64
DATE = $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
75

8-
TARGET ?= local
9-
10-
KIND_KUBE_CONFIG_FOLDER = $${HOME}/.kube/kind
11-
OUT_DIR=$(shell pwd)/build/.out
6+
# variables that can be overridden by the user
7+
PREFIX ?= nginx-kubernetes-gateway ## The name of the image. For example, nginx-kubernetes-gateway
8+
TAG ?= $(VERSION:v%=%) ## The tag of the image. For example, 0.3.0
9+
TARGET ?= local ## The target of the build. Possible values: local and container
10+
KIND_KUBE_CONFIG_FOLDER = $${HOME}/.kube/kind ## The folder where the kind kubeconfig is stored
11+
OUT_DIR ?= $(shell pwd)/build/out ## The folder where the binary will be stored
12+
ARCH ?= amd64 ## The architecture of the image and/or binary. For example: amd64 or arm64
13+
override DOCKER_BUILD_OPTIONS += --build-arg VERSION=$(VERSION) --build-arg GIT_COMMIT=$(GIT_COMMIT) --build-arg DATE=$(DATE) ## The options for the docker build command. For example, --pull
1214

1315
.DEFAULT_GOAL := help
1416

1517
.PHONY: help
1618
help: Makefile ## Display this help
17-
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "; printf "Usage:\n\n make \033[36m<target>\033[0m\n\nTargets:\n\n"}; {printf " \033[36m%-30s\033[0m %s\n", $$1, $$2}'
19+
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "; printf "Usage:\n\n make \033[36m<target>\033[0m [VARIABLE=value...]\n\nTargets:\n\n"}; {printf " \033[36m%-30s\033[0m %s\n", $$1, $$2}'
20+
@grep -E '^(override )?[a-zA-Z_-]+ \??\+?= .*? ## .*$$' $< | sort | awk 'BEGIN {FS = " \\??\\+?= .*? ## "; printf "\nVariables:\n\n"}; {gsub(/override /, "", $$1); printf " \033[36m%-30s\033[0m %s\n", $$1, $$2}'
1821

1922
.PHONY: container
2023
container: build ## Build the container
2124
@docker -v || (code=$$?; printf "\033[0;31mError\033[0m: there was a problem with Docker\n"; exit $$code)
22-
docker build --build-arg VERSION=$(VERSION) --build-arg GIT_COMMIT=$(GIT_COMMIT) --build-arg DATE=$(DATE) --target $(TARGET) -f build/Dockerfile -t $(PREFIX):$(TAG) .
25+
docker build --platform linux/$(ARCH) $(strip $(DOCKER_BUILD_OPTIONS)) --target $(strip $(TARGET)) -f build/Dockerfile -t $(strip $(PREFIX)):$(strip $(TAG)) .
2326

2427
.PHONY: build
2528
build: ## Build the binary
2629
ifeq (${TARGET},local)
2730
@go version || (code=$$?; printf "\033[0;31mError\033[0m: unable to build locally\n"; exit $$code)
28-
CGO_ENABLED=0 GOOS=linux go build -trimpath -a -ldflags "-s -w -X main.version=${VERSION} -X main.commit=${GIT_COMMIT} -X main.date=${DATE}" -o $(OUT_DIR)/gateway github.com/nginxinc/nginx-kubernetes-gateway/cmd/gateway
31+
CGO_ENABLED=0 GOOS=linux GOARCH=$(ARCH) go build -trimpath -a -ldflags "-s -w -X main.version=${VERSION} -X main.commit=${GIT_COMMIT} -X main.date=${DATE}" -o $(OUT_DIR)/gateway github.com/nginxinc/nginx-kubernetes-gateway/cmd/gateway
2932
endif
3033

34+
.PHONY: build-goreleaser
35+
build-goreleaser: ## Build the binary using GoReleaser
36+
@goreleaser -v || (code=$$?; printf "\033[0;31mError\033[0m: there was a problem with GoReleaser. Follow the docs to install it https://goreleaser.com/install\n"; exit $$code)
37+
GOOS=linux GOPATH=$(shell go env GOPATH) GOARCH=$(ARCH) goreleaser build --clean --snapshot --single-target
38+
3139
.PHONY: generate
3240
generate: ## Run go generate
3341
go generate ./...
@@ -36,7 +44,7 @@ generate: ## Run go generate
3644
clean: ## Clean the build
3745
-rm -r $(OUT_DIR)
3846

39-
.PHONY: clean--go-cache
47+
.PHONY: clean-go-cache
4048
clean-go-cache: ## Clean go cache
4149
@go clean -modcache
4250

0 commit comments

Comments
 (0)