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.

138 lines
4.3 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_DEV=develop
NAME_TEST=test
NAME_STAGING=staging
NAME_MASTER=master
RD_SUFFIX=rd
BRANCH_DEV=$(NAME_DEV)-$(RD_SUFFIX)
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
TMP_DATA_DIR=tmpData
CONFIGFILES_PROJECT_NAME=configfiles
#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/...
gitReset:
@echo ==gitReset==
@git fetch --all;
@git reset --hard origin/$(GIT_BRANCH);
buildquick: gitReset
@echo ==buildquick==
@go mod tidy;
@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 configfiles
@echo ==build docker image and push ==
## 非 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_DEV) || $(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 ==deploy to K8s==
@echo $(SHELL_EXPORT);
@if [[ $(GIT_BRANCH) = $(BRANCH_DEV) || $(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) ; \
kubectl replace -f $(TMP_FILE_DEPLOY) --force; \
else \
echo !!!!! no docker building in branch $(GIT_BRANCH) !!!!!!; \
fi
configfiles:
@echo ==get configfiles==
@echo $(GIT_BRANCH)
@if [[ $(GIT_BRANCH) = $(BRANCH_MASTER) ]]; then\
mkdir -p $(TMP_DATA_DIR); \
cd $(TMP_DATA_DIR); \
[ ! -e $(CONFIGFILES_PROJECT_NAME) ] && git clone https://git.pandaminer.com/ox/$(CONFIGFILES_PROJECT_NAME); \
cd $(CONFIGFILES_PROJECT_NAME); \
echo currDir:; \
pwd; \
git fetch; \
git checkout $(GIT_BRANCH); \
git reset --hard origin/$(GIT_BRANCH); \
cd ../..; \
echo currDir:; \
pwd; \
cp ./$(TMP_DATA_DIR)/$(CONFIGFILES_PROJECT_NAME)/$(CURR_MODEL_NAME)/pack/.service.config.yaml ./pack/.service.config.yaml; \
else \
echo !!!!! no configfiles process in branch $(GIT_BRANCH) !!!!!!; \
fi
build: buildquick
clean:
rm -rf ./build ./pack/service
.PHONY: build