1.前提
代码上传到git上。
jenkins可以成功拉取到git上的代码。
服务器上安装了dotnet sdk。
2.新建流水线。
3.流水线脚本:
node{
stage('Git Checkout'){
checkout([$class: 'GitSCM', branches: [[name: '*/test']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '261446b0-5a90-4c76-b060-39cd389b418c', url: 'ssh://*****/microservice-productmanagement-dotnetcore.git']]])
}
stage('dotnet Build'){
sh '''cd src
cd Server
rm -rf publishout
dotnet publish -c release -o publishout --force'''
}
stage('docker Build'){
sh '''
cd src
cd Server
cp /home/***/Dockerfile publishout
docker stop ${Image} | true
docker rm ${Image} | true
docker rmi ${Image}:${Tag} | true
docker build -t ${Image}:${Tag} publishout
docker run -d -p ${Port}:5000 --name ${Image} ${Image}:${Tag}
'''
}
}
说明流水线中的代码检出部分,可通过jenkins自动生成
脚本中的${Image}等,表示的是字符参数,可以通过下图设置:
4.Dockerfile文件
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /app
EXPOSE 5000
ENV ASPNETCORE_ENVIRONMENT=Test
ENV HostPort=5000
ENV ChannelPort=2000
COPY . .
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ENTRYPOINT ["dotnet", "Server.dll"]