Permalink
Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign up# Copyright 2017 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. | |
.PHONY: build-% build container-% container push-% push clean test | |
# A space-separated list of all commands in the repository, must be | |
# set in main Makefile of a repository. | |
# CMDS= | |
# This is the default. It can be overridden in the main Makefile after | |
# including build.make. | |
REGISTRY_NAME=quay.io/k8scsi | |
# Revision that gets built into each binary via the main.version | |
# string. Uses the `git describe` output based on the most recent | |
# version tag with a short revision suffix or, if nothing has been | |
# tagged yet, just the revision. | |
# | |
# Beware that tags may also be missing in shallow clones as done by | |
# some CI systems (like TravisCI, which pulls only 50 commits). | |
REV=$(shell git describe --long --tags --match='v*' --dirty 2>/dev/null || git rev-list -n1 HEAD) | |
# A space-separated list of image tags under which the current build is to be pushed. | |
# Determined dynamically. | |
IMAGE_TAGS= | |
# A "canary" image gets built if the current commit is the head of the remote "master" branch. | |
# That branch does not exist when building some other branch in TravisCI. | |
IMAGE_TAGS+=$(shell if [ "$$(git rev-list -n1 HEAD)" = "$$(git rev-list -n1 origin/master 2>/dev/null)" ]; then echo "canary"; fi) | |
# A "X.Y.Z-canary" image gets built if the current commit is the head of a "origin/release-X.Y.Z" branch. | |
# The actual suffix does not matter, only the "release-" prefix is checked. | |
IMAGE_TAGS+=$(shell git branch -r --points-at=HEAD | grep 'origin/release-' | grep -v -e ' -> ' | sed -e 's;.*/release-\(.*\);\1-canary;') | |
# A release image "vX.Y.Z" gets built if there is a tag of that format for the current commit. | |
# --abbrev=0 suppresses long format, only showing the closest tag. | |
IMAGE_TAGS+=$(shell tagged="$$(git describe --tags --match='v*' --abbrev=0)"; if [ "$$tagged" ] && [ "$$(git rev-list -n1 HEAD)" = "$$(git rev-list -n1 $$tagged)" ]; then echo $$tagged; fi) | |
# Images are named after the command contained in them. | |
IMAGE_NAME=$(REGISTRY_NAME)/$* | |
ifdef V | |
TESTARGS = -v -args -alsologtostderr -v 5 | |
else | |
TESTARGS = | |
endif | |
build-%: | |
mkdir -p bin | |
CGO_ENABLED=0 GOOS=linux go build -a -ldflags '-X main.version=$(REV) -extldflags "-static"' -o ./bin/$* ./cmd/$* | |
container-%: build-% | |
docker build -t $*:latest -f $(shell if [ -e ./cmd/$*/Dockerfile ]; then echo ./cmd/$*/Dockerfile; else echo Dockerfile; fi) --label revision=$(REV) . | |
push-%: container-% | |
set -ex; \ | |
push_image () { \ | |
docker tag $*:latest $(IMAGE_NAME):$$tag; \ | |
docker push $(IMAGE_NAME):$$tag; \ | |
}; \ | |
for tag in $(IMAGE_TAGS); do \ | |
if echo $$tag | grep -q -e '-canary$$'; then \ | |
: "creating or overwriting canary image"; \ | |
push_image; \ | |
elif docker pull $(IMAGE_NAME):$$tag 2>&1 | tee /dev/stderr | grep -q "manifest for $(IMAGE_NAME):$$tag not found"; then \ | |
: "creating release image"; \ | |
push_image; \ | |
else \ | |
: "release image $(IMAGE_NAME):$$tag already exists, skipping push"; \ | |
fi; \ | |
done | |
build: $(CMDS:%=build-%) | |
container: $(CMDS:%=container-%) | |
push: $(CMDS:%=push-%) | |
clean: | |
-rm -rf bin | |
test: | |
go test `go list ./... | grep -v 'vendor'` $(TESTARGS) | |
go vet `go list ./... | grep -v vendor` | |
files=$$(find . -name '*.go' | grep -v './vendor'); \ | |
if [ $$(gofmt -d $$files | wc -l) -ne 0 ]; then \ | |
echo "formatting errors:"; \ | |
gofmt -d $$files; \ | |
false; \ | |
fi |