Skip to content

Commit

Permalink
Minor build improvements
Browse files Browse the repository at this point in the history
Makefile

* Make Go build flags parameterizable
  * Verbose by default
  * Do not force complete rebuild
  * Remove unnecessary '-installsuffix' flag
    https://plus.google.com/117192131596509381660/posts/eNnNePihYnK
* Log build steps
* Rename 'clean' target to 'clean-container'
* Move artifact copy to '.container' target
* Add 'clean' target

Shell script

* Fix shellcheck SC2068
  https://github.com/koalaman/shellcheck/wiki/SC2068
* Reject mandatory vars with empty values
  • Loading branch information
antoineco committed Aug 3, 2018
1 parent 82bfcb0 commit 446641e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
29 changes: 20 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ ARCH ?= $(shell go env GOARCH)
GOARCH = ${ARCH}
DUMB_ARCH = ${ARCH}

GOBUILD_FLAGS :=

ALL_ARCH = amd64 arm arm64 ppc64le s390x

QEMUVERSION = v2.12.0
Expand Down Expand Up @@ -80,7 +82,7 @@ endif

TEMP_DIR := $(shell mktemp -d)

DEF_VARS:=ARCH=$(ARCH) \
DEF_VARS:=ARCH=$(ARCH) \
TAG=$(TAG) \
PKG=$(PKG) \
GOARCH=$(GOARCH) \
Expand Down Expand Up @@ -109,10 +111,15 @@ all-container: $(addprefix sub-container-,$(ALL_ARCH))
all-push: $(addprefix sub-push-,$(ALL_ARCH))

.PHONY: container
container: .container-$(ARCH)
container: clean-container .container-$(ARCH)

.PHONY: .container-$(ARCH)
.container-$(ARCH):
@echo "+ Copying artifact to temporary directory"
mkdir -p $(TEMP_DIR)/rootfs
cp bin/$(ARCH)/nginx-ingress-controller $(TEMP_DIR)/rootfs/nginx-ingress-controller

@echo "+ Building container image $(MULTI_ARCH_IMG):$(TAG)"
cp -RP ./* $(TEMP_DIR)
$(SED_I) "s|BASEIMAGE|$(BASEIMAGE)|g" $(DOCKERFILE)
$(SED_I) "s|QEMUARCH|$(QEMUARCH)|g" $(DOCKERFILE)
Expand All @@ -134,6 +141,11 @@ ifeq ($(ARCH), amd64)
$(DOCKER) tag $(MULTI_ARCH_IMG):$(TAG) $(IMAGE):$(TAG)
endif

.PHONY: clean-container
clean-container:
@echo "+ Deleting container image $(MULTI_ARCH_IMG):$(TAG)"
$(DOCKER) rmi -f $(MULTI_ARCH_IMG):$(TAG) || true

.PHONY: register-qemu
register-qemu:
# Register /usr/bin/qemu-ARCH-static as the handler for binaries in multiple platforms
Expand All @@ -149,17 +161,16 @@ ifeq ($(ARCH), amd64)
$(DOCKER) push $(IMAGE):$(TAG)
endif

.PHONY: clean
clean:
$(DOCKER) rmi -f $(MULTI_ARCH_IMG):$(TAG) || true

.PHONY: build
build: clean
build:
@echo "+ Building bin/$(ARCH)/nginx-ingress-controller"
@$(DEF_VARS) \
GOBUILD_FLAGS="$(GOBUILD_FLAGS)" \
build/go-in-docker.sh build/build.sh

mkdir -p $(TEMP_DIR)/rootfs
cp bin/$(ARCH)/nginx-ingress-controller $(TEMP_DIR)/rootfs/nginx-ingress-controller
.PHONY: clean
clean:
rm -rf bin/ .gocache/ .env

.PHONY: static-check
static-check:
Expand Down
9 changes: 5 additions & 4 deletions build/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,21 @@ mandatory=(
)

missing=false
for var in ${mandatory[@]}; do
if [[ -z "${!var+x}" ]]; then
for var in "${mandatory[@]}"; do
if [[ -z "${!var:-}" ]]; then
echo "Environment variable $var must be set"
missing=true
fi
done

if [ "$missing" = true ];then
if [ "$missing" = true ]; then
exit 1
fi

export CGO_ENABLED=0

go build -a -installsuffix cgo \
go build \
${GOBUILD_FLAGS} \
-ldflags "-s -w \
-X ${PKG}/version.RELEASE=${TAG} \
-X ${PKG}/version.COMMIT=${GIT_COMMIT} \
Expand Down
1 change: 1 addition & 0 deletions build/go-in-docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ TAG=${TAG:-"0.0"}
HOME=${HOME:-/root}
KUBECONFIG=${HOME}/.kube/config
GOARCH=${GOARCH}
GOBUILD_FLAGS=${GOBUILD_FLAGS:-"-v"}
PWD=${PWD}
BUSTED_ARGS=${BUSTED_ARGS:-""}
REPO_INFO=${REPO_INFO:-local}
Expand Down

0 comments on commit 446641e

Please sign in to comment.