Makefile 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. # Copyright 2016 The Rook Authors. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. all: build
  15. include ../makelib/common.mk
  16. include ../makelib/helm.mk
  17. # ====================================================================================
  18. # Options
  19. CHANNEL ?= master
  20. ifeq ($(filter master release,$(CHANNEL)),)
  21. $(error invalid channel $(CHANNEL))
  22. endif
  23. # When running the tag pipeline or release build we always want to add the alpha, beta, or rc suffix
  24. # if provided. Otherwise, it's a master build where we don't want to apply the suffix.
  25. ifneq ($(TAG_WITH_SUFFIX),true)
  26. override VERSION := $(shell echo "$(VERSION)" | sed -e 's/-alpha.0//' -e 's/-beta.0//' -e 's/-rc.0//')
  27. endif
  28. DOCS_PREFIX ?= docs/rook
  29. ifeq ($(shell echo $(BRANCH_NAME)),master)
  30. DOCS_VERSION := latest
  31. else
  32. DOCS_VERSION := $(shell echo $(BRANCH_NAME) | sed -E "s/^release\-([0-9]+)\.([0-9]+)$$/v\1.\2/g")
  33. endif
  34. ifeq ($(shell echo $(BRANCH_NAME)),$(shell $(ROOT_DIR)/build/release/latest_release_branch.sh))
  35. DOCS_VERSION_ALIAS := latest-release
  36. else
  37. DOCS_VERSION_ALIAS :=
  38. endif
  39. DOCS_GIT_REPO ?= git@github.com:rook/rook.github.io.git
  40. ifeq ($(origin BRANCH_NAME), undefined)
  41. BRANCH_NAME := $(shell git branch | grep \* | cut -d ' ' -f2)
  42. endif
  43. ifeq ($(COMMIT_HASH),)
  44. override COMMIT_HASH := $(shell git rev-parse HEAD)
  45. endif
  46. REMOTE_NAME ?= origin
  47. PLATFORMS ?= $(ALL_PLATFORMS)
  48. ifneq ($(filter master release-%,$(BRANCH_NAME)),)
  49. FLAVORS ?= output images docs helm
  50. else
  51. FLAVORS ?= output
  52. override BRANCH_NAME := pr/$(BRANCH_NAME)
  53. endif
  54. DOCKER_REGISTRY ?= rook
  55. REGISTRIES ?= $(DOCKER_REGISTRY)
  56. IMAGE_ARCHS := $(subst linux_,,$(filter linux_%,$(PLATFORMS)))
  57. IMAGE_PLATFORMS := $(subst _,/,$(subst $(SPACE),$(COMMA),$(filter linux_%,$(PLATFORMS))))
  58. S3_BUCKET ?= rook.releases
  59. S3_CP := aws s3 cp --only-show-errors
  60. S3_SYNC := aws s3 sync --only-show-errors
  61. S3_SYNC_DEL := aws s3 sync --only-show-errors --delete
  62. # ====================================================================================
  63. # tools
  64. MANIFEST_TOOL_VERSION=v1.0.2
  65. MANIFEST_TOOL := $(TOOLS_HOST_DIR)/manifest-tool-$(MANIFEST_TOOL_VERSION)
  66. $(MANIFEST_TOOL):
  67. @echo === installing manifest-tool
  68. mkdir -p $(TOOLS_HOST_DIR)
  69. curl -sL https://github.com/estesp/manifest-tool/releases/download/$(MANIFEST_TOOL_VERSION)/manifest-tool-$(shell go env GOHOSTOS)-$(GOHOSTARCH) > $@
  70. chmod +x $@
  71. # ====================================================================================
  72. # Targets
  73. build: $(addprefix build.,$(FLAVORS)) ;
  74. publish: $(addprefix publish.,$(FLAVORS)) ;
  75. promote: $(addprefix promote.,$(FLAVORS)) ;
  76. clean: $(addprefix clean.,$(FLAVORS)) ;
  77. # catch all for unimplemented targets / flavors
  78. %: ; @:
  79. # ====================================================================================
  80. # tagging a release
  81. VERSION_REGEX := ^v\([0-9]*\)[.]\([0-9]*\)[.]\([0-9]*\)[-]*\([alpha|beta|rc]\)*[\.]*\([0-9]*\)$$
  82. VERSION_VALID := $(shell echo "$(VERSION)" | grep -q '$(VERSION_REGEX)' && echo 1 || echo 0)
  83. VERSION_MAJOR := $(shell echo "$(VERSION)" | sed -e 's/$(VERSION_REGEX)/\1/')
  84. VERSION_MINOR := $(shell echo "$(VERSION)" | sed -e 's/$(VERSION_REGEX)/\2/')
  85. VERSION_PATCH := $(shell echo "$(VERSION)" | sed -e 's/$(VERSION_REGEX)/\3/')
  86. tag:
  87. ifneq ($(VERSION_VALID),1)
  88. $(error invalid version $(VERSION), must be a semantic version with v[Major].[Minor].[Patch] and an optional suffix such as -alpha.0, -beta.1, or -rc.2)
  89. endif
  90. @echo === tagging commit hash $(COMMIT_HASH) with $(VERSION) and parsed v$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH)
  91. git tag -f -m "release $(VERSION)" $(VERSION) $(COMMIT_HASH)
  92. git push $(REMOTE_NAME) $(VERSION)
  93. set -e; if ! git ls-remote --heads $(REMOTE_NAME) | grep -q refs/heads/release-$(VERSION_MAJOR).$(VERSION_MINOR); then \
  94. echo === creating new release branch release-$(VERSION_MAJOR).$(VERSION_MINOR) ;\
  95. git branch -f release-$(VERSION_MAJOR).$(VERSION_MINOR) $(COMMIT_HASH) ;\
  96. git push $(REMOTE_NAME) release-$(VERSION_MAJOR).$(VERSION_MINOR) ;\
  97. fi
  98. # ====================================================================================
  99. # docs
  100. deps.docs:
  101. pip3 install -r requirements_docs.txt
  102. build.docs: deps.docs
  103. # Build the docs once before they are published to ensure they are correctly building
  104. cd $(ROOT_DIR) && mkdocs build --strict
  105. publish.docs:
  106. if ! git remote show docs > /dev/null 2>&1; then git remote add docs $(DOCS_GIT_REPO); fi
  107. # If we are running in GitHub actions, remove the extraheader so our custom GitHub API token is used
  108. if test "$$GITHUB_ACTIONS" = "true"; then git config --unset 'http.https://github.com/.extraheader'; fi
  109. git fetch -u docs gh-pages
  110. # Switch to root of repo and then run the build and deploy of the documentation
  111. cd $(ROOT_DIR) && mike deploy --remote docs --push --branch gh-pages --update-aliases --deploy-prefix $(DOCS_PREFIX) $(DOCS_VERSION) $(DOCS_VERSION_ALIAS)
  112. # ====================================================================================
  113. # helm
  114. HELM_TEMP := $(shell mktemp -d)
  115. HELM_URL := $(HELM_BASE_URL)/$(CHANNEL)
  116. promote.helm: $(HELM)
  117. # copy existing charts to a temp dir, then combine with new charts, reindex, and upload
  118. $(S3_SYNC) s3://$(HELM_S3_BUCKET)/$(CHANNEL) $(HELM_TEMP)
  119. $(S3_SYNC) s3://$(S3_BUCKET)/build/$(BRANCH_NAME)/$(VERSION)/charts $(HELM_TEMP)
  120. $(HELM) repo index --url $(HELM_URL) $(HELM_TEMP)
  121. $(S3_SYNC_DEL) $(HELM_TEMP) s3://$(HELM_S3_BUCKET)/$(CHANNEL)
  122. rm -fr $(HELM_TEMP)
  123. # ====================================================================================
  124. # output
  125. publish.output:
  126. $(S3_SYNC_DEL) $(OUTPUT_DIR) s3://$(S3_BUCKET)/build/$(BRANCH_NAME)/$(VERSION)
  127. promote.output:
  128. $(S3_SYNC_DEL) s3://$(S3_BUCKET)/build/$(BRANCH_NAME)/$(VERSION) s3://$(S3_BUCKET)/$(CHANNEL)/$(VERSION)
  129. $(S3_SYNC_DEL) s3://$(S3_BUCKET)/build/$(BRANCH_NAME)/$(VERSION) s3://$(S3_BUCKET)/$(CHANNEL)/current
  130. # ====================================================================================
  131. # images
  132. # 1: registry 2: image, 3: arch
  133. define repo.targets
  134. build.image.$(1).$(2).$(3):
  135. $(DOCKERCMD) tag $(BUILD_REGISTRY)/$(2)-$(3) $(1)/$(2)-$(3):$(VERSION)
  136. @# Save image as _output/images/linux_<arch>/<image>.tar.gz (no builds for darwin or windows)
  137. mkdir -p $(OUTPUT_DIR)/images/linux_$(3)
  138. $(DOCKERCMD) save $(BUILD_REGISTRY)/$(2)-$(3) | gzip -c > $(OUTPUT_DIR)/images/linux_$(3)/$(2).tar.gz
  139. build.all.images: build.image.$(1).$(2).$(3)
  140. publish.image.$(1).$(2).$(3): ; @$(DOCKERCMD) push $(1)/$(2)-$(3):$(VERSION)
  141. publish.all.images: publish.image.$(1).$(2).$(3)
  142. # tag the master image, but do not tag the release image with a generic channel tag
  143. promote.image.$(1).$(2).$(3):
  144. $(DOCKERCMD) pull $(1)/$(2)-$(3):$(VERSION)
  145. $(DOCKERCMD) tag $(1)/$(2)-$(3):$(VERSION) $(1)/$(2)-$(3):$(CHANNEL)
  146. [ "$(CHANNEL)" = "release" ] || $(DOCKERCMD) push $(1)/$(2)-$(3):$(CHANNEL)
  147. promote.all.images: promote.image.$(1).$(2).$(3)
  148. clean.image.$(1).$(2).$(3):
  149. [ -z "$$$$($(DOCKERCMD) images -q $(1)/$(2)-$(3):$(VERSION))" ] || $(DOCKERCMD) rmi $(1)/$(2)-$(3):$(VERSION)
  150. [ -z "$$$$($(DOCKERCMD) images -q $(1)/$(2)-$(3):$(CHANNEL))" ] || $(DOCKERCMD) rmi $(1)/$(2)-$(3):$(CHANNEL)
  151. clean.all.images: clean.image.$(1).$(2).$(3)
  152. endef
  153. $(foreach r,$(REGISTRIES), $(foreach i,$(IMAGES), $(foreach a,$(IMAGE_ARCHS),$(eval $(call repo.targets,$(r),$(i),$(a))))))
  154. publish.manifest.image.%: publish.all.images $(MANIFEST_TOOL)
  155. $(MANIFEST_TOOL) push from-args --platforms $(IMAGE_PLATFORMS) --template $(DOCKER_REGISTRY)/$*-ARCH:$(VERSION) --target $(DOCKER_REGISTRY)/$*:$(VERSION)
  156. # add the "master" tag to the master image, but do not add the "release" tag for the release channel
  157. promote.manifest.image.%: promote.all.images $(MANIFEST_TOOL)
  158. [ "$(CHANNEL)" = "release" ] || $(MANIFEST_TOOL) push from-args --platforms $(IMAGE_PLATFORMS) --template $(DOCKER_REGISTRY)/$*-ARCH:$(VERSION) --target $(DOCKER_REGISTRY)/$*:$(CHANNEL)
  159. build.images: build.all.images
  160. publish.images: $(addprefix publish.manifest.image.,$(IMAGES))
  161. promote.images: $(addprefix promote.manifest.image.,$(IMAGES))
  162. clean.images: clean.all.images
  163. # ====================================================================================
  164. # Help
  165. .PHONY: help
  166. help:
  167. @echo 'Usage: make <OPTIONS> ... <TARGETS>'
  168. @echo ''
  169. @echo 'Targets:'
  170. @echo ' build Build all release artifacts.'
  171. @echo ' clean Remove all release artifacts.'
  172. @echo ' publish Publish all release artifacts.'
  173. @echo ' promote Promote a build to a channel.'
  174. @echo ' tag Tag a build for release.'
  175. @echo ''
  176. @echo 'Options:'
  177. @echo ' VERSION Sets the release version.'
  178. @echo ' BRANCH_NAME Name of the branch we're releasing from.'
  179. @echo ' CHANNEL Sets the release channel. Can be set to master,'
  180. @echo ' or release. Default is not set.'
  181. @echo ' PLATFORMS The supported platforms to build when running.'
  182. @echo ' the build.all target. The default is'
  183. @echo ' all supported platforms'