-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
98 lines (81 loc) · 3.13 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
#!/usr/bin/env make
.DEFAULT_GOAL := help
.DEFAULT_SHELL := /bin/bash
GOARCH := $(shell go env GOARCH)
GOOS := $(shell go env GOOS)
GO := $(GOBIN)/go
GOLINT ?= $(shell command -v golangci-lint 2>/dev/null)
EXECUTABLE ?= dist/simple-prober
#EXECUTABLE ?= dist/simple-prober-$(GOOS)_$(GOARCH)
DOCKER_REGISTRY ?= icr.io
DOCKER_REGISTRY_NAMESPACE ?= qc-production-ext-images
DOCKER_SERVICE_NAME ?= simple-prober
DOCKER_IMAGE := $(DOCKER_REGISTRY)/$(DOCKER_REGISTRY_NAMESPACE)/$(DOCKER_SERVICE_NAME)
DOCKER_TARGET ?= runtime
PROJECT_CHANGESET := $(shell git rev-parse --verify HEAD 2>/dev/null)
define assert-set
@$(if $($1),,$(error $(1) environment variable is not defined))
endef
define assert-command
@$(if $(shell command -v $1 2>/dev/null),,$(error $(1) command not found))
endef
define assert-file
@$(if $(wildcard $($1) 2>/dev/null),,$(error $($1) does not exist))
endef
.PHONY: help
help: ## Shows this pretty help screen
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make <target>\n\nTargets:\n"} /^[a-zA-Z\/_-]+:.*?##/ { printf " %-20s %s\n", $$1, $$2 }' $(MAKEFILE_LIST)
.PHONY: lint
lint: ## Lint the source code
$(call assert-command,$(GOLINT))
@$(GOLINT) run ./...
.PHONY: clean
clean: ## Clean the project executable
@$(GO) clean -v ./...
@rm -f $(EXECUTABLE)
.PHONY: build
build: ## Build the project
ifdef PROJECT_VERSION
@$(GO) build -v -ldflags="-X github.com/jjuarez/simple-prober/cmd.Version='v$(PROJECT_VERSION)+$(PROJECT_CHANGESET)'" -o $(EXECUTABLE) main.go
else
@$(GO) build -v -ldflags="-X github.com/jjuarez/simple-prober/cmd.Version='$(PROJECT_CHANGESET)'" -o $(EXECUTABLE) main.go
endif
.PHONY: test
test: ## Unit tests
@$(GO) test -v ./...
.PHONY: icr.io/login
icr.io/login:
$(call assert-set,IBMCLOUD_API_KEY)
@echo $(IBMCLOUD_API_KEY)|docker login --username iamapikey --password-stdin $(DOCKER_REGISTRY)
.PHONY: docker/build
docker/build: ## Makes the Docker build and takes care of the remote cache by target
ifdef PROJECT_VERSION
@docker image build \
--build-arg BUILDKIT_INLINE_CACHE=1 \
--build-arg VERSION=v$(PROJECT_VERSION)+$(PROJECT_CHANGESET) \
--cache-from $(DOCKER_IMAGE):latest \
--tag $(DOCKER_IMAGE):$(PROJECT_CHANGESET) \
--tag $(DOCKER_IMAGE):latest \
--target $(DOCKER_TARGET) \
--file Dockerfile \
.
else
@docker image build \
--build-arg BUILDKIT_INLINE_CACHE=1 \
--build-arg VERSION=$(PROJECT_CHANGESET) \
--cache-from $(DOCKER_IMAGE):latest \
--tag $(DOCKER_IMAGE):$(PROJECT_CHANGESET) \
--tag $(DOCKER_IMAGE):latest \
--file Dockerfile \
.
endif
@docker image push $(DOCKER_IMAGE):latest
.PHONY: docker/release
docker/release: docker/build ## Builds and release over the Docker registry the image
@docker image push $(DOCKER_IMAGE):$(PROJECT_CHANGESET)
ifdef PROJECT_VERSION
@docker image tag $(DOCKER_IMAGE):$(PROJECT_CHANGESET) $(DOCKER_IMAGE):$(PROJECT_VERSION)
@docker image push $(DOCKER_IMAGE):$(PROJECT_VERSION)
else
$(warning The release rule should have a PROJECT_VERSION defined)
endif