Skip to content

Instantly share code, notes, and snippets.

@appleboy
Last active May 19, 2016 01:01
Show Gist options
  • Save appleboy/a800f02cf37964d328d58e1ef5740384 to your computer and use it in GitHub Desktop.
Save appleboy/a800f02cf37964d328d58e1ef5740384 to your computer and use it in GitHub Desktop.
Deploy web by docker.
#!/bin/bash
# Author: Bo-Yi Wu
# Email: appleboy.tw@gmail.com
# License: MIT
CONTAINER=$1
MODE=$2
CURRENT=$(pwd)
DEPLOY_HOME="${HOME}/${CONTAINER}"
SOURCE="${HOME}/source"
PID_FOLDER="${HOME}/run"
PROJECT=$(date '+%Y%m%d%H%M%S%s')
output() {
color="32"
if [ "$2" = 1 ]; then
color="31"
fi
printf "\033[${color}m"
echo $1
printf "\033[0m"
}
if [ "$#" -lt 2 ]; then
echo "Usage: ./$0 container_name mode"
exit 1
fi
if [ -e ${DEPLOY_HOME} ] && [ ! -L ${DEPLOY_HOME} ]; then
output "${DEPLOY_HOME} is exist, please remove it first." 1
exit 1
fi
if [ ! -f "${CURRENT}/.env.${MODE}" ]; then
output "Missing ${MODE} config file, path: ${CURRENT}/.env.${MODE}" 1
exit 1
fi
[ ! -d ${PID_FOLDER} ] && mkdir -p ${PID_FOLDER}
[ ! -d ${SOURCE} ] && mkdir -p ${SOURCE}
[ ! -f ${PID_FOLDER}/${CONTAINER} ] && touch ${PID_FOLDER}/${CONTAINER}
[ -f ${PID_FOLDER}/${CONTAINER} ] && OLD_PID=$(cat ${PID_FOLDER}/${CONTAINER})
output "Copy config file from .env.${MODE} to .env"
cp ${CURRENT}/.env.${MODE} .env
output "Copy source file to ${SOURCE}/${PROJECT} folder."
cp -r ${CURRENT} ${SOURCE}/${PROJECT}
output "Start link ${CURRENT} folder to ${DEPLOY_HOME}."
[ -L ${DEPLOY_HOME} ] && unlink ${DEPLOY_HOME}
ln -sf ${SOURCE}/${PROJECT} ${DEPLOY_HOME}
if [ -n $OLD_PID ]; then
[ -d "${SOURCE}/$OLD_PID" ] && rm -rf ${SOURCE}/$OLD_PID
fi
output "Write project number to pid file."
echo ${PROJECT} > ${PID_FOLDER}/${CONTAINER}
RUNNING=$(docker inspect --format="{{ .State.Running }}" ${CONTAINER} 2> /dev/null)
if [ $? -eq 1 ]; then
output "UNKNOWN - ${CONTAINER} does not exist." 1
output "Start ${CONTAINER} container."
SOURCE=${SOURCE} PROJECT=${PROJECT} docker-compose -f docker/docker-compose.production.yml up -d --force-recreate
fi
output "Start DB Migration."
docker exec labs-api php artisan migrate
STARTED=$(docker inspect --format="{{ .State.StartedAt }}" ${CONTAINER})
NETWORK=$(docker inspect --format="{{ .NetworkSettings.IPAddress }}" ${CONTAINER})
output "OK - ✨ ✨ ✨ ${CONTAINER} is running. IP: $NETWORK, StartedAt: $STARTED"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment