2016-09-21 04:15:39 +00:00
|
|
|
# Copyright 2016 The Kubernetes Authors.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
# The binary to build (just the basename).
|
|
|
|
BIN := myapp
|
|
|
|
|
|
|
|
# This repo's root import path (under GOPATH).
|
|
|
|
PKG := github.com/thockin/go-build-template
|
|
|
|
|
|
|
|
# Where to push the docker image.
|
|
|
|
REGISTRY ?= thockin
|
|
|
|
|
2016-10-01 03:28:27 +00:00
|
|
|
# This version-strategy uses git tags to set the version string
|
2016-10-28 16:10:41 +00:00
|
|
|
VERSION := $(shell git describe --tags --always --dirty)
|
2016-10-01 03:28:27 +00:00
|
|
|
#
|
|
|
|
# This version-strategy uses a manual value to set the version string
|
|
|
|
#VERSION := 1.2.3
|
|
|
|
|
2016-09-21 04:15:39 +00:00
|
|
|
###
|
|
|
|
### These variables should not need tweaking.
|
|
|
|
###
|
|
|
|
|
|
|
|
SRC_DIRS := cmd pkg # directories which hold app source (not vendored)
|
|
|
|
|
2019-01-25 21:52:31 +00:00
|
|
|
ALL_PLATFORMS := linux/amd64 linux/arm linux/arm64 linux/ppc64le
|
2016-09-23 05:45:29 +00:00
|
|
|
|
2019-01-28 19:22:08 +00:00
|
|
|
# Used internally. Users should pass GOOS and/or GOARCH.
|
|
|
|
OS := $(if $(GOOS),$(GOOS),$(shell go env GOOS))
|
|
|
|
ARCH := $(if $(GOARCH),$(GOARCH),$(shell go env GOARCH))
|
|
|
|
|
2016-09-23 05:45:29 +00:00
|
|
|
# Set default base image dynamically for each arch
|
2019-01-25 21:52:31 +00:00
|
|
|
# TODO: make these all consistent and tagged.
|
2016-09-23 05:45:29 +00:00
|
|
|
ifeq ($(ARCH),amd64)
|
2019-01-28 19:22:08 +00:00
|
|
|
BASEIMAGE ?= alpine:3.8
|
2016-09-23 05:45:29 +00:00
|
|
|
endif
|
|
|
|
ifeq ($(ARCH),arm)
|
2019-01-28 19:22:08 +00:00
|
|
|
BASEIMAGE ?= armel/busybox
|
2016-09-23 05:45:29 +00:00
|
|
|
endif
|
|
|
|
ifeq ($(ARCH),arm64)
|
2019-01-28 19:22:08 +00:00
|
|
|
BASEIMAGE ?= aarch64/busybox
|
2016-09-23 05:45:29 +00:00
|
|
|
endif
|
|
|
|
ifeq ($(ARCH),ppc64le)
|
2019-01-28 19:22:08 +00:00
|
|
|
BASEIMAGE ?= ppc64le/busybox
|
2016-09-23 05:45:29 +00:00
|
|
|
endif
|
2016-09-21 04:15:39 +00:00
|
|
|
|
2019-01-25 21:52:31 +00:00
|
|
|
IMAGE := $(REGISTRY)/$(BIN)
|
|
|
|
TAG := $(VERSION)__$(OS)_$(ARCH)
|
2016-09-21 04:15:39 +00:00
|
|
|
|
2019-01-25 21:52:31 +00:00
|
|
|
BUILD_IMAGE ?= golang:1.11-alpine
|
2016-09-21 04:15:39 +00:00
|
|
|
|
|
|
|
# If you want to build all binaries, see the 'all-build' rule.
|
|
|
|
# If you want to build all containers, see the 'all-container' rule.
|
|
|
|
# If you want to build AND push all containers, see the 'all-push' rule.
|
|
|
|
all: build
|
|
|
|
|
2019-01-28 19:22:08 +00:00
|
|
|
# For the following OS/ARCH expansions, we transform OS/ARCH into OS_ARCH
|
|
|
|
# because make pattern rules don't match with embedded '/' characters.
|
|
|
|
|
2016-09-21 04:15:39 +00:00
|
|
|
build-%:
|
2019-01-28 19:22:08 +00:00
|
|
|
@$(MAKE) build \
|
|
|
|
--no-print-directory \
|
|
|
|
GOOS=$(firstword $(subst _, ,$*)) \
|
|
|
|
GOARCH=$(lastword $(subst _, ,$*))
|
2016-09-21 04:15:39 +00:00
|
|
|
|
|
|
|
container-%:
|
2019-01-28 19:22:08 +00:00
|
|
|
@$(MAKE) container \
|
|
|
|
--no-print-directory \
|
|
|
|
GOOS=$(firstword $(subst _, ,$*)) \
|
|
|
|
GOARCH=$(lastword $(subst _, ,$*))
|
2016-09-21 04:15:39 +00:00
|
|
|
|
|
|
|
push-%:
|
2019-01-28 19:22:08 +00:00
|
|
|
@$(MAKE) push \
|
|
|
|
--no-print-directory \
|
|
|
|
GOOS=$(firstword $(subst _, ,$*)) \
|
|
|
|
GOARCH=$(lastword $(subst _, ,$*))
|
2016-09-21 04:15:39 +00:00
|
|
|
|
2019-01-28 19:22:08 +00:00
|
|
|
all-build: $(addprefix build-, $(subst /,_, $(ALL_PLATFORMS)))
|
2019-01-25 21:52:31 +00:00
|
|
|
|
2019-01-28 19:22:08 +00:00
|
|
|
all-container: $(addprefix container-, $(subst /,_, $(ALL_PLATFORMS)))
|
2016-09-21 04:15:39 +00:00
|
|
|
|
2019-01-28 19:22:08 +00:00
|
|
|
all-push: $(addprefix push-, $(subst /,_, $(ALL_PLATFORMS)))
|
2016-09-21 04:15:39 +00:00
|
|
|
|
2019-01-25 21:52:31 +00:00
|
|
|
build: bin/$(OS)_$(ARCH)/$(BIN)
|
2016-09-21 04:15:39 +00:00
|
|
|
|
2019-01-25 21:52:31 +00:00
|
|
|
# Directories that we need created to build/test.
|
|
|
|
BUILD_DIRS := bin/$(OS)_$(ARCH) \
|
|
|
|
.go/src/$(PKG) \
|
|
|
|
.go/pkg \
|
2019-01-28 18:51:55 +00:00
|
|
|
.go/bin/$(OS)_$(ARCH) \
|
2019-01-25 21:52:31 +00:00
|
|
|
.go/std/$(OS)_$(ARCH) \
|
|
|
|
.go/cache
|
2016-09-21 04:15:39 +00:00
|
|
|
|
2019-01-28 18:51:55 +00:00
|
|
|
# The following structure defeats Go's (intentional) behavior to always touch
|
|
|
|
# result files, even if they have not changed. This will still run `go` but
|
|
|
|
# will not trigger further work if nothing has actually changed.
|
|
|
|
OUTBIN = bin/$(OS)_$(ARCH)/$(BIN)
|
|
|
|
$(OUTBIN): .go/$(OUTBIN).stamp
|
|
|
|
@true
|
|
|
|
|
|
|
|
# This will build the binary under ./.go and update the real binary iff needed.
|
|
|
|
.PHONY: .go/$(OUTBIN).stamp
|
|
|
|
.go/$(OUTBIN).stamp: $(BUILD_DIRS)
|
|
|
|
@echo "making $(OUTBIN)"
|
2019-01-25 21:52:31 +00:00
|
|
|
@docker run \
|
|
|
|
-i \
|
|
|
|
--rm \
|
|
|
|
-u $$(id -u):$$(id -g) \
|
|
|
|
-v $$(pwd):/go/src/$(PKG) \
|
2019-01-28 18:51:55 +00:00
|
|
|
-v $$(pwd)/.go:/go \
|
|
|
|
-v $$(pwd)/.go/bin/$(OS)_$(ARCH):/go/bin \
|
|
|
|
-v $$(pwd)/.go/bin/$(OS)_$(ARCH):/go/bin/$(OS)_$(ARCH) \
|
2019-01-25 21:52:31 +00:00
|
|
|
-v $$(pwd)/.go/std/$(OS)_$(ARCH):/usr/local/go/pkg/$(OS)_$(ARCH)_static \
|
|
|
|
-v $$(pwd)/.go/cache:/.cache \
|
|
|
|
-w /go/src/$(PKG) \
|
|
|
|
--env HTTP_PROXY=$(HTTP_PROXY) \
|
|
|
|
--env HTTPS_PROXY=$(HTTPS_PROXY) \
|
|
|
|
$(BUILD_IMAGE) \
|
|
|
|
/bin/sh -c " \
|
|
|
|
ARCH=$(ARCH) \
|
|
|
|
OS=$(OS) \
|
|
|
|
VERSION=$(VERSION) \
|
|
|
|
PKG=$(PKG) \
|
|
|
|
./build/build.sh \
|
2016-09-21 04:15:39 +00:00
|
|
|
"
|
2019-01-28 18:51:55 +00:00
|
|
|
@if ! cmp -s .go/$(OUTBIN) $(OUTBIN); then \
|
|
|
|
cat .go/$(OUTBIN) > $(OUTBIN); \
|
|
|
|
date >$@; \
|
|
|
|
fi
|
2016-09-21 04:15:39 +00:00
|
|
|
|
2017-08-20 05:44:55 +00:00
|
|
|
# Example: make shell CMD="-c 'date > datefile'"
|
2019-01-25 21:52:31 +00:00
|
|
|
shell: $(BUILD_DIRS)
|
2017-08-20 05:44:55 +00:00
|
|
|
@echo "launching a shell in the containerized build environment"
|
2019-01-25 21:52:31 +00:00
|
|
|
@docker run \
|
|
|
|
-ti \
|
|
|
|
--rm \
|
|
|
|
-u $$(id -u):$$(id -g) \
|
|
|
|
-v $$(pwd):/go/src/$(PKG) \
|
2019-01-28 18:51:55 +00:00
|
|
|
-v $$(pwd)/.go:/go \
|
|
|
|
-v $$(pwd)/.go/bin/$(OS)_$(ARCH):/go/bin \
|
|
|
|
-v $$(pwd)/.go/bin/$(OS)_$(ARCH):/go/bin/$(OS)_$(ARCH) \
|
2019-01-25 21:52:31 +00:00
|
|
|
-v $$(pwd)/.go/std/$(OS)_$(ARCH):/usr/local/go/pkg/$(OS)_$(ARCH)_static \
|
|
|
|
-v $$(pwd)/.go/cache:/.cache \
|
|
|
|
-w /go/src/$(PKG) \
|
|
|
|
--env HTTP_PROXY=$(HTTP_PROXY) \
|
|
|
|
--env HTTPS_PROXY=$(HTTPS_PROXY) \
|
|
|
|
$(BUILD_IMAGE) \
|
2017-08-20 05:44:55 +00:00
|
|
|
/bin/sh $(CMD)
|
|
|
|
|
2019-01-28 19:22:08 +00:00
|
|
|
# Used to track state in hidden files.
|
2019-01-25 21:52:31 +00:00
|
|
|
DOTFILE_IMAGE = $(subst /,_,$(IMAGE))-$(TAG)
|
2016-09-21 04:15:39 +00:00
|
|
|
|
2019-01-25 21:52:31 +00:00
|
|
|
container: .container-$(DOTFILE_IMAGE) say_container_name
|
|
|
|
.container-$(DOTFILE_IMAGE): bin/$(OS)_$(ARCH)/$(BIN) Dockerfile.in
|
|
|
|
@sed \
|
|
|
|
-e 's|{ARG_BIN}|$(BIN)|g' \
|
|
|
|
-e 's|{ARG_ARCH}|$(ARCH)|g' \
|
|
|
|
-e 's|{ARG_OS}|$(OS)|g' \
|
|
|
|
-e 's|{ARG_FROM}|$(BASEIMAGE)|g' \
|
|
|
|
Dockerfile.in > .dockerfile-$(OS)_$(ARCH)
|
|
|
|
@docker build -t $(IMAGE):$(TAG) -f .dockerfile-$(OS)_$(ARCH) .
|
|
|
|
@docker images -q $(IMAGE):$(TAG) > $@
|
2016-09-21 04:15:39 +00:00
|
|
|
|
2019-01-25 21:52:31 +00:00
|
|
|
say_container_name:
|
|
|
|
@echo "container: $(IMAGE):$(TAG)"
|
2016-09-21 04:15:39 +00:00
|
|
|
|
2019-01-25 21:52:31 +00:00
|
|
|
push: .push-$(DOTFILE_IMAGE) say_push_name
|
2016-09-21 04:15:39 +00:00
|
|
|
.push-$(DOTFILE_IMAGE): .container-$(DOTFILE_IMAGE)
|
2019-01-25 21:52:31 +00:00
|
|
|
@docker push $(IMAGE):$(TAG)
|
|
|
|
|
|
|
|
say_push_name:
|
|
|
|
@echo "pushed: $(IMAGE):$(TAG)"
|
2016-09-21 04:15:39 +00:00
|
|
|
|
2019-01-25 21:52:31 +00:00
|
|
|
manifest-list: push
|
|
|
|
platforms=$$(echo $(ALL_PLATFORMS) | sed 's/ /,/g'); \
|
|
|
|
manifest-tool \
|
|
|
|
--username=oauth2accesstoken \
|
|
|
|
--password=$$(gcloud auth print-access-token) \
|
|
|
|
push from-args \
|
|
|
|
--platforms "$$platforms" \
|
|
|
|
--template $(REGISTRY)/$(BIN):$(VERSION)__OS_ARCH \
|
|
|
|
--target $(REGISTRY)/$(BIN):$(VERSION)
|
2016-09-21 04:15:39 +00:00
|
|
|
|
|
|
|
version:
|
|
|
|
@echo $(VERSION)
|
|
|
|
|
2019-01-25 21:52:31 +00:00
|
|
|
test: $(BUILD_DIRS)
|
2019-01-28 18:51:55 +00:00
|
|
|
@docker run \
|
|
|
|
-i \
|
|
|
|
--rm \
|
|
|
|
-u $$(id -u):$$(id -g) \
|
|
|
|
-v $$(pwd):/go/src/$(PKG) \
|
|
|
|
-v $$(pwd)/.go:/go \
|
|
|
|
-v $$(pwd)/.go/bin/$(OS)_$(ARCH):/go/bin \
|
|
|
|
-v $$(pwd)/.go/bin/$(OS)_$(ARCH):/go/bin/$(OS)_$(ARCH) \
|
|
|
|
-v $$(pwd)/.go/std/$(OS)_$(ARCH):/usr/local/go/pkg/$(OS)_$(ARCH)_static \
|
|
|
|
-v $$(pwd)/.go/cache:/.cache \
|
|
|
|
-w /go/src/$(PKG) \
|
|
|
|
--env HTTP_PROXY=$(HTTP_PROXY) \
|
|
|
|
--env HTTPS_PROXY=$(HTTPS_PROXY) \
|
|
|
|
$(BUILD_IMAGE) \
|
|
|
|
/bin/sh -c " \
|
|
|
|
ARCH=$(ARCH) \
|
|
|
|
OS=$(OS) \
|
|
|
|
VERSION=$(VERSION) \
|
|
|
|
PKG=$(PKG) \
|
|
|
|
./build/test.sh $(SRC_DIRS) \
|
2016-09-21 04:15:39 +00:00
|
|
|
"
|
|
|
|
|
2019-01-25 21:52:31 +00:00
|
|
|
$(BUILD_DIRS):
|
|
|
|
@mkdir -p $@
|
2016-09-21 04:15:39 +00:00
|
|
|
|
|
|
|
clean: container-clean bin-clean
|
|
|
|
|
|
|
|
container-clean:
|
2016-09-23 05:45:29 +00:00
|
|
|
rm -rf .container-* .dockerfile-* .push-*
|
2016-09-21 04:15:39 +00:00
|
|
|
|
|
|
|
bin-clean:
|
|
|
|
rm -rf .go bin
|