github.com/mier85/go-sensor@v1.30.1-0.20220920111756-9bf41b3bc7e0/Makefile.release (about) 1 GO_MODULE_NAME ?= github.com/mier85/go-sensor 2 3 VERSION_TAG_PREFIX ?= v 4 VERSION_GO_FILE ?= $(shell grep -l '^const Version = ".\+"' *.go | head -1) 5 6 GIT_REMOTE ?= origin 7 GIT_MAIN_BRANCH ?= $(shell git remote show $(GIT_REMOTE) | sed -n '/HEAD branch/s/.*: //p') 8 GIT_TREE_STATE = $(shell (git status --porcelain | grep -q .) && echo dirty || echo clean) 9 10 GITHUB_CLI = $(shell which gh 2>/dev/null) 11 12 # Search for the latest vX.Y.Z tag ignoring all others, and use the X.Y.Z part as a version 13 GIT_VERSION = $(shell git tag | grep "^$(VERSION_TAG_PREFIX)[0-9]\+\(\.[0-9]\+\)\{2\}" | sort -V | tail -n1 | sed "s~^$(VERSION_TAG_PREFIX)~~" ) 14 ifeq ($(GIT_VERSION),) 15 GIT_VERSION = 0.0.0 16 endif 17 18 # Parse semantic version X.Y.Z found in git tags 19 GIT_MAJOR_VERSION = $(word 1,$(subst ., ,$(GIT_VERSION))) 20 GIT_MINOR_VERSION = $(word 2,$(subst ., ,$(GIT_VERSION))) 21 GIT_PATCH_VERSION = $(word 3,$(subst ., ,$(GIT_VERSION))) 22 23 # Parse version from version.go file 24 VERSION = $(shell grep -hm1 '^const Version = ".\+"' ${VERSION_GO_FILE} | head -1 | sed -E 's/const Version = "([0-9\.]+)"/\1/') 25 26 # Create changelog if GitHub CLI is installed 27 ifneq ($(GITHUB_CLI),) 28 release : CHANGELOG.txt 29 endif 30 31 # Create new release tag and publish a release from it 32 release : 33 git tag $(VERSION_TAG_PREFIX)$(VERSION) 34 git push --tags 35 ifneq ($(GITHUB_CLI),) 36 $(GITHUB_CLI) release create $(VERSION_TAG_PREFIX)$(VERSION) \ 37 --draft \ 38 --title $(VERSION_TAG_PREFIX)$(VERSION) \ 39 --notes-file CHANGELOG.txt 40 rm CHANGELOG.txt 41 else 42 @echo "GitHub CLI is not installed. Please proceed with creating the release on https://github.com" 43 endif 44 45 # Calculate the next major version from the latest one found in git tags, update version.go, commit and push changes to remote 46 major : ensure-git-clean update-local-copy 47 $(eval VERSION = $(shell echo $$(($(GIT_MAJOR_VERSION)+1))).0.0) 48 @printf "You are about to increase the major version to $(VERSION), are you sure? [y/N]: " && read ans && [ $${ans:-N} = y ] 49 sed -i '' 's/^const Version = ".*"/const Version = "$(VERSION)"/' ${VERSION_GO_FILE} 50 git commit -m "Bump version to v$(VERSION)" ${VERSION_GO_FILE} 51 git push $(GIT_REMOTE) HEAD 52 53 # Calculate the next minor version from the latest one found in git tags, update version.go, commit and push changes to remote 54 minor : ensure-git-clean update-local-copy 55 $(eval VERSION = $(GIT_MAJOR_VERSION).$(shell echo $$(($(GIT_MINOR_VERSION)+1))).0) 56 sed -i '' 's/^const Version = ".*"/const Version = "$(VERSION)"/' ${VERSION_GO_FILE} 57 git commit -m "Bump version to v$(VERSION)" ${VERSION_GO_FILE} 58 git push $(GIT_REMOTE) HEAD 59 60 # Calculate the next patch version from the latest one found in git tags, update version.go, commit and push changes to remote 61 patch : ensure-git-clean update-local-copy 62 $(eval VERSION = $(GIT_MAJOR_VERSION).$(GIT_MINOR_VERSION).$(shell echo $$(($(GIT_PATCH_VERSION)+1)))) 63 sed -i '' 's/^const Version = ".*"/const Version = "$(VERSION)"/' ${VERSION_GO_FILE} 64 git commit -m "Bump version to v$(VERSION)" ${VERSION_GO_FILE} 65 git push $(GIT_REMOTE) HEAD 66 67 .INTERMEDIATE : CHANGELOG.txt 68 CHANGELOG.txt : 69 echo "This release of `$(GO_MODULE_NAME)` includes the following fixes & improvements:" > CHANGELOG.txt 70 git log --merges $(VERSION_TAG_PREFIX)$(GIT_VERSION).. --pretty='format:%s' | \ 71 sed 's~Merge pull request #\([0-9]*\).*~\1~' | \ 72 xargs -n1 $(GITHUB_CLI) pr view | \ 73 grep '^title:' | \ 74 sed 's~^title:~*~' >> CHANGELOG.txt 75 echo >> CHANGELOG.txt 76 77 # Make sure there are no uncommitted changes in the working tree 78 ensure-git-clean : 79 ifeq ($(GIT_TREE_STATE),dirty) 80 $(error "There are uncommitted changes in current working tree. Please stash or commit them before release.") 81 endif 82 83 # Switch to the main branch, then pull and rebase to the latest changes including tags 84 update-local-copy : 85 git fetch --tags $(GIT_REMOTE) 86 git checkout $(GIT_MAIN_BRANCH) 87 git rebase $(GIT_REMOTE)/$(GIT_MAIN_BRANCH) 88 89 .PHONY : major minor patch release ensure-git-clean update-local-copy