This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
forked from slashdevops/idp-scim-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
224 lines (198 loc) · 10.8 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
213
214
215
216
217
218
219
220
221
222
223
224
.DELETE_ON_ERROR: clean
EXECUTABLES = go
K := $(foreach exec,$(EXECUTABLES),\
$(if $(shell which $(exec)),some string,$(error "No $(exec) in PATH)))
PROJECT_NAME ?= idp-scim-sync
PROJECT_NAMESPACE ?= slashdevops
PROJECT_MODULES_PATH := $(shell ls -d cmd/*)
PROJECT_MODULES_NAME := $(foreach dir_name, $(PROJECT_MODULES_PATH), $(shell basename $(dir_name)) )
PROJECT_DEPENDENCIES := $(shell go list -m -f '{{if not (or .Indirect .Main)}}{{.Path}}{{end}}' all)
GIT_VERSION ?= $(shell git rev-parse --abbrev-ref HEAD | cut -d "/" -f 2)
GIT_REVISION ?= $(shell git rev-parse HEAD | tr -d '\040\011\012\015\n')
GIT_BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD | tr -d '\040\011\012\015\n')
GIT_USER ?= $(shell git config --get user.name | tr -d '\040\011\012\015\n')
BUILD_DATE ?= $(shell date +'%Y-%m-%dT%H:%M:%S')
BUILD_DIR := ./build
DIST_DIR := ./dist
DIST_ASSEST_DIR := $(DIST_DIR)/assets
GO_LDFLAGS ?= -ldflags "-X github.com/$(PROJECT_NAMESPACE)/$(PROJECT_NAME)/internal/version.Version=$(GIT_VERSION) -X github.com/$(PROJECT_NAMESPACE)/$(PROJECT_NAME)/internal/version.Revision=$(GIT_REVISION) -X github.com/$(PROJECT_NAMESPACE)/$(PROJECT_NAME)/internal/version.Branch=$(GIT_BRANCH) -X github.com/$(PROJECT_NAMESPACE)/$(PROJECT_NAME)/internal/version.BuildUser=\"$(GIT_USER)\" -X github.com/$(PROJECT_NAMESPACE)/$(PROJECT_NAME)/internal/version.BuildDate=$(BUILD_DATE)"
GO_CGO_ENABLED ?= 0
GO_OPTS ?= -v
GO_OS ?= darwin linux windows
GO_ARCH ?= arm64 amd64
# avoid mocks in tests
GO_FILES := $(shell go list ./... | grep -v /mocks/)
CONTAINER_OS ?= linux
CONTAINER_ARCH ?= arm64v8 amd64
CONTAINER_NAMESPACE ?= $(PROJECT_NAMESPACE)
CONTAINER_IMAGE_NAME ?= $(PROJECT_NAME)
DOCKER_CONTAINER_REPO ?= docker.io
GITHUB_CONTAINER_REPO ?= ghcr.io
AWS_ECR_CONTAINER_REPO ?= public.ecr.aws/l2n7y5s7
AWS_SAM_PROJECT_NAME ?= idpscim
AWS_SAM_OS ?= linux
AWS_SAM_ARCH ?= amd64
all: clean test build
mod-update: tidy
$(foreach dep, $(PROJECT_DEPENDENCIES), $(shell go get -u $(dep)))
go mod tidy
tidy:
go mod tidy
fmt:
@go fmt $(GO_FILES)
vet:
go vet $(GO_FILES)
lint:
golangci-lint run
generate:
go generate $(GO_FILES)
test: tidy fmt vet
go test -race -covermode=atomic -coverprofile coverage.out -tags=unit $(GO_FILES)
test-coverage: test
go tool cover -html=coverage.out
build:
$(foreach proj_mod, $(PROJECT_MODULES_NAME), \
$(shell CGO_ENABLED=$(GO_CGO_ENABLED) go build $(GO_LDFLAGS) $(GO_OPTS) -o ./$(BUILD_DIR)/$(proj_mod) ./cmd/$(proj_mod)/ ) \
)
build-dist: build
$(foreach GOOS, $(GO_OS), \
$(foreach GOARCH, $(GO_ARCH), \
$(foreach proj_mod, $(PROJECT_MODULES_NAME), \
$(shell GOOS=$(GOOS) GOARCH=$(GOARCH) CGO_ENABLED=$(GO_CGO_ENABLED) go build $(GO_LDFLAGS) $(GO_OPTS) -o ./$(DIST_DIR)/$(PROJECT_NAME)-$(GOOS)-$(GOARCH)/$(proj_mod) ./cmd/$(proj_mod)/ ) \
) \
) \
)
build-dist-zip:
mkdir ./$(DIST_ASSEST_DIR);
$(foreach GOOS, $(GO_OS), \
$(foreach GOARCH, $(GO_ARCH), \
$(foreach proj_mod, $(PROJECT_MODULES_NAME), \
zip --junk-paths -r ./$(DIST_ASSEST_DIR)/$(PROJECT_NAME)-$(GOOS)-$(GOARCH).zip ./$(DIST_DIR)/$(PROJECT_NAME)-$(GOOS)-$(GOARCH); \
shasum -a 256 ./$(DIST_ASSEST_DIR)/$(PROJECT_NAME)-$(GOOS)-$(GOARCH).zip | cut -d " " -f 1 > ./$(DIST_ASSEST_DIR)/$(PROJECT_NAME)-$(GOOS)-$(GOARCH).sha256; \
) \
) \
)
clean:
rm -rf $(BUILD_DIR) $(DIST_DIR) ./*.out .aws-sam/ build.toml ./packaged.yaml
# This target is used by AWS SAM build command
# and was added to build the binary using custom flags
# Ref:
# + https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/building-custom-runtimes.html
# + https://jiangsc.me/2021/01/24/Lessons-Learnt-On-Deploying-GO-Lambda-Application-on-AWS/
build-LambdaFunction:
CGO_ENABLED=$(GO_CGO_ENABLED) GOOS=$(AWS_SAM_OS) GOARCH=$(AWS_SAM_ARCH) go build $(GO_LDFLAGS) $(GO_OPTS) -o ./$(BUILD_DIR)/AWS_SAM/$(AWS_SAM_PROJECT_NAME) ./cmd/$(AWS_SAM_PROJECT_NAME)/
mkdir -p $(ARTIFACTS_DIR)/dist/$(AWS_SAM_PROJECT_NAME)-$(AWS_SAM_OS)-$(AWS_SAM_ARCH)
cp ./$(BUILD_DIR)/AWS_SAM/$(AWS_SAM_PROJECT_NAME) $(ARTIFACTS_DIR)/dist/$(AWS_SAM_PROJECT_NAME)-$(AWS_SAM_OS)-$(AWS_SAM_ARCH)/
container-build: build-dist
$(foreach OS, $(CONTAINER_OS), \
$(foreach ARCH, $(CONTAINER_ARCH), \
$(if $(findstring v, $(ARCH)), $(eval BIN_ARCH = arm64), $(eval BIN_ARCH = $(ARCH)) ) \
echo "Building $(PROJECT_NAME) for OS=$(OS) ARCH=$(ARCH) and BIN_ARCH=$(BIN_ARCH)"; \
docker build \
--build-arg ARCH=$(ARCH) \
--build-arg BIN_ARCH=$(BIN_ARCH) \
--build-arg OS=$(OS) \
--tag $(DOCKER_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):$(GIT_VERSION)-$(OS)-$(ARCH) \
--tag $(DOCKER_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):latest-$(OS)-$(ARCH) \
--tag $(GITHUB_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):$(GIT_VERSION)-$(OS)-$(ARCH) \
--tag $(GITHUB_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):latest-$(OS)-$(ARCH) \
--tag $(AWS_ECR_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):$(GIT_VERSION)-$(OS)-$(ARCH) \
--tag $(AWS_ECR_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):latest-$(OS)-$(ARCH) \
./.; \
))
container-publish-docker: container-build
$(foreach OS, $(CONTAINER_OS), \
$(foreach ARCH, $(CONTAINER_ARCH), \
$(if $(findstring v, $(ARCH)), $(eval BIN_ARCH = arm64), $(eval BIN_ARCH = $(ARCH)) ) \
\
echo "Building $(PROJECT_NAME) for OS=$(OS) ARCH=$(ARCH) and BIN_ARCH=$(BIN_ARCH)"; \
docker push "$(DOCKER_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):$(GIT_VERSION)-$(OS)-$(ARCH)"; \
docker push "$(DOCKER_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):latest-$(OS)-$(ARCH)"; \
\
docker manifest create --amend \
"$(DOCKER_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):$(GIT_VERSION)" \
"$(DOCKER_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):$(GIT_VERSION)-$(OS)-$(ARCH)"; \
docker manifest annotate \
"$(DOCKER_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):$(GIT_VERSION)" \
"$(DOCKER_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):$(GIT_VERSION)-$(OS)-$(ARCH)" \
--os $(OS) --arch $(BIN_ARCH) \
$(if $(findstring v, $(ARCH)), --variant "v8", ) \
; \
\
docker manifest create --amend \
"$(DOCKER_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):latest" \
"$(DOCKER_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):latest-$(OS)-$(ARCH)"; \
docker manifest annotate \
"$(DOCKER_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):latest" \
"$(DOCKER_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):latest-$(OS)-$(ARCH)" \
--os $(OS) --arch $(BIN_ARCH) \
$(if $(findstring v, $(ARCH)), --variant "v8", ) \
; \
\
docker manifest push "$(DOCKER_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):latest"; \
docker manifest push "$(DOCKER_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):$(GIT_VERSION)"; \
))
container-publish-github: container-build
$(foreach OS, $(CONTAINER_OS), \
$(foreach ARCH, $(CONTAINER_ARCH), \
$(if $(findstring v, $(ARCH)), $(eval BIN_ARCH = arm64), $(eval BIN_ARCH = $(ARCH)) ) \
\
echo "Building $(PROJECT_NAME) for OS=$(OS) ARCH=$(ARCH) and BIN_ARCH=$(BIN_ARCH)"; \
docker push "$(GITHUB_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):$(GIT_VERSION)-$(OS)-$(ARCH)"; \
docker push "$(GITHUB_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):latest-$(OS)-$(ARCH)"; \
\
docker manifest create --amend \
"$(GITHUB_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):$(GIT_VERSION)" \
"$(GITHUB_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):$(GIT_VERSION)-$(OS)-$(ARCH)"; \
docker manifest annotate \
"$(GITHUB_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):$(GIT_VERSION)" \
"$(GITHUB_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):$(GIT_VERSION)-$(OS)-$(ARCH)" \
--os $(OS) --arch $(BIN_ARCH) \
$(if $(findstring v, $(ARCH)), --variant "v8", ) \
; \
\
docker manifest create --amend \
"$(GITHUB_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):latest" \
"$(GITHUB_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):latest-$(OS)-$(ARCH)"; \
docker manifest annotate \
"$(GITHUB_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):latest" \
"$(GITHUB_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):latest-$(OS)-$(ARCH)" \
--os $(OS) --arch $(BIN_ARCH) \
$(if $(findstring v, $(ARCH)), --variant "v8", ) \
; \
\
docker manifest push "$(GITHUB_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):latest"; \
docker manifest push "$(GITHUB_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):$(GIT_VERSION)"; \
))
container-publish-aws-ecr: container-build
$(foreach OS, $(CONTAINER_OS), \
$(foreach ARCH, $(CONTAINER_ARCH), \
$(if $(findstring v, $(ARCH)), $(eval BIN_ARCH = arm64), $(eval BIN_ARCH = $(ARCH)) ) \
\
echo "Building $(PROJECT_NAME) for OS=$(OS) ARCH=$(ARCH) and BIN_ARCH=$(BIN_ARCH)"; \
docker push "$(AWS_ECR_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):$(GIT_VERSION)-$(OS)-$(ARCH)"; \
docker push "$(AWS_ECR_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):latest-$(OS)-$(ARCH)"; \
\
docker manifest create --amend \
"$(AWS_ECR_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):$(GIT_VERSION)" \
"$(AWS_ECR_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):$(GIT_VERSION)-$(OS)-$(ARCH)"; \
docker manifest annotate \
"$(AWS_ECR_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):$(GIT_VERSION)" \
"$(AWS_ECR_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):$(GIT_VERSION)-$(OS)-$(ARCH)" \
--os $(OS) --arch $(BIN_ARCH) \
$(if $(findstring v, $(ARCH)), --variant "v8", ) \
; \
\
docker manifest create --amend \
"$(AWS_ECR_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):latest" \
"$(AWS_ECR_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):latest-$(OS)-$(ARCH)"; \
docker manifest annotate \
"$(AWS_ECR_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):latest" \
"$(AWS_ECR_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):latest-$(OS)-$(ARCH)" \
--os $(OS) --arch $(BIN_ARCH) \
$(if $(findstring v, $(ARCH)), --variant "v8", ) \
; \
\
docker manifest push "$(AWS_ECR_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):latest"; \
docker manifest push "$(AWS_ECR_CONTAINER_REPO)/$(CONTAINER_NAMESPACE)/$(CONTAINER_IMAGE_NAME):$(GIT_VERSION)"; \
))