-
Notifications
You must be signed in to change notification settings - Fork 7
/
Makefile
114 lines (95 loc) · 3.35 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
NAME ?= pbs-exporter
BUILD_DATE ?= $(shell date -Iseconds)
VERSION ?= $(shell git describe --tags --abbrev=0 2>/dev/null || git rev-parse --short HEAD)-local
# Adds "-dirty" suffix if there are uncommitted changes in the git repository
COMMIT_REF ?= $(shell git describe --dirty --always)
GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)
LICENSE ?= GPL-3
REPO_URL ?= https://github.com/natrontech/pbs-exporter
MAIL ?= info@natron.io
VENDOR ?= natrontech
DESCRIPTION ?= Export Proxmox Backup Server metrics for Prometheus
#########
# Go #
#########
.PHONY: go-tidy
go-tidy:
go mod tidy -compat=1.23
@echo "Go modules tidied."
.PHONY: go-update
go-update:
go get -u ./...
make go-tidy
@echo "Go modules updated."
.PHONY: go-build
go-build:
go build -o $(NAME) -trimpath -tags="netgo" -ldflags "-s -w -X main.Version=$(VERSION) -X main.Commit=$(COMMIT_REF) -X main.BuildTime=$(BUILD_DATE)" main.go
@echo "Go build completed."
#########
# TOOLS #
#########
KO_VERSION = v0.17.1
KO = $(shell pwd)/bin/ko
ko:
$(call go-install-tool,$(KO),github.com/google/ko@$(KO_VERSION))
#########
# Ko #
#########
KO_PLATFORM ?= linux/$(GOARCH)
KOCACHE ?= /tmp/ko-cache
KO_TAGS := $(VERSION)
# Function to check if VERSION contains "-local" or "-rc*"
ifeq ($(findstring -local,$(VERSION)),)
ifeq ($(findstring -rc,$(VERSION)),)
# If VERSION does not contain "-local" or "-rc*", add 'latest' to KO_TAGS
KO_TAGS := $(VERSION),latest
endif
endif
REGISTRY ?= ghcr.io
REPO ?= $(VENDOR)
KO_REPOSITORY := $(REGISTRY)/$(REPO)/$(NAME)
REGISTRY_PASSWORD ?= dummy
REGISTRY_USERNAME ?= dummy
LD_FLAGS := "-s \
-w \
-X main.Version=$(VERSION) \
-X main.Commit=$(COMMIT_REF) \
-X main.BuildTime=$(BUILD_DATE)"
LABELS := "--image-label=org.opencontainers.image.created=$(BUILD_DATE),$\
org.opencontainers.image.authors=$(MAIL),$\
org.opencontainers.image.url=$(REPO_URL),$\
org.opencontainers.image.documentation=$(REPO_URL),$\
org.opencontainers.image.source=$(REPO_URL),$\
org.opencontainers.image.version=$(VERSION),$\
org.opencontainers.image.revision=$(COMMIT_REF),$\
org.opencontainers.image.vendor=$(VENDOR),$\
org.opencontainers.image.licenses=$(LICENSE),$\
org.opencontainers.image.title=$(NAME),$\
org.opencontainers.image.description=$(DESCRIPTION),$\
org.opencontainers.image.base.name=cgr.dev/chainguard/static"
# Local ko build
.PHONY: ko-build-local
ko-build-local: ko
@echo Building $(NAME) $(KO_TAGS) for $(KO_PLATFORM) >&2
@LD_FLAGS=$(LD_FLAGS) KOCACHE=$(KOCACHE) KO_DOCKER_REPO=$(KO_REPOSITORY) \
$(KO) build ./ --bare --tags=$(KO_TAGS) $(LABELS) --push=false --local --platform=$(KO_PLATFORM) --sbom=none
# Ko publish image
.PHONY: ko-login
ko-login: ko
@$(KO) login $(REGISTRY) --username $(REGISTRY_USERNAME) --password $(REGISTRY_PASSWORD)
.PHONY: ko-publish
ko-publish: ko-login
@LD_FLAGS=$(LD_FLAGS) KOCACHE=$(KOCACHE) KO_DOCKER_REPO=$(KO_REPOSITORY) \
$(KO) build ./ --bare --tags=$(KO_TAGS) $(LABELS) --sbom=none
###########
# Helpers #
###########
# go-install-tool will 'go install' any package $2 and install it to $1.
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
define go-install-tool
@[ -f $(1) ] || { \
set -e ;\
GOBIN=$(PROJECT_DIR)/bin go install $(2) ;\
}
endef