-
Notifications
You must be signed in to change notification settings - Fork 19
/
Makefile
212 lines (173 loc) · 5.46 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
APP = rhtap-cli
BIN_DIR ?= ./bin
BIN ?= $(BIN_DIR)/$(APP)
# Primary source code directories.
CMD ?= ./cmd/...
PKG ?= ./pkg/...
# Golang general flags for build and testing.
GOFLAGS ?= -v
GOFLAGS_TEST ?= -failfast -v -cover
CGO_ENABLED ?= 0
CGO_LDFLAGS ?=
# GitHub action current ref name, provided by the action context environment
# variables, and credentials needed to push the release.
GITHUB_REF_NAME ?= ${GITHUB_REF_NAME:-}
GITHUB_TOKEN ?= ${GITHUB_TOKEN:-}
# Container registry credentials.
IMAGE_REPO_USERNAME ?=
IMAGE_REPO_PASSWORD ?=
# Container registry repository, the hostname of the registry, or empty for
# default registry.
IMAGE_REPO ?= ghcr.io
# Container image namespace, usually the organization or user name.
IMAGE_NAMESPACE ?= redhat-appstudio
# Container image tag.
IMAGE_TAG ?= latest
# Fully qualified container image name.
IMAGE_FQN ?= $(IMAGE_REPO)/$(IMAGE_NAMESPACE)/$(APP):$(IMAGE_TAG)
# Determine the appropriate tar command based on the operating system.
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Darwin)
TAR := gtar
else
TAR := tar
endif
# Directory with the installer resources, scripts, Helm Charts, etc.
INSTALLER_DIR ?= ./installer
# Tarball with the installer resources.
INSTALLER_TARBALL ?= $(INSTALLER_DIR)/installer.tar
# Data to include in the tarball.
INSTALLER_TARBALL_DATA ?= $(shell find -L $(INSTALLER_DIR) -type f \
! -path "$(INSTALLER_TARBALL)" \
! -name embed.go \
)
.EXPORT_ALL_VARIABLES:
.default: build
#
# Build and Run
#
# Builds the application executable with installer resources embedded.
.PHONY: $(BIN)
$(BIN): installer-tarball
@echo "# Building '$(BIN)'"
@[ -d $(BIN_DIR) ] || mkdir -p $(BIN_DIR)
go build -o $(BIN) $(CMD) $(ARGS)
.PHONY: build
build: $(BIN)
# Uses goreleaser to create a snapshot build.
.PHONY: goreleaser-snapshot
goreleaser-snapshot: installer-tarball
goreleaser-snapshot: tool-goreleaser
goreleaser build --clean --snapshot $(ARGS)
snapshot: goreleaser-snapshot
# Runs the application with arbitrary ARGS.
.PHONY: run
run: installer-tarball
go run $(CMD) $(ARGS)
#
# Installer Tarball
#
# Creates a tarball with all resources required for the installation process.
.PHONY: installer-tarball
installer-tarball: $(INSTALLER_TARBALL)
$(INSTALLER_TARBALL): $(INSTALLER_TARBALL_DATA)
@echo "# Generating '$(INSTALLER_TARBALL)'"
@test -f "$(INSTALLER_TARBALL)" && rm -f "$(INSTALLER_TARBALL)" || true
@$(TAR) -C "$(INSTALLER_DIR)" -cpf "$(INSTALLER_TARBALL)" \
$(shell echo "$(INSTALLER_TARBALL_DATA)" | sed "s:\./installer/:./:g")
#
# Container Image
#
# By default builds the container image using Podman.
image: image-podman
# Builds the container image with Podman.
image-podman:
@echo "# Building '$(IMAGE_FQN)'..."
podman build --tag="$(IMAGE_FQN)" .
# Logins into the container registry.
login-buildah:
@echo "# Login into '$(IMAGE_REPO)' with user '$(IMAGE_REPO_USERNAME)'"
@buildah login \
--username="$(IMAGE_REPO_USERNAME)" \
--password="$(IMAGE_REPO_PASSWORD)" \
$(IMAGE_REPO)
# Builds the container image with Buildah.
image-buildah:
@echo "# Building '$(IMAGE_FQN)'..."
buildah bud --tag="$(IMAGE_FQN)" .
# Tags the container image with the provided arguments as tag.
image-buildah-tag: NEW_IMAGE_FQN = $(IMAGE_REPO)/$(IMAGE_NAMESPACE)/$(APP):$(ARGS)
image-buildah-tag:
@echo "# Tagging '$(IMAGE_FQN)' with $(ARGS)..."
buildah tag $(IMAGE_FQN) $(NEW_IMAGE_FQN)
# Pushes the container image to the registry.
image-buildah-push:
@echo "# Pushing '$(IMAGE_FQN)'..."
buildah push $(IMAGE_FQN)
#
# Tools
#
# Installs golangci-lint.
tool-golangci-lint: GOFLAGS =
tool-golangci-lint:
@which golangci-lint &>/dev/null || \
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest &>/dev/null
# Installs GitHub CLI ("gh").
tool-gh: GOFLAGS =
tool-gh:
@which gh >/dev/null 2>&1 || \
go install github.com/cli/cli/v2/cmd/gh@latest >/dev/null 2>&1
# Installs GoReleaser.
tool-goreleaser: GOFLAGS =
tool-goreleaser:
@which goreleaser >/dev/null 2>&1 || \
go install github.com/goreleaser/goreleaser@latest >/dev/null 2>&1
#
# Test and Lint
#
test: test-unit
# Runs the unit tests.
.PHONY: test-unit
test-unit: installer-tarball
go test $(GOFLAGS_TEST) $(CMD) $(PKG) $(ARGS)
# Uses golangci-lint to inspect the code base.
.PHONY: lint
lint: tool-golangci-lint
golangci-lint run ./...
#
# GitHub Release
#
# Asserts the required environment variables are set and the target release
# version starts with "v".
github-preflight:
ifeq ($(strip $(GITHUB_REF_NAME)),)
$(error variable GITHUB_REF_NAME is not set)
endif
ifeq ($(shell echo ${GITHUB_REF_NAME} |grep -v -E '^v'),)
@echo GITHUB_REF_NAME=\"${GITHUB_REF_NAME}\"
else
$(error invalid GITHUB_REF_NAME, it must start with "v")
endif
ifeq ($(strip $(GITHUB_TOKEN)),)
$(error variable GITHUB_TOKEN is not set)
endif
# Creates a new GitHub release with GITHUB_REF_NAME.
.PHONY: github-release-create
github-release-create: tool-gh
gh release view $(GITHUB_REF_NAME) >/dev/null 2>&1 || \
gh release create --generate-notes $(GITHUB_REF_NAME)
# Runs "goreleaser" to build the artifacts and upload them into the current
# release payload, it amends the release in progress with the application
# executables.
.PHONY: goreleaser-release
goreleaser-release: installer-tarball
goreleaser-release: tool-goreleaser
goreleaser-release: CGO_ENABLED = 0
goreleaser-release: GOFLAGS = -a
goreleaser-release:
goreleaser release --clean --fail-fast $(ARGS)
# Releases the GITHUB_REF_NAME.
github-release: \
github-preflight \
github-release-create \
goreleaser-release