Skip to content

Instantly share code, notes, and snippets.

@pasdam
Last active January 3, 2019 18:05
Show Gist options
  • Save pasdam/1b77a2e0c0e0ab3cd94589aeed052fe8 to your computer and use it in GitHub Desktop.
Save pasdam/1b77a2e0c0e0ab3cd94589aeed052fe8 to your computer and use it in GitHub Desktop.
This script allows to build a docker image generating the tag version from the git revision
#!/bin/sh
script_name="${BASH_SOURCE[${#BASH_SOURCE[@]} - 1]}"
script_folder=$(cd `dirname $script_name` && pwd)
print_usage() {
echo "Usage: $script_name <options>"
echo ""
echo "Options:"
echo "\t-a <arguments>\targuments to pass to the docker build command"
echo "\t-d\t\tfor a dry run"
echo "\t-f <folder>\tto specify the folder with the Dockerfile; default value: current folder"
echo "\t-n <name>\tto specify the image name; default: name of the folder with the Dockerfile"
echo "\t-t <tag>\tto specify the image tag; default: git revision"
echo "\t-v\t\tto print every step performed"
}
dry_run=false
folder=$(pwd)
name=
tag=
verbose=false
arguments=
while getopts ":a:df:n:t:v" option
do
case "${option}"
in
a) arguments=$OPTARG;;
d) dry_run=true;;
f) folder=$(cd $OPTARG && pwd);; # make sure the path is absolute
n) name=$OPTARG;;
t) tag="$OPTARG";;
v) verbose=true;;
:) print_usage; >&2 echo "Missing argument for option -$OPTARG"; exit 1 ;;
?) print_usage; >&2 echo "Unknown option: ${option}"; exit 1;;
esac
done
shift $((OPTIND -1))
if [ -z "$name" ]; then
name=${folder##*/}
fi
if [ -z "$tag" ]; then
tag=$(cd ${folder} && git rev-parse --short HEAD 2> /dev/null)
if [[ tag -eq "" ]]; then
tag="0000000"
fi
if [[ ! -z $(git status -s) ]]; then
# the local repository contains uncommitted files
echo "\033[33mThe repository contains local changes, this image should only be used for testing\033[0m"
tag=$tag".dirty"
fi
fi
echo "Building docker image with tag "$name':'$tag
if [[ "$verbose" = true || "$dry_run" = true ]] ; then
echo 'docker build --tag '$name':'$tag $arguments $folder
fi
if [ "$dry_run" = false ]; then
docker build --tag $name":"$tag $arguments $folder
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment