启动后端
脚本
api-release.sh
#!/bin/bash
# 定义常量
#TAG=dev_mvp1.1
APP_NAME=zhaojian-api
# 分支
BRANCH=$1
# 镜像名称
TAG=$2
# build core
echo "\ncd zhaojian-core/"
cd zhaojian-core/
echo "\ngit checkout ${BRANCH}"
git checkout ${BRANCH}
#!/bin/bash
# 定义常量
#TAG=dev_mvp1.1
APP_NAME=zhaojian-api
TAG=$2
BRANCH=$1
# build core
echo "\ncd zhaojian-core/"
cd zhaojian-core/
echo "\ngit checkout ${BRANCH}"
git checkout ${BRANCH}
echo "\ngit pull"
git pull
echo "\nmvn clean install -Dmaven.test.skip=true"
mvn clean install -Dmaven.test.skip=true
# package api
echo "\ncd ../zhaojian-api"
cd ../zhaojian-api
echo "\ngit checkout ${BRANCH}"
git checkout ${BRANCH}
echo "\ngit pull"
git pull
echo "\nmvn clean install -Dmaven.test.skip=true"
mvn clean package -Dmaven.test.skip=true
# 编译镜像
echo "\nsudo docker build -t ccr.ccs.tencentyun.com/zhaojian-api:${TAG} ."
sudo docker build -t ccr.ccs.tencentyun.com/zhaojian-api:${TAG} .
# 获取镜像ID
imageId=`sudo docker images | grep ${APP_NAME} | grep ${TAG} | awk '{print $3}'`
echo "find ${APP_NAME} imageId ${imageId}"
# 查看所有容器
sudo docker ps -a
# 获取容器ID
containerId=`sudo docker ps -a | grep $APP_NAME | awk '{print $1}'`
echo "${APP_NAME} contaniner id is ${containerId}"
# 停止并删除容器
echo "\nsudo docker stop ${containerId};sudo docker container rm ${containerId}"
sudo docker stop $containerId;sudo docker container rm $containerId
# 启动容器
echo "sudo docker run -d -m 3g -p 8000:8000 --name ${APP_NAME} ${imageId}"
sudo docker run -d -m 3g -p 8000:8000 --name ${APP_NAME} ${imageId}
# 获取启动的容器ID
newContainerId=`sudo docker ps -a | grep ${APP_NAME} | awk '{print $1}'`
while [ ! -n "$newContainerId" ]
do
# 为空,重新获取容器ID
echo "try to find containerId"
newContainerId=`sudo docker ps -a | grep ${APP_NAME} | awk '{print $1}'`
done
# 查看日志
sudo docker logs --tail=500 -f ${newContainerId}
启动前端
脚本
web-release.sh
#!/bin/bash
# 定义常量
# TAG=ublok_v1.2_ppe-6
APP_NAME=merchant-web
BRANCH=$1
IMAGE_TAG=$2
# 查看所有容器
# sudo docker ps -a
# 获取容器ID
# containerId=`sudo docker ps -a | grep $APP_NAME | awk '{print $1}'`
# echo "zhaojian-api contaninerId ${containerId}"
# sudo docker stop $containerId
# echo "stop ${APP_NAME} container ${containerId} "
# sudo docker container rm $containerId
# echo "rm container ${containerId}\n\n"
echo "cd merchant-web"
cd merchant-web
echo "git checkout ${BRANCH}"
git checkout ${BRANCH}
echo "git pull"
git pull
echo "sudo rm -rf dist"
sudo rm -rf dist
# 打包
echo "sudo npm run build:prod"
sudo npm run build:prod
# 编译镜像
echo "sudo docker build -t ccr.ccs.tencentyun.com/merchant-web:${IMAGE_TAG} ."
sudo docker build -t ccr.ccs.tencentyun.com/merchant-web:${IMAGE_TAG} .
# 获取最新镜像id
imageId=`sudo docker images | grep ${APP_NAME} | grep ${IMAGE_TAG} | awk '{print $3}'`
echo "The image id is ${imageId}"
# 查看所有容器
echo "sudo docker ps -a"
sudo docker ps -a
# 获取容器ID
containerId=`sudo docker ps -a | grep $APP_NAME | awk '{print $1}'`
echo "The ${APP_NAME} contaniner id is ${containerId}"
# 停止容器
echo "sudo docker stop ${containerId}"
sudo docker stop ${containerId}
# 删除容器
echo "sudo docker container rm ${containerId}"
sudo docker container rm ${containerId}
# 启动新容器
echo "sudo docker run -d -m 200m -p 8001:8001 --name ${APP_NAME} ${imageId}"
sudo docker run -d -m 200m -p 8001:8001 --name ${APP_NAME} ${imageId}
# 查看运行中的容器
echo "sudo docker ps"
sudo docker ps
newContainerId=`sudo docker ps | grep ${APP_NAME} | awk '{print $1}'`
echo "The new container id is ${newContainerId}"
echo "sudo docker logs --tail=500 -f ${newContainerId}"
sudo docker logs --tail=500 -f ${newContainerId}
#end
运行脚本示例
如下图
sh api-release.sh dev_2.0 dev_11
第一个参数是分支(dev_2.0),第二个参数是docker镜像名称(dev_11)