|
1 | | -# Get git commit hash |
2 | | -GIT_COMMIT := $(shell git rev-parse --short HEAD) |
3 | | - |
4 | | -# Try to get the tag, if it exists |
5 | | -GIT_TAG := $(shell git describe --tags --exact-match HEAD 2>/dev/null) |
6 | | - |
7 | | -# Set TAG based on whether a git tag exists |
8 | | -ifdef GIT_TAG |
9 | | - # We're on a tagged commit, use the tag |
10 | | - TAG := $(GIT_TAG) |
11 | | -else |
12 | | - # Not on a tag, use build-{commit} format |
13 | | - TAG := build-$(GIT_COMMIT) |
14 | | -endif |
| 1 | +# Include logic that can be reused across projects. |
| 2 | +include hack/make/build.mk |
| 3 | + |
| 4 | +# Define target platforms, image builder and the fully qualified image name. |
| 5 | +TARGET_PLATFORMS ?= linux/amd64,linux/arm64 |
| 6 | + |
| 7 | +REPO ?= rancherlabs |
| 8 | +IMAGE ?= swiss-army-knife |
| 9 | +IMAGE_NAME = $(REPO)/$(IMAGE) |
| 10 | +FULL_IMAGE_TAG = $(IMAGE_NAME):$(TAG) |
| 11 | +BUILD_ACTION = --load |
15 | 12 |
|
16 | 13 | # Default target |
17 | 14 | .PHONY: all |
18 | 15 | all: build |
19 | 16 |
|
20 | | -# Build target |
| 17 | +# Build target (for local Go binary) |
21 | 18 | .PHONY: build |
22 | 19 | build: |
23 | 20 | go build -o echo-server main.go |
24 | 21 |
|
| 22 | +build-image: buildx-machine ## build (and load) the container image targeting the current platform. |
| 23 | + $(IMAGE_BUILDER) build -f Dockerfile \ |
| 24 | + --builder $(MACHINE) $(IMAGE_ARGS) \ |
| 25 | + --build-arg VERSION=$(VERSION) --platform=$(TARGET_PLATFORMS) -t "$(FULL_IMAGE_TAG)" $(BUILD_ACTION) . |
| 26 | + @echo "Built $(FULL_IMAGE_TAG)" |
| 27 | + |
| 28 | +build-validate: buildx-machine ## build (and load) the container image targeting the current platform. |
| 29 | + mkdir -p ci |
| 30 | + $(IMAGE_BUILDER) build -f Dockerfile \ |
| 31 | + --builder $(MACHINE) $(IMAGE_ARGS) \ |
| 32 | + --build-arg VERSION=$(VERSION) \ |
| 33 | + --platform=$(TARGET_PLATFORMS) \ |
| 34 | + --output type=oci,dest=ci/multiarch-image.oci \ |
| 35 | + -t "$(FULL_IMAGE_TAG)" . |
| 36 | + @echo "Built $(FULL_IMAGE_TAG) multi-arch image saved to ci/multiarch-image.oci" |
| 37 | + |
| 38 | +push-image: validate buildx-machine ## build the container image targeting all platforms defined by TARGET_PLATFORMS and push to a registry. |
| 39 | + $(IMAGE_BUILDER) build -f Dockerfile \ |
| 40 | + --builder $(MACHINE) $(IMAGE_ARGS) $(IID_FILE_FLAG) $(BUILDX_ARGS) \ |
| 41 | + --build-arg VERSION=$(VERSION) --platform=$(TARGET_PLATFORMS) -t "$(FULL_IMAGE_TAG)" --push . |
| 42 | + @echo "Pushed $(FULL_IMAGE_TAG)" |
| 43 | + |
| 44 | +validate: validate-dirty ## Run validation checks. |
| 45 | + |
| 46 | +validate-dirty: |
| 47 | +ifdef DIRTY |
| 48 | + @echo Git is dirty |
| 49 | + @git --no-pager status |
| 50 | + @git --no-pager diff |
| 51 | + @exit 1 |
| 52 | +endif |
| 53 | + |
25 | 54 | # Log target - outputs variables for CI/CD |
26 | 55 | .PHONY: log |
27 | 56 | log: |
28 | 57 | @echo "TAG=$(TAG)" |
| 58 | + @echo "VERSION=$(VERSION)" |
29 | 59 | @echo "BUILD_DATE=$(shell date -u +"%Y-%m-%dT%H:%M:%SZ")" |
30 | | - @echo "GIT_COMMIT=$(GIT_COMMIT)" |
| 60 | + @echo "GIT_COMMIT=$(shell git rev-parse --short HEAD)" |
| 61 | + |
| 62 | +clean: ## clean up project. |
| 63 | + rm -rf build |
| 64 | + rm -rf ci |
| 65 | + rm -f echo-server |
0 commit comments