Skip to content

Instantly share code, notes, and snippets.

@claudioc
Last active April 29, 2018 07:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save claudioc/880d021e259a834db9c92492710bf96e to your computer and use it in GitHub Desktop.
Save claudioc/880d021e259a834db9c92492710bf96e to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -e
type docker >/dev/null 2>&1 || { echo >&2 "docker is not available. Aborting."; exit 1; }
GIT_BRANCH=$(git symbolic-ref HEAD | sed 's/refs\/heads\///')
# Configuration section
IMAGE_NAME=myproject
LIVE_BRANCH=master
TEST_BRANCH=testing
EXPOSED_PORT=80
LOCAL_PORT=8000
BEFORE_BUILD="npm run build"
TAG_FOR_LIVE="latest"
TAG_FOR_STAGING="test"
# end configuration section
TAG_NAME=${TAG_FOR_STAGING}
if [ "${GIT_BRANCH}" == "${LIVE_BRANCH}" ]; then
TAG_NAME=${TAG_FOR_LIVE}
fi
COMMIT_ID=$(git rev-parse --short HEAD)
TAGGED_WITH_COMMIT="${IMAGE_NAME}:${COMMIT_ID}"
TAGGED_WITH_NAME="${IMAGE_NAME}:${TAG_NAME}"
build() {
NODE_ENV=production ${BEFORE_BUILD} || exit 1
docker build \
-t ${TAGGED_WITH_COMMIT} \
-t ${TAGGED_WITH_NAME} \
./
}
run() {
local container=$(docker ps | grep ${TAGGED_WITH_NAME} | awk '{print $1}')
if [ "${container}" == "" ]; then
docker run -p ${LOCAL_PORT}:${EXPOSED_PORT} ${TAGGED_WITH_NAME}
else
echo "Container is already running"
fi
}
stop() {
local container=$(docker ps | grep ${TAGGED_WITH_NAME} | awk '{print $1}')
if [ "${container}" != "" ]; then
docker stop -t 0 ${container}
else
echo "Nothing to stop ¯\_(ツ)_/¯"
fi
}
sh() {
local container=$(docker ps | grep ${TAGGED_WITH_NAME} | awk '{print $1}')
if [ "${container}" != "" ]; then
docker exec -ti ${container} /bin/bash
else
echo "No running image found ¯\_(ツ)_/¯"
fi
}
case "$1" in
build)
build
;;
run)
run
;;
start)
stop
build
run
;;
restart)
stop
run
;;
stop)
stop
;;
sh)
sh
;;
*)
echo $"Usage: $0 {build|run|start|restart|stop|sh}"
exit 1
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment