echo "=====准备开始发布==========="
#要构建的解决方案名称
solutionName=Electricity.ManageService
#.sln文件全路径
solutionDir=${solutionName}.sln
#.csproj文件全路径
csprojDir=${solutionName}.Api/${solutionName}.Api.csproj
#制定run的端口
port=6001
#项目发布的目录
webDir=/home/service
#静像名称
imageName=manageapi
#dotnet restore ${JENKINS_HOME}/workspace/${JOB_NAME}
echo "===========正在编译程序=============="
#发布网站到webDir
dotnet publish ${JENKINS_HOME}/workspace/${JOB_NAME}/${csprojDir} -c Release -o ${webDir}/${JOB_NAME} /p:Version=1.0.${BUILD_NUMBER}
echo "=====正在操作Docker==========="
containerps=$(docker ps -a -f name=${imageName} -q)
for alpha in "$containerps";do
if [ "${alpha}" == "" ];then
echo "不存指定容器!"
else
docker stop ${alpha}
docker rm ${alpha}
fi
done
dockerlist=$(docker images ${imageName} -q)
for alpha2 in "${dockerlist}";do
if [ "${alpha2}" == "" ];then
echo "不存在指定镜像!"
else
docker rmi ${alpha2}
fi
done
echo "===========正在构建镜像=============="
#通过Dockerfile重新构建镜像
cd ${webDir}/${JOB_NAME}
docker build -t ${imageName} .
#docker run容器并绑定到端口
docker run --name ${imageName} -p ${port}:${port} -d ${imageName}
echo "===========发布成功=============="