Skip to content

Instantly share code, notes, and snippets.

@bkcsoft
Last active March 12, 2018 11:29
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bkcsoft/8833077 to your computer and use it in GitHub Desktop.
Save bkcsoft/8833077 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
if [ $# -eq 0 ]; then
printf 1 "Usage: $(basename $0) [build-args [-- run-args [-- cmd-args] ] ]"
printf 1 " NOTE!!! the -rm-flag is hardcoded for build!"
fi
BUILD_ARG=()
RUN_ARG=()
CMD_ARG=()
ARG_SWITCH=0
while [ "${#@}" -ne 0 ]; do
if [ "$1" == "--" ]; then
let ARG_SWITCH++
shift
fi
case $ARG_SWITCH in
0)
BUILD_ARG+=("$1")
;;
1)
RUN_ARG+=("$1")
;;
2)
CMD_ARG+=("$1")
;;
esac
shift
done
NAME="$(basename $PWD)"
if [ "$(dirname $PWD)/${NAME}" != "${PWD}" ]; then
printf 2 "Failed to get basename"
exit 1
fi
TAG="$(date +%Y%m%d-%H%M)"
if git rev-parse >/dev/null 2>&1 ; then
# I can has git... get revision \o/
COMMIT="$(git log -1 | awk '/commit/ {print $2}')"
TAG="${TAG}-${COMMIT:0:6}"
fi
printf 2 "Building ${NAME}:${TAG}..."
if ! docker build -rm "${BUILD_ARG[@]}" -t="${NAME}:${TAG}" . ; then
printf 2 "Build failed :("
exit 1
fi
printf 2 "Running ${NAME}:${TAG}..."
docker run "${RUN_ARG[@]}" "${NAME}:${TAG}" "${CMD_ARG[@]}"
@micklove
Copy link

micklove commented Mar 12, 2018

Thanks :) Also, found out u can use

# for long sha, use "%H"
COMMIT=$(git log -1 --pretty=format:"%h")
TAG="${TAG}-${COMMIT}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment