release.mk 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # versioning
  2. BUMP_VERSION_CONFIG ?= .bumpversion.cfg
  3. # allow missing "patch" number
  4. BUMP_VERSION_PARSE_REGEX ?= (?P<major>\d+)\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?
  5. CURRENT_VERSION_FILE ?= VERSION
  6. DIR_RELEASE_ARCHIVES ?= .
  7. RELEASE_TAR_COMPRESSION_BIN = gzip
  8. ifeq ($(RELEASE_TAR_COMPRESSION_BIN),gzip)
  9. RELEASE_TAR_COMPRESSION_EXTENSION = gz
  10. else ifeq ($(RELEASE_TAR_COMPRESSION_BIN),bzip2)
  11. RELEASE_TAR_COMPRESSION_EXTENSION = bz2
  12. else ifeq ($(RELEASE_TAR_COMPRESSION_BIN),xz)
  13. RELEASE_TAR_COMPRESSION_EXTENSION = xz
  14. else ifeq ($(RELEASE_TAR_COMPRESSION_BIN),zstd)
  15. RELEASE_TAR_COMPRESSION_EXTENSION = zst
  16. else
  17. $(error Invalid RELEASE_TAR_COMPRESSION specified: should be one of gzip / bzip2 / xz)
  18. endif
  19. RELEASE_TAR_FILENAME = $(DIR_RELEASE_ARCHIVES)/$(PROJECT_NAME)-$(call get_current_version).tar.$(RELEASE_TAR_COMPRESSION_EXTENSION)
  20. RELEASE_TAR_SIGN_FILENAME = $(RELEASE_TAR_FILENAME).asc
  21. RELEASE_TAR_HASH_FUNC ?= sha256sum
  22. RELEASE_TAR_HASH_FILENAME = $(RELEASE_TAR_FILENAME).$(RELEASE_TAR_HASH_FUNC)
  23. # We need to set an explicit `--distribution` argument for `debchange` in order to work around a
  24. # weird Ubuntu-specific hack, which causes non-predictable distribution names.
  25. # Users may override this by setting `DEB_RELEASE_DISTRIBUTION` to an empty string.
  26. # See https://notabug.org/sumpfralle/makefilet/issues/38
  27. DEB_RELEASE_DISTRIBUTION ?= unstable
  28. # override the variable for selecting a specific gpg key
  29. SIGNATURE_EMAIL ?=
  30. IS_VERSION_WITH_BUMPVERSION = $(wildcard $(BUMP_VERSION_CONFIG))
  31. IS_VERSION_WITH_GIT = $(shell git rev-parse --is-inside-work-tree >/dev/null 2>&1 && echo 1 || true)
  32. IS_VERSION_WITH_DEB = $(wildcard debian/changelog)
  33. # We only want to bump version numbers in a package.json (and package-lock.json) file if
  34. # * a package.json exists
  35. # * it actually contains a version number and is not just used for dependency tracking (`npm pkg get version` returns '{}' if no version is tracked)
  36. IS_VERSION_WITH_NPM = $(shell if [ -e package.json ] && [ "$$(npm pkg get version 2>/dev/null || echo "{}")" != "{}" ]; then echo 1; fi)
  37. # assemble the list of files to be committed during "make release-*"
  38. BUMP_VERSION_FILES ?= $(BUMP_VERSION_CONFIG)
  39. ifneq ($(IS_VERSION_WITH_BUMPVERSION),)
  40. BUMP_VERSION_FILES += $(shell sed -n 's/^\[bumpversion:file:\(.*\)\]$$/\1/p' "$(BUMP_VERSION_CONFIG)")
  41. else
  42. BUMP_VERSION_FILES =
  43. endif
  44. ifneq ($(IS_VERSION_WITH_DEB),)
  45. BUMP_VERSION_FILES += debian/changelog
  46. endif
  47. ifneq ($(IS_VERSION_WITH_NPM),)
  48. BUMP_VERSION_FILES += package.json
  49. ifneq "$(wildcard package-lock.json)" ""
  50. BUMP_VERSION_FILES += package-lock.json
  51. endif
  52. endif
  53. define IS_RELEASE_CURRENT_COMMIT_TEST =
  54. [ "$$(git show -s --format=%H "v$(call get_current_version)^{commit}" --)" = "$$(git show -s --format=%H)" ]
  55. endef
  56. get_current_version = $(shell if ! cut -f 1-3 -d . "$(CURRENT_VERSION_FILE)" 2>/dev/null; then echo >&2 "Missing '$(CURRENT_VERSION_FILE)' file (see variable 'CURRENT_VERSION_FILE)'"; echo "unknown"; fi)
  57. .PHONY: help
  58. help: help-release
  59. .PHONY: help-release
  60. help-release:
  61. @echo "Release-related packaging targets:"
  62. @echo " release-major"
  63. @echo " release-minor"
  64. @echo " release-patch"
  65. @echo " release-undo"
  66. @echo " release-push"
  67. @echo " release-tar"
  68. @echo " release-tar-sign"
  69. @echo
  70. release-major: VERSION_BUMP=major
  71. release-minor: VERSION_BUMP=minor
  72. release-patch: VERSION_BUMP=patch
  73. .PHONY: release-major release-minor release-patch
  74. release-major release-minor release-patch: release-generic
  75. .PHONY: release-generic
  76. # prevent duplicate tags for successive commits
  77. release-generic:: | release-check-current-commit-is-not-release
  78. ifneq ($(IS_VERSION_WITH_BUMPVERSION),)
  79. release-generic:: | release-bumpversion
  80. endif
  81. ifneq ($(IS_VERSION_WITH_DEB),)
  82. release-generic:: | release-debchange
  83. endif
  84. # take care for the ordering: 'release-git' needs to run before '-debchange' and '-bumpversion'
  85. ifneq ($(IS_VERSION_WITH_GIT),)
  86. release-generic:: release-git
  87. endif
  88. ifneq ($(IS_VERSION_WITH_NPM),)
  89. release-generic:: release-npm
  90. # ensure that "release-git" is executed after "release-npm": package.json has to be updated in time
  91. release-git: release-npm
  92. endif
  93. .PHONY: release-check-current-commit-is-not-release
  94. release-check-current-commit-is-not-release:
  95. @$(IS_RELEASE_CURRENT_COMMIT_TEST) && { echo >&2 "ERROR: current commit is a release"; false; } || true
  96. .PHONY: release-check-current-commit-is-release
  97. release-check-current-commit-is-release:
  98. @$(IS_RELEASE_CURRENT_COMMIT_TEST) || { echo >&2 "ERROR: current commit is not a release"; false; }
  99. .PHONY: release-bumpversion
  100. release-bumpversion:
  101. @if [ ! -e "$(BUMP_VERSION_CONFIG)" ]; then \
  102. echo >&2 "Configuration file '$(BUMP_VERSION_CONFIG)' missing"; \
  103. exit 1; \
  104. fi
  105. @# bump version without committing (done after updating debian/changelog)
  106. @bumpversion --config-file "$(BUMP_VERSION_CONFIG)" \
  107. --parse "$(BUMP_VERSION_PARSE_REGEX)" \
  108. --current-version "$(call get_current_version)" \
  109. "$(VERSION_BUMP)"
  110. .PHONY: release-debchange
  111. release-debchange:
  112. debchange --newversion "$(call get_current_version)-1" "New upstream release"
  113. @# Append a named distribution, if one is specified.
  114. @# Read from /dev/null in order to prevent `debchange` from asking questions regarding the
  115. @# (potentially unsupported) distribution name.
  116. debchange --release ""$(patsubst %, --distribution %, $(DEB_RELEASE_DISTRIBUTION)) </dev/null
  117. .PHONY: release-git
  118. release-git:
  119. if [ -n "$(BUMP_VERSION_FILES)" ]; then git add $(BUMP_VERSION_FILES); fi
  120. @# add other version-related files
  121. if [ -n "$(BUMP_VERSION_COMMIT_FILES)" ]; then git add $(BUMP_VERSION_COMMIT_FILES); fi
  122. git commit -m "Bump version: $$(git show "HEAD:./$(CURRENT_VERSION_FILE)") -> $(call get_current_version)"
  123. git tag -a "v$(call get_current_version)" -m "Bump version: $(VERSION_BUMP)"
  124. .PHONY: release-npm
  125. release-npm:
  126. npm --no-git-tag-version version "$(call get_current_version)"
  127. .PHONY: release-undo
  128. release-undo: release-check-current-commit-is-release
  129. [ -z "$$(git status --short --untracked-files=no)" ]; # refuse to run in a non-clean working directory
  130. git tag -d "v$(call get_current_version)"
  131. git reset --hard HEAD~
  132. .PHONY: release-push
  133. release-push:
  134. git push --follow-tags
  135. .PHONY: release-tar
  136. release-tar: $(RELEASE_TAR_FILENAME) $(RELEASE_TAR_HASH_FILENAME)
  137. $(RELEASE_TAR_FILENAME): release-check-current-commit-is-release
  138. git archive --prefix=$(PROJECT_NAME)-$(RELEASE)/ --format=tar HEAD \
  139. | "$(RELEASE_TAR_COMPRESSION_BIN)" > "$(RELEASE_TAR_FILENAME).tmp"
  140. mv "$(RELEASE_TAR_FILENAME).tmp" "$(RELEASE_TAR_FILENAME)"
  141. $(RELEASE_TAR_HASH_FILENAME): $(RELEASE_TAR_FILENAME)
  142. $(RELEASE_TAR_HASH_FUNC) "$<" >"$@"
  143. .PHONY: release-tar-sign
  144. release-tar-sign: $(RELEASE_TAR_SIGN_FILENAME)
  145. $(RELEASE_TAR_SIGN_FILENAME): $(RELEASE_TAR_FILENAME)
  146. gpg --armor --detach-sign --sign $(patsubst %,--local-user %,$(SIGNATURE_EMAIL)) "$<"