Skip to content

Instantly share code, notes, and snippets.

@rigelk
Forked from bkcsoft/docker-build-n-run.sh
Last active August 29, 2015 14:12
Show Gist options
  • Save rigelk/c753e2c677ef750d0506 to your computer and use it in GitHub Desktop.
Save rigelk/c753e2c677ef750d0506 to your computer and use it in GitHub Desktop.
un script pour lancer un build docker suivi d’un run de l’image ainsi créée, tous deux pleinement paramétrés.
#!/bin/bash
if [ $# -eq 0 ]; then
echo "Usage: $(basename $0) [build-args [-- run-args [-- cmd-args] ] ]"
echo " NOTE: the -rm-flag is hardcoded for build!"
exit 1
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 SECTION
TAG="$(date +%Y%m%d-%H%M)"
# Jenkins tag sub-section
if [ -n $BUILD_TAG ]; then
TAG=$BUILD_TAG
fi
# Git tag sub-section
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
# SVN tag sub-section
#TODO
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[@]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment