Skip to content

Instantly share code, notes, and snippets.

@adrianorsouza
Last active July 24, 2020 22:11
Show Gist options
  • Save adrianorsouza/d970eecefa5c9c5db714d439587d1da6 to your computer and use it in GitHub Desktop.
Save adrianorsouza/d970eecefa5c9c5db714d439587d1da6 to your computer and use it in GitHub Desktop.
Script that makes it easier to automate the make a release of a project using git and annotated tag
#!/usr/bin/env bash
#
# Script to make a new release of this project.
#
# @author Adriano Rosa <https://adrianorosa.com>
# @date: 2020-07-24 14:47
# @link: https://gist.github.com/adrianorsouza/d970eecefa5c9c5db714d439587d1da6
#
# This bash script is a simple version of the npm package:
# `release-task` https://github.com/adrianorsouza/release-task.
#
# It was created to provide to same automated release task where
# does not require nodejs to be installed in the system.
#
#
# NOTE: This script will do the following:
# - update the CHANGELOG.md
# - commit the changes
# - create a new tag
# - push to origin
#
# @usage ./release.sh 3.2.0
# ----------------------------------------------------------
set -e
DATE=$(date +%Y-%m-%d)
LATEST_TAG=`git describe --abbrev=0`
CHANGES=`git --no-pager log @{push}.. --pretty=format:" - %ad - %s" --date=short`
CURRENT_BRANCH=`git rev-parse --abbrev-ref HEAD`
if [[ ! ${CHANGES} ]]; then
echo "There is no CHANGES to make a new release."
exit;
fi
VERSION=$1
echo "Making a new release ..."
if [[ -z $1 ]]; then
echo -n "Type in the new annotated tag [latest: ${LATEST_TAG}]: "
read VERSION
if [[ -z $VERSION ]]; then
echo "ERROR: The tag is invalid!"
exit;
fi
fi
# -------------------------
# UPDATE THE CHANGELOG
# -------------------------
CHANGELOG="CHANGELOG.md"
# The CHANGELOG string template
TEMPLATE="# Changelog
Version ${VERSION} - ${DATE}
===========================
${CHANGES}";
# The Annotated TAG message string template
TEMPLATE_MESSAGE_TAG="Release ${VERSION}
${CHANGES}";
CONTENT=`cat ${CHANGELOG}`
CONTENT_UPDATED="${CONTENT/\# Changelog/$TEMPLATE}"
tee -i ${CHANGELOG} <<EOF
${CONTENT_UPDATED}
EOF
clear
# -------------------------
# MAKE A COMMIT
# -------------------------
git diff
echo -n "Make a commit for ${CHANGELOG}? [y, n]: "
read MAKE_COMMIT
if [[ ${MAKE_COMMIT} == 'y' || ${MAKE_COMMIT} == 'yes' ]]; then
git commit ${CHANGELOG} -m "chore(release): ${VERSION}"
fi
# -------------------------
# CREATE AN ANNOTATED TAG
# -------------------------
echo -n "Creating a new TAG ${VERSION}? [y, n]: "
read CREATE_TAG
if [[ ${CREATE_TAG} == 'y' || ${CREATE_TAG} == 'yes' ]]; then
git tag ${VERSION} -a -m "${TEMPLATE_MESSAGE_TAG}"
echo "TAG was created!"
fi
# -------------------------
# PUSH TO ORIGIN
# -------------------------
echo -n "Release done! Push current branch ${CURRENT_BRANCH} to origin? [y, n]: "
read PUSH
if [[ ${PUSH} = 'y' || ${PUSH} == 'yes' ]]; then
git push origin ${CURRENT_BRANCH} ${VERSION}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment