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.

109 lines
3.2 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. #GIT_BRANCH=master-rd
  30. ## git分支和命名空间的对应s
  31. ## master-rd 分支,对应命名空间 halley-rd
  32. ## develop-rd 分支,对应命名空间 halley-dev-rd
  33. ifeq ($(GIT_BRANCH),$(BRANCH_MASTER))
  34. K8S_DEPLOY_PARAM_CURR_K8S_NAMESPACE=$(K8S_NAMESPACE_PREFIX)-$(RD_SUFFIX)
  35. else
  36. K8S_DEPLOY_PARAM_CURR_K8S_NAMESPACE=$(K8S_NAMESPACE_PREFIX)-dev-$(RD_SUFFIX)
  37. endif
  38. 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
  39. SHELL_EXPORT := $(foreach v,$(MAKE_ENV),$(v)='$($(v))' )
  40. # GIT_BRANCH=11
  41. all: build
  42. # unittest:
  43. # # @go test -v ./internal/...
  44. gitReset:
  45. @echo ==gitReset==
  46. @git fetch --all;
  47. @git reset --hard origin/$(GIT_BRANCH);
  48. buildquick: gitReset
  49. @echo ==buildquick==
  50. @go mod tidy;
  51. @echo GIT_BRANCH=$(GIT_BRANCH) GIT_HASH=$(GIT_HASH) K8S_DEPLOY_PARAM_IMAGE_TAG=$(K8S_DEPLOY_PARAM_IMAGE_TAG)
  52. @echo BUILD_FLAGS=$(BUILD_FLAGS)
  53. @go build $(BUILD_FLAGS) -o ./pack/service ./
  54. docker: clean buildquick
  55. @echo ==build docker image and push ==
  56. ## 非 x86_64平台,不允许做docker镜像的构建
  57. @if [ "$(X86_64)" = "" ] ; then\
  58. echo !!!! not x86_64 platform , can not build docker image !!!!!!!!; \
  59. exit 11; \
  60. fi
  61. ## 非 develop-rd、master-rd 分支,不允许做docker镜像构建
  62. @echo ACR_PREFIX=$(ACR_PREFIX);
  63. @echo CURR_MODEL_NAME=$(CURR_MODEL_NAME);
  64. @echo GIT_BRANCH=$(GIT_BRANCH);
  65. @if [[ $(GIT_BRANCH) = $(BRANCH_DEV) || $(GIT_BRANCH) = $(BRANCH_MASTER) ]]; then\
  66. docker build -t $(CURR_MODEL_NAME):$(K8S_DEPLOY_PARAM_IMAGE_TAG) -f pack/Dockerfile .;\
  67. docker tag $(CURR_MODEL_NAME):$(K8S_DEPLOY_PARAM_IMAGE_TAG) $(ACR_PREFIX)/$(CURR_MODEL_NAME):$(K8S_DEPLOY_PARAM_IMAGE_TAG); \
  68. docker push $(ACR_PREFIX)/$(CURR_MODEL_NAME):$(K8S_DEPLOY_PARAM_IMAGE_TAG); \
  69. else \
  70. echo !!!!! no docker building in branch $(GIT_BRANCH) !!!!!!; \
  71. fi
  72. deployK8s: docker
  73. @echo ==deploy to K8s==
  74. @echo $(SHELL_EXPORT);
  75. @if [[ $(GIT_BRANCH) = $(BRANCH_DEV) || $(GIT_BRANCH) = $(BRANCH_MASTER) ]]; then\
  76. $(SHELL_EXPORT) envsubst < deployK8s.yaml > $(TMP_FILE_DEPLOY) ; kubectl apply -f $(TMP_FILE_DEPLOY) ; \
  77. kubectl replace -f $(TMP_FILE_DEPLOY) --force; \
  78. else \
  79. echo !!!!! no docker building in branch $(GIT_BRANCH) !!!!!!; \
  80. fi
  81. build: buildquick
  82. clean:
  83. rm -rf ./build ./pack/service
  84. .PHONY: build