Last active
April 15, 2019 14:41
-
-
Save marton78/37f1844e94be0a44cb30e8ed5eb64bca to your computer and use it in GitHub Desktop.
Bash script to auto-generate versioning information based on Git history
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script determines a version string and a deployment target based on Git history | |
# | |
# Returns: VERSION DEPLOYMENT_TARGET | |
# where DEPLOYMENT_TARGET is [prod|stage|dev|''] | |
branch=$(git rev-parse --abbrev-ref HEAD) | |
tag=$(git describe --tags --exact-match 2>/dev/null) | |
# if on master, require an explicit tag | |
# version will be the tag | |
if [[ $branch == master ]]; then | |
if [ -z "$tag" ]; then | |
echo "ERROR: Untagged master branch" >>/dev/stderr | |
exit 1 | |
fi | |
echo $tag prod | |
# if on a release/X branch, forbid explicit tags | |
# version will be X-betaN | |
elif [[ $branch == release/* ]]; then | |
if [ -n "$tag" ]; then | |
echo "ERROR: Explicitly tagged commit ${tag} in branch ${branch}" >>/dev/stderr | |
exit 1 | |
fi | |
after_the_slash=${branch#*/} | |
base=$(git merge-base -a HEAD develop) | |
count=$(git rev-list --count HEAD ^${base}) | |
echo ${after_the_slash}-beta${count} stage | |
# if on a hotfix branch, forbid explicit tags | |
# anything after "hotfix" in the branch name will be ignored | |
# version will be $last_tag-hotfixN | |
elif [[ $branch == hotfix* ]]; then | |
if [ -n "$tag" ]; then | |
echo "ERROR: Explicitly tagged commit ${tag} in branch ${branch}" >>/dev/stderr | |
exit 1 | |
fi | |
last_tag=$(git describe --abbrev=0 2>/dev/null) | |
if [ -z "$last_tag" ]; then | |
echo "ERROR: No tag found in history of ${branch}" >>/dev/stderr | |
exit 1 | |
fi | |
count=$(git rev-list --count HEAD ^${last_tag}) | |
echo ${last_tag}-hotfix${count} stage | |
# if on the devel/develop branch, allow explicit tags | |
# version will be either $devel-$tag | |
# or in the format $devel-YYMMDD-HASH | |
elif [[ $branch == devel* ]]; then | |
if [ -z "$tag" ]; then | |
ver=$(git log -1 --pretty='%cd-%h' --date=format:'%y%m%d') | |
else | |
ver=$tag | |
fi | |
echo devel-${ver} dev | |
# in any other branch, allow explicit tags | |
# version will be either $branch-$tag | |
# or in the format $branch-YYMMDD-HASH | |
else | |
if [ -z "$tag" ]; then | |
ver=$(git log -1 --pretty='%cd-%h' --date=format:'%y%m%d') | |
else | |
ver=$tag | |
fi | |
echo "WARNING: No deployment target for ${branch}" >>/dev/stderr | |
echo ${branch}-${ver} | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment