基于TeamCity的Asp.Net Core 的容器化自动部署

前提:Dockerfile的路径与脚本

路径与解决方案同一级目录

脚本内容

FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
ENV ASPNETCORE_ENVIRONMENT Production
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ["Saas.XXXXXXXXXXXX.GateWayService/Saas.XXXXXXXXXXXX.GateWayService.csproj", "Saas.XXXXXXXXXXXX.GateWayService/"]
COPY ["XXXX.Library/XXXX.Library.csproj", "XXXX.Library/"]
COPY ["Saas.BaseService.Client/Saas.BaseService.Client.csproj", "Saas.BaseService.Client/"]
RUN dotnet restore "Saas.XXXXXXXXXXXX.GateWayService/Saas.XXXXXXXXXXXX.GateWayService.csproj"
COPY . .
WORKDIR "/src/Saas.XXXXXXXXXXXX.GateWayService"
RUN dotnet build "Saas.XXXXXXXXXXXX.GateWayService.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "Saas.XXXXXXXXXXXX.GateWayService.csproj" -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Saas.XXXXXXXXXXXX.GateWayService.dll"]

总体步骤

1 restore

2 build

3 Create Images

4 push images

echo '================rename image================'
docker tag getwayservice:v%build.number% registry.cn-beijing.aliyuncs.com/自定义名字/gatewayservice:v%build.number%
echo '================name:registry.cn-beijing.aliyuncs.com/自定义名字
/gatewayservice:v%build.number%================'
echo '================login aliyun================'
docker login --username=账号  --password=密码  registry.cn-beijing.aliyuncs.com
echo '================push image================'
docker push registry.cn-beijing.aliyuncs.com/自定义名字
/gatewayservice:v%build.number%
echo '================push succeed================'

5 Clear Local Image(节省本地存储资源)

echo '================remove local image================'
docker rmi getwayservice:v%build.number%
docker rmi registry.cn-beijing.aliyuncs.com/自定义/gatewayservice:v%build.number%
echo '================remove succeed================'

6 Deploy Image

echo '==ssh connect 目标服务器=='
ssh -t -t 账号@目标服务器 << remotessh 
echo '==ssh connect successed=='
echo '==docker login registry.cn-beijing.aliyuncs.com=='
docker login --username=账号  --password=密码  registry.cn-beijing.aliyuncs.com
docker pull registry.cn-beijing.aliyuncs.com/自定义/gatewayservice:v%build.number%
docker tag registry.cn-beijing.aliyuncs.com/自定义/gatewayservice:v%build.number% gatewayservice:v%build.number%
docker rmi registry.cn-beijing.aliyuncs.com/自定义/gatewayservice:v%build.number%
docker stop gatewayservice
docker rm gatewayservice
docker rmi gatewayservice:v%last.build.number%
docker run -d -p 8080:80 --name gatewayservice gatewayservice:v%build.number%
echo '==DONE=='
exit 
remotessh