从零开始的CI/DI Jenkins集成docker+.NetCore自动化发布

忘忧 2021年11月10日 3,078次浏览

环境介绍

Centos7.6版本

开始搭建

搭建Jenkins

1.安装Jenkins

wget -O /etc/yum.repos.d/jenkins.repo \
    https://pkg.jenkins.io/redhat-stable/jenkins.repo
rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
yum upgrade
yum install epel-release java-11-openjdk-devel
yum install jenkins.noarch -y //安装
systemctl status jenkins.service//查看状态
systemctl start jenkins.service//启动

修改Jenkins配置文件
vim /etc/sysconfig/jenkins 修改JENKINS_USER=“jenkins” 修改为JENKINS_USER=“root”

2.访问Jenkins
http://101.35.80.206:8080/ http://IP:8080/
等待Jenkins初始化
image.png
命令行输入

cat /var/lib/jenkins/secrets/initialAdminPassword

将会得到初始化密码
输入input框即可初始化Jenkins
image.png
默认安装即可
image.png
创建一个默认用户
image.png
安装完成开始使用Jenkins吧!

开始使用Jenkins

配置证书

配置gitee账号

点击Manage Credentials
image.png
点击Jenkins
image.png
点击倒三角并点击Add credentials
image.png
选择Username with password
image.png
输入账号密码以及Id user pass Id
image.png

配置Jenkins插件

点击Manage Jenkins然后再点击Manage Plugins
image.png
image.png
这里安装以下插件

  1. SSH Pipeline Steps
  2. Gitee
  3. Gogs

安装Docker

这里推荐使用阿里云的自动化安装脚本

Docker CE 镜像源站

curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun //安装
systemctl status docker //查看状态
//如果没有启动请使用
systemctl start docker //启动服务

groupadd docker
usermod -aG docker ${USER}
systemctl restart docker
systemctl restart jenkins

安装.Net5

我本机环境是Centos7.6 其他Linux系统请参考
https://docs.microsoft.com/en-us/dotnet/core/install/linux?WT.mc_id=dotnet-35129-website

rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
yum install dotnet-sdk-5.0

nuget证书问题

dotnet dev-certs
cp /etc/pki/tls/cert.pem  /usr/local/openssl/cert.pem

创建一个项目

创建一个流水线项目
image.png
初始化项目并填上git地址
image.png
选择分支
image.png
注明Jenkinsfile文件地址
image.png
保存配置

配置163 STMP邮件服务器信息

进入Jenkins系统配置
image.png
找到E-mail Notification
image.png
配置STMP服务器,账号,授权码
image.png
image.png
这些东西在163邮箱中获取
https://email.163.com/

配置JenkinsFile文件

创建Jenkinsfile文件
注意! 如果系统上有多个dotnet 需要使用绝对路径 比如 /usr/share/dotnet/dotnet 如果系统上只装了一个dotnet的话使用dotnet
image.png

def remote = [:]
remote.name = 'TService'
remote.host =  ''//服务器地址
remote.user = 'root'
remote.password = ''服务器密码
remote.allowAnyHosts = true
pipeline {
    agent any

    environment {
        RELEASE_PATH = "${WORKSPACE}/releases"
        COMMIT_ID = sh(returnStdout: true, script: 'git rev-parse HEAD')
        DOCKER_USERNAME = "" //docker hub账号
        DOCKER_PASSWORD = "" //docker hub密码
    }
    stages {
        stage('Build') {
            steps {
                echo 'Building...'
                sh "/usr/share/dotnet/dotnet build"
            }
        }
        stage('Test') {
            steps {
                echo 'Testing..'
            }
        }
        stage('Publish') {
            steps {
                echo 'Publish....'
                sh "/usr/share/dotnet/dotnet publish -c=Release -o=${RELEASE_PATH}/DevOps WebApplication1/WebApplication1.csproj"
                echo RELEASE_PATH
                writeFile(file: "${RELEASE_PATH}/DevOps/commit-${COMMIT_ID}.txt", text: "")
            }
        }
        stage('Docker Build') {
            steps {
                echo 'Docker Build...'
                sh "docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD}"
                sh "cp WebApplication1/Dockerfile ${RELEASE_PATH}/DevOps"
                sh "cd ${RELEASE_PATH}/DevOps && docker build -t ${DOCKER_USERNAME}/dev_ops:latest ."
                sh "docker push ${DOCKER_USERNAME}/dev_ops:latest"
            }
        }
        stage('Service Docker Run'){
            steps {
                script{
                    try {
                                echo "Service Docker Run...."
                                sshCommand remote: remote, command: "docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD}"
                                sshCommand remote: remote, command: "docker pull ${DOCKER_USERNAME}/dev_ops:latest"
                                sshCommand remote: remote, command: "docker stop dev_ops"
                                sshCommand remote: remote, command: "docker rm dev_ops"
                                sshCommand remote: remote, command: "docker run --name dev_ops -d -p 5003:80 ${DOCKER_USERNAME}/dev_ops"
                                sshCommand remote: remote, command: "docker logout"
                            } 
                            catch (Exception  e){
                                echo "Service Docker Run...."
                                sshCommand remote: remote, command: "docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD}"
                                sshCommand remote: remote, command: "docker pull ${DOCKER_USERNAME}/dev_ops:latest"
                                sshCommand remote: remote, command: "docker run --name dev_ops -d -p 5003:80 ${DOCKER_USERNAME}/dev_ops"
                                sshCommand remote: remote, command: "docker logout"
                            }
                }
         
            }
         
        }
        stage('Delete Publish Release') {
            steps {
                echo "Delete Files..."
                sh "rm -rf ${RELEASE_PATH}"
            }
        }
    }
    post {
       success {
             mail bcc: '', body: "如题", cc: '', from: ''//发送者邮箱, replyTo: '', subject: "发布成功", to: ''//接收者邮箱
       }

       failure {
              mail bcc: '', body: "如题1", cc: '', from: ''//发送者邮箱, replyTo: '', subject: "发布失败", to: ''//接收者邮箱
       }
   }
}

配置DockFile

DockFile文件地址
image.png
DockerFile文件内容

FROM mcr.microsoft.com/dotnet/aspnet:5.0
COPY . /app
WORKDIR /app
EXPOSE 80
EXPOSE 443
ENTRYPOINT ["dotnet", "WebApplication1.dll"]

配置完成后将代码Git push到仓库中

然后再Jenkins中开始构建发布
image.png
发布成功
image.png
检查是否收到Jenkins邮件
image.png