You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.2 KiB

# detect operating system
ifeq ($(OS),Windows_NT)
CURRENT_OS := Windows
else
CURRENT_OS := $(shell uname -s)
endif
UNAME=$(shell uname -a)
X86_64=$(findstring x86_64, $(UNAME))
#GOBIN
GOBIN = $(shell pwd)/build/bin
GO ?= latest
# variables
NAME_TEST=test
NAME_STAGING=staging
NAME_MASTER=master
RD_SUFFIX=rd
BRANCH_TEST=$(NAME_TEST)-$(RD_SUFFIX)
BRANCH_STAGING=$(NAME_STAGING)-$(RD_SUFFIX)
BRANCH_MASTER=$(NAME_MASTER)-$(RD_SUFFIX)
CURR_MODEL_NAME=$(notdir $(shell pwd))
GIT_BRANCH=$(shell git rev-parse --abbrev-ref HEAD)
GIT_HASH=$(shell git rev-parse HEAD | cut -c 1-8)
BUILD_FLAGS = -tags netgo -ldflags "-X git.pandaminer.com/ox/build.GitBranch=$(GIT_BRANCH)"
ACR_PREFIX=registry.ap-southeast-1.aliyuncs.com/oxbtc-rd
K8S_NAMESPACE_PREFIX=ox
K8S_DEPLOY_PARAM_CURR_MODEL_NAME=$(CURR_MODEL_NAME)
K8S_DEPLOY_PARAM_IMAGE_TAG=$(GIT_BRANCH)-$(GIT_HASH)
K8S_DEPLOY_PARAM_RESOURCE_SUFFIX=$(GIT_BRANCH)
K8S_DEPLOY_PARAM_ARC_PREFIX=$(ACR_PREFIX)
TMP_FILE_DEPLOY=.tmp.deploy.txt
#GIT_BRANCH=master-rd
## git分支和命名空间的对应s
## master-rd 分支,对应命名空间 ox-rd
## test-rd 分支,对应命名空间 ox-test-rd
## staging-rd 分支,对应命名空间 ox-staging-rd
ifeq ($(GIT_BRANCH),$(BRANCH_MASTER))
K8S_DEPLOY_PARAM_CURR_K8S_NAMESPACE=$(K8S_NAMESPACE_PREFIX)-$(RD_SUFFIX)
else
K8S_DEPLOY_PARAM_CURR_K8S_NAMESPACE=$(K8S_NAMESPACE_PREFIX)-$(GIT_BRANCH)
endif
MAKE_ENV += K8S_DEPLOY_PARAM_CURR_MODEL_NAME K8S_DEPLOY_PARAM_IMAGE_TAG K8S_DEPLOY_PARAM_RESOURCE_SUFFIX K8S_DEPLOY_PARAM_ARC_PREFIX K8S_DEPLOY_PARAM_CURR_K8S_NAMESPACE
SHELL_EXPORT := $(foreach v,$(MAKE_ENV),$(v)='$($(v))' )
# GIT_BRANCH=11
all: build
# unittest:
# # @go test -v ./internal/...
buildquick:
@echo GIT_BRANCH=$(GIT_BRANCH) GIT_HASH=$(GIT_HASH) K8S_DEPLOY_PARAM_IMAGE_TAG=$(K8S_DEPLOY_PARAM_IMAGE_TAG)
@echo BUILD_FLAGS=$(BUILD_FLAGS)
@go build $(BUILD_FLAGS) -o ./pack/service ./
docker: clean buildquick
## 非 x86_64平台,不允许做docker镜像的构建
@if [ "$(X86_64)" = "" ] ; then\
echo !!!! not x86_64 platform , can not build docker image !!!!!!!!; \
exit 11; \
fi
## 非 test-rd、staging-rd、master-rd 分支,不允许做docker镜像构建
@echo ACR_PREFIX=$(ACR_PREFIX);
@echo CURR_MODEL_NAME=$(CURR_MODEL_NAME);
@echo GIT_BRANCH=$(GIT_BRANCH);
@if [ $(GIT_BRANCH) = $(BRANCH_TEST) ] || [ $(GIT_BRANCH) = $(BRANCH_STAGING) ] || [ $(GIT_BRANCH) = $(BRANCH_MASTER) ]; then\
cd pack;\
docker build -t $(CURR_MODEL_NAME):$(K8S_DEPLOY_PARAM_IMAGE_TAG) .;\
docker tag $(CURR_MODEL_NAME):$(K8S_DEPLOY_PARAM_IMAGE_TAG) $(ACR_PREFIX)/$(CURR_MODEL_NAME):$(K8S_DEPLOY_PARAM_IMAGE_TAG); \
docker push $(ACR_PREFIX)/$(CURR_MODEL_NAME):$(K8S_DEPLOY_PARAM_IMAGE_TAG); \
else \
echo !!!!! no docker building in branch $(GIT_BRANCH) !!!!!!; \
fi
deployK8s: docker
@echo $(SHELL_EXPORT);
@if [ $(GIT_BRANCH) = $(BRANCH_TEST) ] || [ $(GIT_BRANCH) = $(BRANCH_STAGING) ] || [ $(GIT_BRANCH) = $(BRANCH_MASTER) ]; then\
$(SHELL_EXPORT) envsubst < deployK8s.yaml > $(TMP_FILE_DEPLOY) ; kubectl apply -f $(TMP_FILE_DEPLOY) ; \
else \
echo !!!!! no docker building in branch $(GIT_BRANCH) !!!!!!; \
fi
build: buildquick
clean:
rm -rf ./build ./pack/service
.PHONY: build