555
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.

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