|
| 1 | +# Copyright 2019 The Kubernetes Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +.PHONY: build-% build container-% container push-% push clean test |
| 16 | + |
| 17 | +# A space-separated list of all commands in the repository, must be |
| 18 | +# set in main Makefile of a repository. |
| 19 | +# CMDS= |
| 20 | + |
| 21 | +# This is the default. It can be overridden in the main Makefile after |
| 22 | +# including build.make. |
| 23 | +REGISTRY_NAME=quay.io/k8scsi |
| 24 | + |
| 25 | +# Revision that gets built into each binary via the main.version |
| 26 | +# string. Uses the `git describe` output based on the most recent |
| 27 | +# version tag with a short revision suffix or, if nothing has been |
| 28 | +# tagged yet, just the revision. |
| 29 | +# |
| 30 | +# Beware that tags may also be missing in shallow clones as done by |
| 31 | +# some CI systems (like TravisCI, which pulls only 50 commits). |
| 32 | +REV=$(shell git describe --long --tags --match='v*' --dirty 2>/dev/null || git rev-list -n1 HEAD) |
| 33 | + |
| 34 | +# A space-separated list of image tags under which the current build is to be pushed. |
| 35 | +# Determined dynamically. |
| 36 | +IMAGE_TAGS= |
| 37 | + |
| 38 | +# A "canary" image gets built if the current commit is the head of the remote "master" branch. |
| 39 | +# That branch does not exist when building some other branch in TravisCI. |
| 40 | +IMAGE_TAGS+=$(shell if [ "$$(git rev-list -n1 HEAD)" = "$$(git rev-list -n1 origin/master 2>/dev/null)" ]; then echo "canary"; fi) |
| 41 | + |
| 42 | +# A "X.Y.Z-canary" image gets built if the current commit is the head of a "origin/release-X.Y.Z" branch. |
| 43 | +# The actual suffix does not matter, only the "release-" prefix is checked. |
| 44 | +IMAGE_TAGS+=$(shell git branch -r --points-at=HEAD | grep 'origin/release-' | grep -v -e ' -> ' | sed -e 's;.*/release-\(.*\);\1-canary;') |
| 45 | + |
| 46 | +# A release image "vX.Y.Z" gets built if there is a tag of that format for the current commit. |
| 47 | +# --abbrev=0 suppresses long format, only showing the closest tag. |
| 48 | +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) |
| 49 | + |
| 50 | +# Images are named after the command contained in them. |
| 51 | +IMAGE_NAME=$(REGISTRY_NAME)/$* |
| 52 | + |
| 53 | +ifdef V |
| 54 | +# Adding "-alsologtostderr" assumes that all test binaries contain glog. This is not guaranteed. |
| 55 | +TESTARGS = -v -args -alsologtostderr -v 5 |
| 56 | +else |
| 57 | +TESTARGS = |
| 58 | +endif |
| 59 | + |
| 60 | +# Specific packages can be excluded from each of the tests below by setting the *_FILTER_CMD variables |
| 61 | +# to something like "| grep -v 'github.com/kubernetes-csi/project/pkg/foobar'". See usage below. |
| 62 | + |
| 63 | +build-%: |
| 64 | + mkdir -p bin |
| 65 | + CGO_ENABLED=0 GOOS=linux go build -a -ldflags '-X main.version=$(REV) -extldflags "-static"' -o ./bin/$* ./cmd/$* |
| 66 | + |
| 67 | +container-%: build-% |
| 68 | + docker build -t $*:latest -f $(shell if [ -e ./cmd/$*/Dockerfile ]; then echo ./cmd/$*/Dockerfile; else echo Dockerfile; fi) --label revision=$(REV) . |
| 69 | + |
| 70 | +push-%: container-% |
| 71 | + set -ex; \ |
| 72 | + push_image () { \ |
| 73 | + docker tag $*:latest $(IMAGE_NAME):$$tag; \ |
| 74 | + docker push $(IMAGE_NAME):$$tag; \ |
| 75 | + }; \ |
| 76 | + for tag in $(IMAGE_TAGS); do \ |
| 77 | + if [ "$$tag" = "canary" ] || echo "$$tag" | grep -q -e '-canary$$'; then \ |
| 78 | + : "creating or overwriting canary image"; \ |
| 79 | + push_image; \ |
| 80 | + elif docker pull $(IMAGE_NAME):$$tag 2>&1 | tee /dev/stderr | grep -q "manifest for $(IMAGE_NAME):$$tag not found"; then \ |
| 81 | + : "creating release image"; \ |
| 82 | + push_image; \ |
| 83 | + else \ |
| 84 | + : "release image $(IMAGE_NAME):$$tag already exists, skipping push"; \ |
| 85 | + fi; \ |
| 86 | + done |
| 87 | + |
| 88 | +build: $(CMDS:%=build-%) |
| 89 | +container: $(CMDS:%=container-%) |
| 90 | +push: $(CMDS:%=push-%) |
| 91 | + |
| 92 | +clean: |
| 93 | + -rm -rf bin |
| 94 | + |
| 95 | +test: |
| 96 | + |
| 97 | +.PHONY: test-go |
| 98 | +test: test-go |
| 99 | +test-go: |
| 100 | + @ echo; echo "### $@:" |
| 101 | + go test `go list ./... | grep -v 'vendor' $(TEST_GO_FILTER_CMD)` $(TESTARGS) |
| 102 | + |
| 103 | +.PHONY: test-vet |
| 104 | +test: test-vet |
| 105 | +test-vet: |
| 106 | + @ echo; echo "### $@:" |
| 107 | + go vet `go list ./... | grep -v vendor $(TEST_VET_FILTER_CMD)` |
| 108 | + |
| 109 | +.PHONY: test-fmt |
| 110 | +test: test-fmt |
| 111 | +test-fmt: |
| 112 | + @ echo; echo "### $@:" |
| 113 | + files=$$(find . -name '*.go' | grep -v './vendor' $(TEST_FMT_FILTER_CMD)); \ |
| 114 | + if [ $$(gofmt -d $$files | wc -l) -ne 0 ]; then \ |
| 115 | + echo "formatting errors:"; \ |
| 116 | + gofmt -d $$files; \ |
| 117 | + false; \ |
| 118 | + fi |
| 119 | + |
| 120 | +.PHONY: test-subtree |
| 121 | +test: test-subtree |
| 122 | +test-subtree: |
| 123 | + @ echo; echo "### $@:" |
| 124 | + ./release-tools/verify-subtree.sh release-tools |
0 commit comments