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.

131 lines
4.0 KiB

  1. # detect operating system
  2. ifeq ($(OS),Windows_NT)
  3. CURRENT_OS := Windows
  4. else
  5. CURRENT_OS := $(shell uname -s)
  6. endif
  7. UNAME=$(shell uname -a)
  8. X86_64=$(findstring x86_64, $(UNAME))
  9. #GOBIN
  10. GOBIN = $(shell pwd)/build/bin
  11. GO ?= latest
  12. # variables
  13. NAME_DEV=develop
  14. NAME_MASTER=master
  15. RD_SUFFIX=rd
  16. BRANCH_DEV=$(NAME_DEV)-$(RD_SUFFIX)
  17. BRANCH_MASTER=$(NAME_MASTER)-$(RD_SUFFIX)
  18. CURR_MODEL_NAME=$(notdir $(shell pwd))
  19. GIT_BRANCH=$(shell git rev-parse --abbrev-ref HEAD)
  20. GIT_HASH=$(shell git rev-parse HEAD | cut -c 1-8)
  21. BUILD_FLAGS = -tags netgo -ldflags "-X git.pandaminer.com/ox/build.GitBranch=$(GIT_BRANCH)"
  22. ACR_PREFIX=registry.ap-southeast-1.aliyuncs.com/oxbtc-rd
  23. K8S_NAMESPACE_PREFIX=halley
  24. K8S_DEPLOY_PARAM_CURR_MODEL_NAME=$(CURR_MODEL_NAME)
  25. K8S_DEPLOY_PARAM_IMAGE_TAG=$(GIT_BRANCH)-$(GIT_HASH)
  26. K8S_DEPLOY_PARAM_RESOURCE_SUFFIX=$(GIT_BRANCH)
  27. K8S_DEPLOY_PARAM_ARC_PREFIX=$(ACR_PREFIX)
  28. TMP_FILE_DEPLOY=.tmp.deploy.txt
  29. TMP_DATA_DIR=tmpData
  30. CONFIGFILES_PROJECT_NAME=configfiles
  31. #GIT_BRANCH=master-rd
  32. ## git分支和命名空间的对应s
  33. ## master-rd 分支,对应命名空间 halley-rd
  34. ## develop-rd 分支,对应命名空间 halley-dev-rd
  35. ifeq ($(GIT_BRANCH),$(BRANCH_MASTER))
  36. K8S_DEPLOY_PARAM_CURR_K8S_NAMESPACE=$(K8S_NAMESPACE_PREFIX)-$(RD_SUFFIX)
  37. else
  38. K8S_DEPLOY_PARAM_CURR_K8S_NAMESPACE=$(K8S_NAMESPACE_PREFIX)-dev-$(RD_SUFFIX)
  39. endif
  40. 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
  41. SHELL_EXPORT := $(foreach v,$(MAKE_ENV),$(v)='$($(v))' )
  42. # GIT_BRANCH=11
  43. all: build
  44. # unittest:
  45. # # @go test -v ./internal/...
  46. gitReset:
  47. @echo ==gitReset==
  48. @git fetch --all;
  49. @git reset --hard origin/$(GIT_BRANCH);
  50. buildquick: gitReset
  51. @echo ==buildquick==
  52. @go mod tidy;
  53. @echo GIT_BRANCH=$(GIT_BRANCH) GIT_HASH=$(GIT_HASH) K8S_DEPLOY_PARAM_IMAGE_TAG=$(K8S_DEPLOY_PARAM_IMAGE_TAG)
  54. @echo BUILD_FLAGS=$(BUILD_FLAGS)
  55. @go build $(BUILD_FLAGS) -o ./pack/service ./
  56. docker: clean buildquick configfiles
  57. @echo ==build docker image and push ==
  58. ## 非 x86_64平台,不允许做docker镜像的构建
  59. @if [ "$(X86_64)" = "" ] ; then\
  60. echo !!!! not x86_64 platform , can not build docker image !!!!!!!!; \
  61. exit 11; \
  62. fi
  63. ## 非 develop-rd、master-rd 分支,不允许做docker镜像构建
  64. @echo ACR_PREFIX=$(ACR_PREFIX);
  65. @echo CURR_MODEL_NAME=$(CURR_MODEL_NAME);
  66. @echo GIT_BRANCH=$(GIT_BRANCH);
  67. @if [[ $(GIT_BRANCH) = $(BRANCH_DEV) || $(GIT_BRANCH) = $(BRANCH_MASTER) ]]; then\
  68. docker build -t $(CURR_MODEL_NAME):$(K8S_DEPLOY_PARAM_IMAGE_TAG) -f pack/Dockerfile .;\
  69. docker tag $(CURR_MODEL_NAME):$(K8S_DEPLOY_PARAM_IMAGE_TAG) $(ACR_PREFIX)/$(CURR_MODEL_NAME):$(K8S_DEPLOY_PARAM_IMAGE_TAG); \
  70. docker push $(ACR_PREFIX)/$(CURR_MODEL_NAME):$(K8S_DEPLOY_PARAM_IMAGE_TAG); \
  71. else \
  72. echo !!!!! no docker building in branch $(GIT_BRANCH) !!!!!!; \
  73. fi
  74. deployK8s: docker
  75. @echo ==deploy to K8s==
  76. @echo $(SHELL_EXPORT);
  77. @if [[ $(GIT_BRANCH) = $(BRANCH_DEV) || $(GIT_BRANCH) = $(BRANCH_MASTER) ]]; then\
  78. $(SHELL_EXPORT) envsubst < deployK8s.yaml > $(TMP_FILE_DEPLOY) ; kubectl apply -f $(TMP_FILE_DEPLOY) ; \
  79. kubectl replace -f $(TMP_FILE_DEPLOY) --force; \
  80. else \
  81. echo !!!!! no docker building in branch $(GIT_BRANCH) !!!!!!; \
  82. fi
  83. configfiles:
  84. @echo ==get configfiles==
  85. @echo $(GIT_BRANCH)
  86. @if [[ $(GIT_BRANCH) = $(BRANCH_MASTER) ]]; then\
  87. mkdir -p $(TMP_DATA_DIR); \
  88. cd $(TMP_DATA_DIR); \
  89. [ ! -e $(CONFIGFILES_PROJECT_NAME) ] && git clone https://git.pandaminer.com/halley/$(CONFIGFILES_PROJECT_NAME); \
  90. cd $(CONFIGFILES_PROJECT_NAME); \
  91. echo currDir:; \
  92. pwd; \
  93. git fetch; \
  94. git checkout $(GIT_BRANCH); \
  95. git reset --hard origin/$(GIT_BRANCH); \
  96. cd ../..; \
  97. echo currDir:; \
  98. pwd; \
  99. cp ./$(TMP_DATA_DIR)/$(CONFIGFILES_PROJECT_NAME)/$(CURR_MODEL_NAME)/pack/.service.config.yaml ./pack/.service.config.yaml; \
  100. else \
  101. echo !!!!! no configfiles process in branch $(GIT_BRANCH) !!!!!!; \
  102. fi
  103. build: buildquick
  104. clean:
  105. rm -rf ./build ./pack/service
  106. .PHONY: build