From c98eae0523af742085d409d6ba899a087d34790b Mon Sep 17 00:00:00 2001 From: Ivan Ilves Date: Tue, 11 Oct 2022 22:02:48 +0200 Subject: [PATCH] chore: add application version support --- Makefile | 43 ++++++++++++++++++++++++++++---- README.md | 9 +++++++ cmd/ttg/main.go | 8 ++++++ pkg/shell/shell.go | 7 ++++++ scripts/github-create-release.sh | 30 ++++++++++++++++++++++ scripts/github-upload-assets.sh | 34 +++++++++++++++++++++++++ 6 files changed, 126 insertions(+), 5 deletions(-) create mode 100755 scripts/github-create-release.sh create mode 100755 scripts/github-upload-assets.sh diff --git a/Makefile b/Makefile index e2b74aa..e378e5a 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,12 @@ -APP_NAME := ttg +APP_NAME := ttg +GITHUB_REPO := ivanilves/ttg +API_VERSION := 0.1 + +APP_VERSION := $(shell (git fetch --tags && git tag --sort=creatordate | grep -F "v${API_VERSION}." || echo UNDEFINED) | tail -n1) +CURRENT_PATCH := $(shell (git fetch --tags && git tag --sort=creatordate | grep -F "v${API_VERSION}." || echo -1) | tail -n1 | sed -r "s/^v([0-9]+\.){2}//") +NEXT_PATCH := $(shell expr ${CURRENT_PATCH} + 1) +NEXT_VERSION := v${API_VERSION}.${NEXT_PATCH} + BUILD_PATH := ./cmd/${APP_NAME} RELEASE_PATH := ./release @@ -19,11 +27,19 @@ install: PREFIX := /usr/local install: install ${BUILD_PATH}/ttg ${PREFIX}/bin/ -release-binary: GOOS ?= $(shell uname -s | tr '[A-Z]' '[a-z]') -release-binary: GOARCH ?= $(shell uname -m | sed 's/x86_64/amd64/') +changelog: LAST_RELEASED_TAG:=$(shell git tag --sort=creatordate | tail -n1) +changelog: GITHUB_COMMIT_URL:=https://github.com/${GITHUB_REPO}/commit +changelog: + @echo "## Changelog" \ + && git log --oneline --reverse ${LAST_RELEASED_TAG}..HEAD | egrep -iv "^[0-9a-f]{7,} (Merge pull request |Merge branch |.*NORELEASE)" | \ + sed -r "s|^([0-9a-f]{7,}) (.*)|* [\`\1\`](${GITHUB_COMMIT_URL}/\1) \2|" + +release-binary: GOOS ?= $(shell uname -s | tr '[A-Z]' '[a-z]') +release-binary: GOARCH ?= $(shell uname -m | sed 's/x86_64/amd64/') +release-binary: LDFLAGS := "-X 'main.appVersion=${NEXT_VERSION}'" release-binary: mkdir -p ${RELEASE_PATH}/${APP_NAME}-${GOOS}-${GOARCH} && cd ${BUILD_PATH} && \ - GOOS=${GOOS} GOARCH=${GOARCH} go build -mod=vendor -o ../../${RELEASE_PATH}/${APP_NAME}-${GOOS}-${GOARCH}/${APP_NAME} + GOOS=${GOOS} GOARCH=${GOARCH} go build -mod=vendor -ldflags ${LDFLAGS} -o ../../${RELEASE_PATH}/${APP_NAME}-${GOOS}-${GOARCH}/${APP_NAME} release-binaries: ${MAKE} --no-print-directory release-binary GOOS=linux GOARCH=amd64 @@ -33,4 +49,21 @@ release-binaries: release-artifacts: cd ${RELEASE_PATH} && find . -mindepth 1 -maxdepth 1 -type d | xargs -i tar -C {} -zc ${APP_NAME} -f {}.tar.gz -release: release-binaries release-artifacts +release-metadata: + echo ${NEXT_VERSION} >${RELEASE_PATH}/TAG + echo ${NEXT_VERSION} >${RELEASE_PATH}/NAME + ${MAKE} --no-print-directory changelog >${RELEASE_PATH}/CHANGELOG.md + cp -f README.md ${RELEASE_PATH}/ + +release: release-binaries release-artifacts release-metadata + +next-version-tag: + git tag ${NEXT_VERSION} + +github-release: + scripts/github-create-release.sh ${GITHUB_REPO} ./release + +github-assets: + scripts/github-upload-assets.sh ${GITHUB_REPO} ./release + +github: github-release github-assets diff --git a/README.md b/README.md index e9f566b..26dee23 100644 --- a/README.md +++ b/README.md @@ -26,3 +26,12 @@ experience, i.e. you will need to exit subshell before you "jump" to the next pr * `make dep` - install dependencies; * `make build` - build the `ttg` binary in `cmd/ttg` path; * `make install` - [optional] install built `ttg` binary under the `${PREFIX}/bin` location; + +## How to release a new version? + +:bulb: Set `GITHUB_TOKEN` environmental variable. + +* `make release` - create release artifacts; +* `make next-version-tag` - tag your HEAD with the incremented version; +* `git push --tags` - push your new tag to Git; +* `make github` - create a GitHub release using your artifacts; diff --git a/cmd/ttg/main.go b/cmd/ttg/main.go index 5766456..9c74e1a 100644 --- a/cmd/ttg/main.go +++ b/cmd/ttg/main.go @@ -12,9 +12,13 @@ import ( "github.com/ivanilves/ttg/pkg/shell" ) +var appVersion = "default" + +var version bool var outFile string func init() { + flag.BoolVar(&version, "version", false, "print application version and exit") flag.StringVar(&outFile, "outFile", "", "output project path into the file specified instead of spawning a shell") } @@ -33,6 +37,10 @@ func main() { flag.Usage = usage flag.Parse() + if version { + shell.PrintAndExit(appVersion) + } + matches := flag.Args() rootPath, err := scm.RootPath() diff --git a/pkg/shell/shell.go b/pkg/shell/shell.go index c29832a..d486ec8 100644 --- a/pkg/shell/shell.go +++ b/pkg/shell/shell.go @@ -51,3 +51,10 @@ func Spawn(path string) error { return cmd.Run() } + +// PrintAndExit prints a string passed and exits after +func PrintAndExit(s string) { + fmt.Printf("%s\n", s) + + os.Exit(0) +} diff --git a/scripts/github-create-release.sh b/scripts/github-create-release.sh new file mode 100755 index 0000000..9f3d6e7 --- /dev/null +++ b/scripts/github-create-release.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash +# +if [[ ${#} -ne 2 ]]; then + echo "Create release on GitHub" + echo "Usage: ${0} GITHUB_REPO RELEASE_PATH" >>/dev/stderr + exit 1 +fi + +if [[ -z "${GITHUB_TOKEN}" ]]; then + echo "GITHUB_TOKEN not set" >>/dev/stderr + exit 1 +fi + +set -euo pipefail + +declare -r GITHUB_REPO="${1}" +declare -r RELEASE_PATH="${2}" + +declare -r API_URL=https://api.github.com/repos/${GITHUB_REPO}/releases + +pushd "${RELEASE_PATH}" + declare -r TAG="$(cat TAG)" + declare -r NAME="$(cat NAME)" + declare -r BODY="$(sed 's/$/\\n/' CHANGELOG.md | tr -d '\n' | sed 's/\"/\\"/g')" + declare -r DATA="{\"tag_name\":\"${TAG}\", \"target_commitish\": \"main\",\"name\": \"${NAME}\", \"body\": \"${BODY}\", \"draft\": false, \"prerelease\": false}" + + curl -f -X POST -H "Content-Type:application/json" \ + -H "Authorization: Token ${GITHUB_TOKEN}" "${API_URL}" \ + -d "${DATA}" +popd diff --git a/scripts/github-upload-assets.sh b/scripts/github-upload-assets.sh new file mode 100755 index 0000000..3560d5c --- /dev/null +++ b/scripts/github-upload-assets.sh @@ -0,0 +1,34 @@ +#!/usr/bin/env bash +# +if [[ ${#} -ne 2 ]]; then + echo "Upload assets to GitHub release" + echo "Usage: ${0} GITHUB_REPO ASSETS_PATH" >>/dev/stderr + exit 1 +fi + +if [[ -z "${GITHUB_TOKEN}" ]]; then + echo "GITHUB_TOKEN not set" >>/dev/stderr + exit 1 +fi + +set -euo pipefail + +declare -r GITHUB_REPO="${1}" +declare -r ASSETS_PATH="${2}" + +declare -r API_URL=https://api.github.com/repos/${GITHUB_REPO}/releases +declare -r UPLOAD_URL=https://uploads.github.com/repos/${GITHUB_REPO}/releases + +pushd "${ASSETS_PATH}" + declare -r TAG="$(cat TAG)" + + ID=$(curl -s -H "Authorization: Token ${GITHUB_TOKEN}" "${API_URL}/tags/${TAG}" | jq ".id") + + for FILE in $(find ./ -mindepth 1 -maxdepth 1 -type f -name "*.tar.gz"); do + curl -f -H "Content-Type: $(file -b --mime-type ${FILE})" \ + -H "Authorization: Token ${GITHUB_TOKEN}" \ + --data-binary @${FILE} \ + "${UPLOAD_URL}/${ID}/assets?name=$(basename ${FILE})" + echo + done +popd