Local Docker Jenkins build dotnet core web applications to the Linux server Docker

1, ready to work

surroundings

  • Local:Windows ,Docker
  • Code repository:Git
  • Server:Linux ,Docker

Premise preparation

  1. Have created a dockerfilefile of dotnet core 3 webthe project
    to create a new project dotnet web 3.0, add Dockerfile clip file in the project file, as follows:

    FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
    WORKDIR /app
    EXPOSE 80
    COPY . .
    ENTRYPOINT ["dotnet", "WebApplication.dll"]
  2. Ready git repository, the project code will upload up
  3. Built with dotnet core 3.0 环境thejenkins

    FROM jenkins/jenkins:lts
    # 切换root用户安装东西
    USER root
    # Show distro information!
    RUN uname -a && cat /etc/*release
    RUN apt-get update
    RUN apt-get install -y curl libunwind8 gettext apt-transport-https
    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'
    RUN apt-get update
    RUN apt-get install -y dotnet-sdk-3.1
    # 切换回来jenkins用户
    USER jenkins

Because there is no dotnetcore jenkins environment, it is necessary to create a local support dotnetcore environment.
Find a place to create a new folder, create dockerfile file, the contents above.

  1. Mirror build and run the container

    docker build . -t jenkins_dotnet

    Here the waiting time will be longer -

    # 运行刚刚构建好的容器
    docker run -d  --name jenkins -p 8080:8080 jenkins_dotnet
  2. Open jenkins, install the recommended plugins
    to open localhost: 8080 , you can see

    docker exec -it jenkins bash
    dotnet --version
    cat /var/jenkins_home/secrets/initialAdminPassword

    Copy the password to log out, click on the 左边按钮install recommended plugins

    Here will be relatively long time, if part of the installation fails, the other remaining after installation click to retry, if it is not on the server docker, can now go to hold, the apparatus may also be connected to the server, to execute docker pull mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slimremove dotnet core 3.1 times the pull of the mirror, and so will create a mirror image of it will be a little faster

    Plug-in installation is complete, there will be some administrators to create and address configuration, do it.

    If there are plug-in installation fails, try to install the plug point too heavy, it is necessary now docker restart jenkinsunder restart the application, and then reopen localhost: 8080 using the administrator login created.

2, improve the environment, the official start

Oh, and some need to install plug-ins =. = ||

You need to install plug-ins:

  • SCP publisher
  • Publish Over SSH
  • Environment Injector

进入 系统管理>插件管理->可选插件->输入插件名称->勾选需要插件->点击安装

安装完后,设置下这些插件,进入系统管理->系统配置,

  1. SCP publisher设置
    Ctrl + F 搜下 SCP找到SCP repository hosts-SCP sites设置位置,点新增
    HostName: 服务器IP地址
    Port:端口,默认22、
    Root Repository Path:文件存放目录
    User Name:登录用户名
    Password/Passphrase:密码

  2. Publish Over SSH 设置
    Ctrl + F 搜下 SCP找到SCP repository hosts-SSH Server设置位置,点新增再点高级勾选上 Use password authentication, or use a different key
    Name:名称
    Hostname:服务器IP地址
    Username:登录用户名
    Remote Directory:远程目录
    Passphrase / Password:密码
    Port:连接端口(默认22)

配置完后别忘记点保存。

创建构建任务

点击新建任务, 选择构建一个自由风格的软件项目

  1. 源码管理设置
    选择Git,在Repository URL填入Git仓库地址

  2. 触发构建器
    可以配置一些定时构建等,我这里只是测试所以没有选择触发器。

  3. 构建环境
    1. 勾选Delete workspace before build starts
    2. 勾选Inject environment variables to the build process,存放构建是需要用到的环境变量
  4. 构建- 添加执行shell步骤
    增加构建步骤-执行shell,脚本如下:

    #切换目录
    cd ./WebApplication
    #还原nuget包
    dotnet restore
    #编译
    dotnet build
    #删除之前发布文件
    cd ./bin
    rm -rf web-publish
    rm -f web-publish.tar
    cd ..
    #发布
    dotnet publish -o ./bin/web-publish
    #删除配置文件
    cd ./bin/web-publish
    cp ../../Dockerfile .
    rm -rf config
    cd ..
    #压缩
    tar -cvf web-publish.tar web-publish 
  5. 构建后步骤
    1. 上传文件到服务器
      增加构建后操作步骤-选择Publish artifacts to SCP Repository ,填入需要上传的压缩文件

      这里文件目录的基础目录是workspace,如果不知道具体的地址,可以先不创建构建后步骤保存下,然后点击立即构建,等待成功后,点击工作空间看下文件路径是怎样的,比如我的是这样的:

      得到压缩文件目录是WebApplication/bin/web-publish.tar

    2. 添加 服务器上要执行的shll命令 步骤

    增加构建后操作轴- 选择 Send build artifacts over SSH

    这里要新增两个Transfers Set,在第一个TransfersExec command输入创建镜像脚本:

    # 工作目录
    WORK_DIR="/root/publish/WebApplication";
    cd ${WORK_DIR}
    # 删除原有发布文件夹
    rm -rf web-publish;
    # 解压
    tar -xvf web-publish.tar;
    #删除文件压缩包
    rm -f web-publish.tar;
    #切换生成目录
    cd web-publish/
    #备份镜像
    #停止容器
    docker stop ${DOCKER_CONTAINER_NAME};
    #删除容器
    docker rm ${DOCKER_CONTAINER_NAME};
    #删除镜像
    docker rmi $(docker images | grep ${DOCKER_IMAGE_NAME});
    #创建镜像
    docker build -t ${DOCKER_IMAGE_NAME} ./;

    在第二个TransfersExec command,输入运行容器命令:

    # 运行容器
    docker run -d -p 8001:80 --name  ${DOCKER_CONTAINER_NAME}  ${DOCKER_IMAGE_NAME}

    到这里所有的设置都已经搞好啦,点击立即构建 测试一下吧~

第一次研究这个,本来以为本地用Docker直接拉取个jenkins会简单快速一点,还是花了不少时间~

Guess you like

Origin www.cnblogs.com/xiao24/p/docker_jenkins_dotnetcore3.html