Skip to content

Instantly share code, notes, and snippets.

@ben-p-commits
Last active May 5, 2021 15:29
Show Gist options
  • Save ben-p-commits/e1864ee6519401b23934e729bf6deabc to your computer and use it in GitHub Desktop.
Save ben-p-commits/e1864ee6519401b23934e729bf6deabc to your computer and use it in GitHub Desktop.
CI tagging script
#color utilities
Y="\033[1;33m"
R="\033[0;31m"
G="\033[0;32m"
B="\033[0;34m"
NC="\033[0m"
# handy functions
usage() { echo "Usage: ci_tag [-t <tf1|tf2|release>] [-m <release note>]" 1>&2;}
error() { echo "${R}$1${NC}";}
die() { kill -INT $$ ;}
ask() {
local prompt default REPLY
while true; do
if [ "${2:-}" = "Y" ]; then
prompt="Y/n"
default=Y
elif [ "${2:-}" = "N" ]; then
prompt="y/N"
default=N
else
prompt="y/n"
default=
fi
# Ask the question (not using "read -p" as it uses stderr not stdout)
echo -n "$1 [$prompt]:"
# Read the answer (use /dev/tty in case stdin is redirected from somewhere else)
read REPLY </dev/tty
# Default?
if [ -z "$REPLY" ]; then
REPLY=$default
fi
# Check if the reply is valid
case "$REPLY" in
Y*|y*) return 0 ;;
N*|n*) return 1 ;;
esac
done
}
#check that there are args
if [[ $# -eq 0 ]]; then
error "No arguments provided" && usage && die;
fi
while getopts ":t:m:h" o; do
case "${o}" in
t) t=${OPTARG};;
m) m=${OPTARG};;
*) error "you must provide options for -t and -m" && usage && die;;
esac
done
# set prefix
case $t in
tf1) prefix="TF1" && target="testflight 1";;
tf2) prefix="TF2" && target="testflight 2";;
release) prefix="R" && target="production";;
*) error "Only <tf1|tf2|release> are valid options for -t" && usage && die;;
esac
# check for release note
if [[ -z $m ]]; then
error "You must provide a release note with -m!" && usage && die;
fi
#check for git repo
if ! $(git rev-parse --is-inside-work-tree); then
error "Must be invoked in a git repository!" && usage && die;
fi
commit=$(git rev-parse --short HEAD);
tagcommand="git tag -a ${prefix}_${commit} -m \"${m}\"";
pushcommand="git push --tags";
branch="$(git symbolic-ref --short HEAD)";
#warn the user of what they are about to do
echo "\nCommit ${commit} on ${B}${branch}${NC} will be tagged ${G}${prefix}_${commit}${NC} for ${target}.\n";
if ask "Proceed and push tags?" N; then
echo;
echo "${G}Awesome! Here we go.${NC}";
eval $tagcommand && eval $pushcommand;
echo "${G}Done!${NC} :)"
else
error "\nNevermind!" && die;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment