|
1 | 1 | SHELL := /bin/bash |
2 | 2 |
|
3 | | -# Env var inputs for container builds |
| 3 | +# Env var inputs for container image builds |
4 | 4 | # REGISTRY: The registry to which the build image should be pushed to. Defaults to "docker.io" (Docker Hub) |
5 | 5 | # IMAGE: The name of the image to build and publish in the afore mentioned registry. Defaults to "gotify/cli" |
6 | 6 | # PLATFORM: The platform for which the image should be built, defaults to amd64, arm64, i386, arm/v7 and riscv64 |
7 | 7 | REGISTRY ?= docker.io |
8 | 8 | IMAGE ?= gotify/cli |
9 | 9 | PLATFORM ?= linux/amd64,linux/arm64,linux/386,linux/arm/v7,linux/riscv64 |
10 | 10 |
|
| 11 | +# Env var inputs for all builds |
| 12 | +# VERSION: The version for which the container image or the binary is being built. This variable has not default. |
| 13 | +# When it is not provided, no version will be specified in the built package. |
| 14 | +# COMMIT: The commit of this project for which the cli is being built, for reference in the tool's "version" command. |
| 15 | +# Default to git's HEAD |
| 16 | +# LD_FLAGS: Build flags, for the tool's "version" command. Defaults to current date, commit and version (if any). |
| 17 | +COMMIT ?= $(shell git rev-parse --verify HEAD) |
| 18 | +LD_FLAGS ?= $(if $(VERSION),-X main.Version=${VERSION}) \ |
| 19 | + -X main.BuildDate=$(shell date "+%F-%T") \ |
| 20 | + -X main.Commit=${COMMIT} |
| 21 | + |
11 | 22 | # Resolve go version |
12 | 23 | ifdef GOTOOLCHAIN |
13 | 24 | GO_VERSION=$(GOTOOLCHAIN) |
|
16 | 27 | endif |
17 | 28 | DOCKER_GO_BUILD=go build -mod=readonly -a -installsuffix cgo -ldflags "$$LD_FLAGS" |
18 | 29 |
|
19 | | -require-commit: |
20 | | - if [ -z ${COMMIT} ]; then echo "Need to set COMMIT" && exit 1; fi; |
21 | | - |
22 | | -build-docker-multiarch: require-commit |
| 30 | +build-docker-multiarch: |
23 | 31 | docker buildx build \ |
24 | | - --sbom=true \ |
25 | | - --provenance=true \ |
26 | 32 | $(if $(DOCKER_BUILD_PUSH),--push) \ |
27 | 33 | -t ${REGISTRY}/${IMAGE}:${COMMIT} \ |
28 | 34 | $(if $(VERSION),-t ${REGISTRY}/${IMAGE}:latest) \ |
29 | 35 | $(if $(VERSION),-t ${REGISTRY}/${IMAGE}:${VERSION}) \ |
30 | 36 | $(if $(VERSION),-t ${REGISTRY}/${IMAGE}:$(shell echo $(VERSION) | cut -d '.' -f -2)) \ |
31 | 37 | $(if $(VERSION),-t ${REGISTRY}/${IMAGE}:$(shell echo $(VERSION) | cut -d '.' -f -1)) \ |
32 | 38 | --build-arg GO_VERSION=$(GO_VERSION) \ |
33 | | - --build-arg LD_FLAGS="$$LD_FLAGS" \ |
| 39 | + --build-arg LD_FLAGS="$(LD_FLAGS)" \ |
34 | 40 | --platform $(PLATFORM) \ |
35 | 41 | -f docker/Dockerfile . |
36 | 42 |
|
37 | | -_build_within_docker: OUTPUT = gotify-app |
| 43 | +_build_within_docker: OUTPUT = gotify-cli |
38 | 44 | _build_within_docker: |
39 | 45 | ${DOCKER_GO_BUILD} -o ${OUTPUT} |
40 | 46 |
|
41 | 47 | build: |
42 | | - if [ '$(shell echo "${GIT_TAG}" | cut -c 1 )' != 'v' ]; then exit 1; fi; |
43 | | - $(eval LD_FLAGS := -X main.Version=$(shell echo ${GIT_TAG} | cut -c 2-) -X main.BuildDate=$(shell date "+%F-%T") -X main.Commit=$(shell git rev-parse --verify HEAD)) |
44 | 48 | CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags="${LD_FLAGS}" -o build/gotify-cli-windows-amd64.exe cli.go |
45 | 49 | CGO_ENABLED=0 GOOS=windows GOARCH=386 go build -ldflags="${LD_FLAGS}" -o build/gotify-cli-windows-386.exe cli.go |
46 | 50 | CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="${LD_FLAGS}" -o build/gotify-cli-linux-amd64 cli.go |
|
0 commit comments