Jenkins笔记(三)部署.Net Core Webapi项目

Jenkins笔记(三)部署.Net Core Webapi项目,第1张

Jenkins笔记(三)部署.Net Core Webapi项目 我的博客

博客首页

前言

本次部署未不使用docker,而是采用拉取代码后编译运行的方式。

下面先给张部署的流程图,再介绍部署的配置文件和可能存在的坑

开始部署

首先,进入Jenkins首页,选择新建一个任务,这里选择流水线:

这里选流水线只是为了练习一下pipeline的语法和部署流程,起始选自由风格其实更简单

选择pipeline之后什么都不用配置,只需要编写脚本即可,这里有两种写脚本的方式,第一种是直接在jenkins上写,第二种是在项目中写sh文件,然后jenkins中只需要bash xxx.sh运行脚本文件即可,可以根据自己的需要选一种,我这里直接用第一种方式,下面是全部的运行脚本:

pipeline {
    agent any

    stages {
        stage('git') 
        {
            steps {
                echo 'clone from gitee'
                sh '/usr/share/dotnet/dotnet --version'
                git branch: 'dev', url: 'https://gitee.com/lizhenghaodamowang/CloudWorking.git'
                echo 'clone finished'
            }
        }
        stage('build')
        {
            steps
            { 
                sh 'rm -rf ./bin/Debug/net5.0/*'
                sh 'rm -rf /root/stickynotesbackend/*'
                dotnetRestore project: './CloudWorkingBackend/CloudWorkingBackend.sln', sdk: 'dotnet5'
                dotnetBuild project: './CloudWorkingBackend/CloudWorkingBackend.sln', sdk: 'dotnet5'
              
                
            }
        }
        stage('kill process')
        {
            steps
            {
                sh '''
                set +e
k_pid=`lsof -t -i:5000`
echo $k_pid
if [ $k_pid>0 ]
then
  kill -9 $k_pid
fi
'''
            }
        }
        stage('run')
        {
            steps
            {
                sh 'cp -rf ./CloudWorkingBackend/CloudWorkingBackend/bin/Debug/net5.0/*  /root/stickynotesbackend/'
          
               sh '''JENKINS_NODE_cookie=dontKillMe
cd  /root/stickynotesbackend
nohup /usr/share/dotnet/dotnet /root/stickynotesbackend/CloudWorkingBackend.dll --urls="http://*:5000" >> /root/nohup.out &'''
                echo 'run success'
            }
        }
            
    }
}

上面的配置文件中有几个地方是需要注意的:

    dotnetRestore、dotnetBuild命令是需要安装.Net SDK Support插件的dotnet命令如果提示找不到的话,需要使用/usr/share/dotnet/dotnet替代JENKINS_NODE_cookie=dontKillMe一定要加这一句命令,否则在Jenkins的pipeline运行完后会自动杀掉子进程,导致webapi并没有运行起来,但是控制台结果是成功的服务器如果没有装sdk一定要装SDK,不是runtime

配置完后看构建日志是否正常,没有报错的情况下再通过ip,端口打开swagger界面看是否正常

过程中遇到的问题及解决方案 错误1:
/usr/share/dotnet/sdk/5.0.404/NuGet.targets(131,5): error : Unable to load the service index for source https://api.nuget.org/v3/index.json. [/var/lib/jenkins/workspace/stickynoteswebbackend/CloudWorkingBackend/CloudWorkingBackend.sln]
/usr/share/dotnet/sdk/5.0.404/NuGet.targets(131,5): error :   The SSL connection could not be established, see inner exception. [/var/lib/jenkins/workspace/stickynoteswebbackend/CloudWorkingBackend/CloudWorkingBackend.sln]
/usr/share/dotnet/sdk/5.0.404/NuGet.targets(131,5): error :   The remote certificate is invalid because of errors in the certificate chain: UntrustedRoot [/var/lib/jenkins/workspace/stickynoteswebbackend/CloudWorkingBackend/CloudWorkingBackend.sln]
.NET Command Completed - Exit Code: 1

如果直接在服务器上用dotnet restore一样的错误:

> dotnet restore
/usr/share/dotnet/sdk/3.1.101/NuGet.targets(123,5): error : Unable to load the service index for source https://api.nuget.org/v3/index.json. [/www/dotnet/xxx/xxx.csproj]
/usr/share/dotnet/sdk/3.1.101/NuGet.targets(123,5): error :   The SSL connection could not be established, see inner exception. [/www/dotnet/xxx/xxx.csproj]
/usr/share/dotnet/sdk/3.1.101/NuGet.targets(123,5): error :   The remote certificate is invalid according to the validation procedure. [/www/dotnet/xxx/xxx.csproj]

    参考博客1:https://blog.skitisu.com/solve-dotnet-restore-nuget-invalid-certificate/
根据端口关闭进程

https://stackoverflow.com/questions/11583562/how-to-kill-a-process-running-on-particular-port-in-linux

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5720426.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存