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.

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