release.mk 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. PROJECT_NAME ?= project
  2. # versioning
  3. BUMP_VERSION_CONFIG ?= .bumpversion.cfg
  4. # allow missing "patch" number
  5. BUMP_VERSION_PARSE_REGEX = (?P<major>\d+)\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?
  6. CURRENT_VERSION_FILE ?= VERSION
  7. DIR_RELEASE_ARCHIVES ?= .
  8. RELEASE_TAR_COMPRESSION_BIN = gzip
  9. ifeq ($(RELEASE_TAR_COMPRESSION_BIN),gzip)
  10. RELEASE_TAR_COMPRESSION_EXTENSION = gz
  11. else ifeq ($(RELEASE_TAR_COMPRESSION_BIN),bzip2)
  12. RELEASE_TAR_COMPRESSION_EXTENSION = bz2
  13. else ifeq ($(RELEASE_TAR_COMPRESSION_BIN),xz)
  14. RELEASE_TAR_COMPRESSION_EXTENSION = xz
  15. else
  16. $(error Invalid RELEASE_TAR_COMPRESSION specified: should be one of gzip / bzip2 / xz)
  17. endif
  18. RELEASE_TAR_FILENAME = $(DIR_RELEASE_ARCHIVES)/$(PROJECT_NAME)-$(call get_current_version).tar.$(RELEASE_TAR_COMPRESSION_EXTENSION)
  19. RELEASE_TAR_SIGN_FILENAME = $(RELEASE_TAR_FILENAME).asc
  20. RELEASE_TAR_HASH_FUNC ?= sha256sum
  21. RELEASE_TAR_HASH_FILENAME = $(RELEASE_TAR_FILENAME).$(RELEASE_TAR_HASH_FUNC)
  22. IS_VERSION_WITH_BUMPVERSION = $(wildcard $(BUMP_VERSION_CONFIG))
  23. IS_VERSION_WITH_GIT = $(shell git rev-parse --is-inside-work-tree >/dev/null 2>&1 && echo 1 || true)
  24. IS_VERSION_WITH_DEB = $(wildcard debian/changelog)
  25. IS_VERSION_WITH_NPM = $(wildcard package.json)
  26. # assemble the list of files to be committed during "make release-*"
  27. BUMP_VERSION_FILES ?= $(BUMP_VERSION_CONFIG)
  28. ifneq ($(IS_VERSION_WITH_BUMPVERSION),)
  29. BUMP_VERSION_FILES += $(shell sed -n 's/^\[bumpversion:file:\(.*\)\]$$/\1/p' "$(BUMP_VERSION_CONFIG)")
  30. else
  31. BUMP_VERSION_FILES =
  32. endif
  33. ifneq ($(IS_VERSION_WITH_DEB),)
  34. BUMP_VERSION_FILES += debian/changelog
  35. endif
  36. ifneq "$(IS_VERSION_WITH_NPM)" ""
  37. BUMP_VERSION_FILES += package.json
  38. ifneq "$(wildcard package-lock.json)" ""
  39. BUMP_VERSION_FILES += package-lock.json
  40. endif
  41. endif
  42. define IS_RELEASE_CURRENT_COMMIT_TEST =
  43. [ "$$(git show -s --format=%H "v$(call get_current_version)^{commit}" --)" = "$$(git show -s --format=%H)" ]
  44. endef
  45. 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)
  46. .PHONY: help
  47. help: help-release
  48. .PHONY: help-release
  49. help-release:
  50. @echo "Release-related packaging targets:"
  51. @echo " release-major"
  52. @echo " release-minor"
  53. @echo " release-patch"
  54. @echo " release-undo"
  55. @echo " release-push"
  56. @echo " release-tar"
  57. @echo " release-tar-sign"
  58. @echo
  59. release-major: VERSION_BUMP=major
  60. release-minor: VERSION_BUMP=minor
  61. release-patch: VERSION_BUMP=patch
  62. .PHONY: release-major release-minor release-patch
  63. release-major release-minor release-patch: release-generic
  64. .PHONY: release-generic
  65. # prevent duplicate tags for successive commits
  66. release-generic:: | release-check-current-commit-is-not-release
  67. ifneq ($(IS_VERSION_WITH_BUMPVERSION),)
  68. release-generic:: | release-bumpversion
  69. endif
  70. ifneq ($(IS_VERSION_WITH_DEB),)
  71. release-generic:: | release-debchange
  72. endif
  73. # take care for the ordering: 'release-git' needs to run before '-debchange' and '-bumpversion'
  74. ifneq ($(IS_VERSION_WITH_GIT),)
  75. release-generic:: release-git
  76. endif
  77. ifneq "$(IS_VERSION_WITH_NPM)" ""
  78. release-generic:: release-npm
  79. # ensure that "release-git" is executed after "release-npm": package.json has to be updated in time
  80. release-git: release-npm
  81. endif
  82. .PHONY: release-check-current-commit-is-not-release
  83. release-check-current-commit-is-not-release:
  84. @$(IS_RELEASE_CURRENT_COMMIT_TEST) && { echo >&2 "ERROR: current commit is a release"; false; } || true
  85. .PHONY: release-check-current-commit-is-release
  86. release-check-current-commit-is-release:
  87. @$(IS_RELEASE_CURRENT_COMMIT_TEST) || { echo >&2 "ERROR: current commit is not a release"; false; }
  88. .PHONY: release-bumpversion
  89. release-bumpversion:
  90. @if [ ! -e "$(BUMP_VERSION_CONFIG)" ]; then \
  91. echo >&2 "Configuration file '$(BUMP_VERSION_CONFIG)' missing"; \
  92. exit 1; \
  93. fi
  94. @# bump version without committing (done after updating debian/changelog)
  95. @bumpversion --config-file "$(BUMP_VERSION_CONFIG)" \
  96. --parse "$(BUMP_VERSION_PARSE_REGEX)" \
  97. --current-version "$(call get_current_version)" \
  98. "$(VERSION_BUMP)"
  99. .PHONY: release-debchange
  100. release-debchange:
  101. debchange --newversion "$(call get_current_version)-1" "New upstream release"
  102. debchange --release ""
  103. .PHONY: release-git
  104. release-git:
  105. if [ -n "$(BUMP_VERSION_FILES)" ]; then git add $(BUMP_VERSION_FILES); fi
  106. @# add other version-related files
  107. if [ -n "$(BUMP_VERSION_COMMIT_FILES)" ]; then git add $(BUMP_VERSION_COMMIT_FILES); fi
  108. git commit -m "Bump version: $$(git show "HEAD:./$(CURRENT_VERSION_FILE)") -> $(call get_current_version)"
  109. git tag -a "v$(call get_current_version)" -m "Bump version: $(VERSION_BUMP)"
  110. .PHONY: release-npm
  111. release-npm:
  112. npm --no-git-tag-version version "$(VERSION_BUMP)"
  113. .PHONY: release-undo
  114. release-undo: release-check-current-commit-is-release
  115. $(IS_RELEASE_CURRENT_COMMIT_TEST)
  116. [ -z "$$(git status --short --untracked-files=no)" ]; # refuse to run in a non-clean working directory
  117. git tag -d "v$(call get_current_version)"
  118. git reset --hard HEAD~
  119. .PHONY: release-push
  120. release-push:
  121. git push --follow-tags
  122. .PHONY: release-tar
  123. release-tar: $(RELEASE_TAR_FILENAME) $(RELEASE_TAR_HASH_FILENAME)
  124. $(RELEASE_TAR_FILENAME):
  125. $(IS_RELEASE_CURRENT_COMMIT_TEST)
  126. git archive --prefix=$(PROJECT_NAME)-$(RELEASE)/ --format=tar HEAD \
  127. | "$(RELEASE_TAR_COMPRESSION_BIN)" > "$(RELEASE_TAR_FILENAME).tmp"
  128. mv "$(RELEASE_TAR_FILENAME).tmp" "$(RELEASE_TAR_FILENAME)"
  129. $(RELEASE_TAR_HASH_FILENAME): $(RELEASE_TAR_FILENAME)
  130. $(RELEASE_TAR_HASH_FUNC) "$<" >"$@"
  131. .PHONY: release-tar-sign
  132. release-tar-sign: $(RELEASE_TAR_SIGN_FILENAME)
  133. $(RELEASE_TAR_SIGN_FILENAME): $(RELEASE_TAR_FILENAME)
  134. gpg --armor --detach-sign --sign "$<"