forked from openshift/ocm-container
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
160 lines (126 loc) · 5.3 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
PROJECT_NAME=ocm-container
CONTAINER_ENGINE:=$(shell command -v podman 2>/dev/null || command -v docker 2>/dev/null)
ifeq ($(CONTAINER_ENGINE),)
$(error ERROR: container engine not foumd, install podman or docker and run make again)
endif
REGISTRY_USER?=$(QUAY_USER)
REGISTRY_TOKEN?=$(QUAY_TOKEN)
IMAGE_REGISTRY?=quay.io
IMAGE_REPOSITORY?=app-sre
IMAGE_NAME?=$(PROJECT_NAME)
IMAGE_URI=$(IMAGE_REGISTRY)/$(IMAGE_REPOSITORY)/$(IMAGE_NAME)
GIT_REVISION=$(shell git rev-parse --short=7 HEAD)
TAG?=latest
BUILD_ARGS?=
ARCHITECTURE?=$(shell arch)
# Golang-specific
unexport GOFLAGS
GOOS?=linux
TESTOPTS ?=
GOARCH?=amd64
GOENV=GOOS=${GOOS} GOARCH=${GOARCH} CGO_ENABLED=0 GOFLAGS=
GOPATH := $(shell go env GOPATH)
HOME?=$(shell mktemp -d)
# Ensure go modules are enabled:
export GO111MODULE=on
export GOPROXY=https://proxy.golang.org
# Disable CGO so that we always generate static binaries:
export CGO_ENABLED=0
GOLANGCI_LINT_VERSION=v1.51.2
GORELEASER_VERSION=v1.24.0
GORELEASER_CONFIG=.goreleaser.yaml
# Number of cores to use when building assets
GORELEASER_CORES=4
GORELEASER_ADDITIONAL_ARGS=
export GITHUB_TOKEN?=
.Phony: checkEnv
checkEnv:
@test "${CONTAINER_ENGINE}" != "" || (echo "CONTAINER_ENGINE must be defined" && exit 1)
@${CONTAINER_ENGINE} version || (echo "CONTAINER_ENGINE must be installed and in PATH" && exit 1)
.PHONY: init
init:
bash init.sh
.PHONY: build-minimal
build-minimal:
@${CONTAINER_ENGINE} build $(BUILD_ARGS) -f Containerfile -t $(IMAGE_NAME)-minimal:$(TAG) --target=ocm-container-minimal
.PHONY: build
build:
@${CONTAINER_ENGINE} build $(BUILD_ARGS) -f Containerfile -t $(IMAGE_NAME):$(TAG) --target=ocm-container
.PHONY: build-image-amd64
build-image-amd64:
@${CONTAINER_ENGINE} build $(BUILD_ARGS) -f Containerfile --platform=linux/amd64 -t $(IMAGE_NAME):$(TAG)-amd64 --target=ocm-container
.PHONY: build-image-arm64
build-image-arm64:
@${CONTAINER_ENGINE} build $(BUILD_ARGS) -f Containerfile --platform=linux/arm64 -t $(IMAGE_NAME):$(TAG)-arm64 --target=ocm-container
.PHONY: registry-login
registry-login:
@test "${REGISTRY_USER}" != "" && test "${REGISTRY_TOKEN}" != "" || (echo "REGISTRY_USER and REGISTRY_TOKEN must be defined" && exit 1)
@${CONTAINER_ENGINE} login -u="${REGISTRY_USER}" -p="${REGISTRY_TOKEN}" "$(IMAGE_REGISTRY)"
.PHONY: tag
tag:
$(eval BUILD_ID=$(shell ${CONTAINER_ENGINE} image inspect --format '{{slice .ID 7 19}}' $(IMAGE_NAME):$(TAG)))
# Our image tag uses the format sha256: starting our slice later to exclude that
${CONTAINER_ENGINE} tag $(IMAGE_NAME):$(TAG) $(IMAGE_URI):$(BUILD_ID)-$(GIT_REVISION)-$(ARCHITECTURE)
${CONTAINER_ENGINE} tag $(IMAGE_NAME):$(TAG) $(IMAGE_URI):$(BUILD_ID)-$(ARCHITECTURE)
${CONTAINER_ENGINE} tag $(IMAGE_NAME):$(TAG) $(IMAGE_URI):latest-$(ARCHITECTURE)
.PHONY: push
push:
$(eval BUILD_ID=$(shell ${CONTAINER_ENGINE} image inspect --format '{{slice .ID 7 19}}' $(IMAGE_NAME):$(TAG)))
${CONTAINER_ENGINE} push $(IMAGE_URI):$(BUILD_ID)-$(GIT_REVISION)-$(ARCHITECTURE)
${CONTAINER_ENGINE} push $(IMAGE_URI):$(BUILD_ID)-$(ARCHITECTURE)
${CONTAINER_ENGINE} push $(IMAGE_URI):latest-$(ARCHITECTURE)
.PHONY: build-manifest
build-manifest:
# builds the joint manifest for a new dual-arch container definition
# we're currently just going to use the build id from the AMD version of the image to tag here
$(eval AMD_BUILD_ID=$(shell ${CONTAINER_ENGINE} image inspect --format '{{slice .ID 7 19}}' $(IMAGE_NAME):latest-amd64))
# $(eval ARM_BUILD_ID=$(shell ${CONTAINER_ENGINE} image inspect --format '{{slice .ID 7 19}}' $(IMAGE_NAME):latest-arm64))
${CONTAINER_ENGINE} manifest exists $(IMAGE_URI):$(AMD_BUILD_ID)-$(GIT_REVISION) && ${CONTAINER_ENGINE} manifest rm $(IMAGE_URI):$(AMD_BUILD_ID)-$(GIT_REVISION) || true
${CONTAINER_ENGINE} manifest create $(IMAGE_URI):$(AMD_BUILD_ID)-$(GIT_REVISION) $(IMAGE_URI):$(AMD_BUILD_ID)-$(GIT_REVISION)-amd64 # $(IMAGE_URI):$(ARM_BUILD_ID)-$(GIT_REVISION)-arm64
${CONTAINER_ENGINE} manifest exists $(IMAGE_URI):latest && ${CONTAINER_ENGINE} manifest rm $(IMAGE_URI):latest || true
${CONTAINER_ENGINE} manifest create $(IMAGE_URI):latest $(IMAGE_URI):$(AMD_BUILD_ID)-$(GIT_REVISION)-amd64 # $(IMAGE_URI):$(ARM_BUILD_ID)-$(GIT_REVISION)-arm64
.PHONY: push-manifest
push-manifest:
$(eval AMD_BUILD_ID=$(shell ${CONTAINER_ENGINE} image inspect --format '{{slice .ID 7 19}}' $(IMAGE_NAME):latest-amd64))
${CONTAINER_ENGINE} manifest push $(IMAGE_URI):$(AMD_BUILD_ID)-$(GIT_REVISION)
${CONTAINER_ENGINE} manifest push $(IMAGE_URI):latest
.PHONY: tag-n-push
tag-n-push: registry-login tag push
# Golang-related
.PHONY: go_build
go_build:
mod fmt lint test build_snapshot
.PHONY: build_binary
build_binary:
$(GOENV) go build -o build/$(PROJECT_NAME) .
.PHONY: mod
mod:
go mod tidy
.PHONY: test
test:
go test ./... -v $(TESTOPTS)
# TODO: Set this up
.PHONY: coverage
coverage:
hack/codecov.sh
.PHONY: lint
lint:
$(GOPATH)/bin/golangci-lint run --timeout 5m
.PHONY: release
release:
ifndef GITHUB_TOKEN
$(error GITHUB_TOKEN is undefined)
endif
goreleaser check --config $(GORELEASER_CONFIG)
goreleaser release --clean --config $(GORELEASER_CONFIG) --parallelism $(GORELEASER_CORES) $(GORELEASER_ADDITIONAL_ARGS)
.PHONY: build-snapshot
build-snapshot:
goreleaser build --clean --snapshot --single-target=true --config $(GORELEASER_CONFIG)
.PHONY: fmt
fmt:
gofmt -s -l -w cmd pkg tests
.PHONY: clean
clean:
rm -rf \
build/*
dist/*