使用docker版jenkins部署dotnetcore应用

安装docker版jenkins

因为jenkinsdocker版本本身没有 dotnetcore的环境,所以我们需要先自动动手制作下包含dotnet环境的jenkins Docker Container
Dockerfile

FROM jenkins/jenkins

# Switch to root to install .NET Core SDK
USER root

# Show distro information!
RUN uname -a && cat /etc/*release

# Based on instructiions at https://www.microsoft.com/net/download/linux-package-manager/debian9/sdk-current
# Install dependency for .NET Core 2
RUN apt-get update
RUN apt-get install -y curl libunwind8 gettext apt-transport-https

# Based on instructions at https://www.microsoft.com/net/download/linux-package-manager/debian9/sdk-current
# Install microsoft.qpg
RUN curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
RUN mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg
RUN sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-debian-stretch-prod stretch main" > /etc/apt/sources.list.d/dotnetdev.list'

# Install the .NET Core framework
RUN apt-get update
RUN apt-get install -y dotnet-sdk-2.1.4

# Switch back to the jenkins user.
USER jenkins

为了方面我已经把配置信息放到了github上大家可以访问直接使用

https://github.com/YahuiWong/jenkins-dotnet-core

使用步骤

初始化docker环境

git clone https://github.com/YahuiWong/jenkins-dotnet-core.git
cd jenkins-dotnet-core
sh init.sh
docker-compose up -d

初始化配置jenkins

  1. Open ip:8080 on the browser

  2. vi jenkins_home/secrets/initialAdminPassword & Set the initialAdminPassword string to your jenkins page

至此安装完毕。

配置jenkins构建dotent core的任务

构建一个自由风格的软件项目

jenkins-docker-dotnet-core-publish-201853118046

配置源码管理资料

jenkins-docker-dotnet-core-publish-201853118236

注意:初次配置的话 需要在 添加一个可以访问代码地址的 Credentials

配置构建脚本

如下图步骤添加打包脚本
jenkins-docker-dotnet-core-publish-201853118512

打包脚本示例

echo '============查看打包环境================' 
pwd
ls
echo $PATH
whoami
which dotnet
dotnet --info
dotnet --version
echo '============================begin restore======================================='
dotnet restore
echo '============================end restore======================================='
echo '============================cd project======================================='
cd ./您的项目路径
echo '============================begin build======================================='
dotnet build # 为了生成XML注释文件 用于swagger注释
rm -rf $WORKSPACE/jenkins_publish
mkdir $WORKSPACE/jenkins_publish
dotnet publish -c:Release -o $WORKSPACE/jenkins_publish # 如果针对给定运行时发布项目带上-r 如:-r centos.7-x64
cp ./bin/Debug/netcoreapp2.0/您的项目路径.xml $WORKSPACE/jenkins_publish/ # 拷贝swagger注释
echo '============================end build======================================='

配置发布途径

我这里使用的是 jenkinsPublish Over FTP插件,安装好此插件之后在 系统管理->系统设置->Publish over FTP 里可以新增一个你要发布的ftp服务器信息。为了保证ftp可以正常连接建议尝试下右下角的Test Configuration确认success

上面我们已经配置好Publish Over FTP要用的ftp账号,新增我们新增构建后操作如下图选择此插件
jenkins-docker-dotnet-core-publish-2018531182746

然后选择配置好的ftp选项,配置如下所示
jenkins-docker-dotnet-core-publish-2018531183019

注意:这里的配置信息中 jenkins_publish是和打包脚本对应的

保存打包任务

立即构建

点击立即构建之后,把构建任务开始进行中
jenkins-docker-dotnet-core-publish-2018531184031

如果想看的实时的构建信息可以点击如下图看到控制台输出
jenkins-docker-dotnet-core-publish-2018531184219

构建完毕

打开部署站点地址,发现站点已经运行起来了

jenkins-docker-dotnet-core-publish-2018531184328

转载请注明出处 http://blog.yahui.wang/2018/05/31/jenkins-docker-dotnet-core-publish/

猜你喜欢

转载自www.cnblogs.com/YahuiWang/p/jenkins-docker-dotnet-core-publish.html