Skip to content

Commit

Permalink
chore: add application version support
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanilves committed Oct 12, 2022
1 parent 69c71e2 commit c98eae0
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 5 deletions.
43 changes: 38 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand All @@ -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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
8 changes: 8 additions & 0 deletions cmd/ttg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand All @@ -33,6 +37,10 @@ func main() {
flag.Usage = usage
flag.Parse()

if version {
shell.PrintAndExit(appVersion)
}

matches := flag.Args()

rootPath, err := scm.RootPath()
Expand Down
7 changes: 7 additions & 0 deletions pkg/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
30 changes: 30 additions & 0 deletions scripts/github-create-release.sh
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions scripts/github-upload-assets.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c98eae0

Please sign in to comment.