-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommon.mk
129 lines (108 loc) · 3.64 KB
/
common.mk
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
SHELL := /bin/bash
ARCH ?= $(shell go env GOARCH)
CURRENT_USER := $(if $(GITHUB_USER),$(GITHUB_USER),$(shell id -u -n))
## Location to install dependencies to
LOCALBIN ?= $(shell pwd)/bin
$(LOCALBIN):
mkdir -p $(LOCALBIN)
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.26.0
## Tool Binaries
MELANGE ?= $(LOCALBIN)/melange
APKO ?= $(LOCALBIN)/apko
## Version to use for building
VERSION ?= $(shell git describe --tags --match='[0-9]*.[0-9]*.[0-9]*' --abbrev=4)
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
else
GOBIN=$(shell go env GOBIN)
endif
image-tag = $(shell echo "$1" | sed 's/+/-/')
.PHONY: print-%
print-%:
@echo -n $($*)
.PHONY: check-env-%
check-env-%:
@ if [ "${${*}}" = "" ]; then \
echo "Environment variable $* not set"; \
exit 1; \
fi
melange: $(MELANGE)
$(MELANGE): $(LOCALBIN)
go install chainguard.dev/melange@latest && \
test -s $(GOBIN)/melange && \
ln -sf $(GOBIN)/melange $(LOCALBIN)/melange
apko: $(APKO)
$(APKO): $(LOCALBIN)
go install chainguard.dev/apko@latest && \
test -s $(GOBIN)/apko && \
ln -sf $(GOBIN)/apko $(LOCALBIN)/apko
CHAINGUARD_TOOLS_USE_DOCKER = 0
ifeq ($(CHAINGUARD_TOOLS_USE_DOCKER),"1")
MELANGE_CACHE_DIR ?= /go/pkg/mod
APKO_CMD = docker run -v $(shell pwd):/work -w /work -v $(shell pwd)/build/.docker:/root/.docker cgr.dev/chainguard/apko
MELANGE_CMD = docker run --privileged --rm -v $(shell pwd):/work -w /work -v "$(shell go env GOMODCACHE)":${MELANGE_CACHE_DIR} cgr.dev/chainguard/melange
else
MELANGE_CACHE_DIR ?= cache/.melange-cache
APKO_CMD = apko
MELANGE_CMD = melange
endif
$(MELANGE_CACHE_DIR):
mkdir -p $(MELANGE_CACHE_DIR)
.PHONY: apko-build
apko-build: ARCHS ?= $(ARCH)
apko-build: check-env-IMAGE apko-template
cd build && ${APKO_CMD} \
build apko.yaml ${IMAGE} apko.tar \
--arch ${ARCHS}
.PHONY: apko-build-and-publish
apko-build-and-publish: ARCHS ?= $(ARCH)
apko-build-and-publish: check-env-IMAGE apko-template
@bash -c 'set -o pipefail && cd build && ${APKO_CMD} publish apko.yaml ${IMAGE} --arch ${ARCHS} | tee digest'
$(MAKE) apko-output-image
.PHONY: apko-login
apko-login:
rm -f build/.docker/config.json
@ { [ "${PASSWORD}" = "" ] || [ "${USERNAME}" = "" ] ; } || \
${APKO_CMD} \
login -u "${USERNAME}" \
--password "${PASSWORD}" "${REGISTRY}"
.PHONY: apko-print-pkg-version
apko-print-pkg-version: ARCHS ?= $(ARCH)
apko-print-pkg-version: apko-template check-env-PACKAGE_NAME
cd build && \
${APKO_CMD} show-packages apko.yaml --arch=${ARCHS} | \
grep ${PACKAGE_NAME} | \
cut -s -d" " -f2 | \
head -n1
.PHONY: apko-output-image
apko-output-image: check-env-IMAGE
@digest=$$(cut -s -d'@' -f2 build/digest); \
if [ -z "$$digest" ]; then \
echo "error: no image digest found" >&2; \
exit 1; \
fi ; \
echo "$(IMAGE)@$$digest" > build/image
.PHONY: melange-build
melange-build: ARCHS ?= $(ARCH)
melange-build: MELANGE_SOURCE_DIR ?= .
melange-build: $(MELANGE_CACHE_DIR) melange-template
mkdir -p build
${MELANGE_CMD} \
keygen build/melange.rsa
${MELANGE_CMD} \
build build/melange.yaml \
--arch ${ARCHS} \
--signing-key build/melange.rsa \
--cache-dir=$(MELANGE_CACHE_DIR) \
--source-dir $(MELANGE_SOURCE_DIR) \
--out-dir build/packages/
.PHONY: melange-template
melange-template: check-env-MELANGE_CONFIG check-env-PACKAGE_VERSION
mkdir -p build
envsubst '$${PACKAGE_VERSION}' < ${MELANGE_CONFIG} > build/melange.yaml
.PHONY: apko-template
apko-template: check-env-APKO_CONFIG check-env-PACKAGE_VERSION
mkdir -p build
envsubst '$${PACKAGE_VERSION}' < ${APKO_CONFIG} > build/apko.yaml