CI/DI Jenkins集成docker+.NetCore自动化发布-进阶教程

目的是什么

请阅读该篇文章后再观看
从零开始的CI/DI Jenkins集成docker+.NetCore自动化发布

Webhook提交发布

Jeknins 安装插件 GiteeGogs
image.png
image.png

开启webhook

image.png

配置项目秘钥webhook

下拉找到Secret Token for Gitee WebHook
image.png
随机秘钥
image.png

配置gitee项目

image.png
image.png
image.png
image.png
添加一个WebHooks
地址为 https://jk.wangyou233.wang/gitee-project/test
密码为刚刚设置的秘钥
15b71aee80e0a7e58f3effd3d818b5f2
image.png
点击添加
image.png
然后我们测试一下 打开git 随意修改一个文件提交并打开Jenkins项目
image.png
image.png
当我们提交后gitee会发送一个请求给到Jenkins,触发Jenkins CI/CD的功能
image.png
发布成功
image.png

更丰富的邮件提醒

安装Jenkins插件Email Extension
image.png
配置插件
image.png
找到Extended E-mail Notification
image.png
配置信息
image.png
邮件内容

<!DOCTYPE html>  
<html>  
<head>  
<meta charset="UTF-8">  
<title>${ENV, var="JOB_NAME"}-第${BUILD_NUMBER}次构建日志</title>  
</head>  
  
<body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4"  
    offset="0">  
    <table width="95%" cellpadding="0" cellspacing="0"  style="font-size: 11pt; font-family: Microsoft YaHei, Tahoma, Arial, Helvetica">  
        <tr>  
            <td>各位同事,大家好,以下为 ${PROJECT_NAME } 构建任务信息</td>  
        </tr>  
        <tr>  
            <td><br />  
            <b style="font-weight:bold; color:#66cc00">构建信息</b>  
            <hr size="2" width="100%" align="center" /></td>  
        </tr>  
        <tr>  
            <td>  
                <ul>  
                    <li>任务名称 : ${PROJECT_NAME}</li>  
                    <li>构建编号 : 第${BUILD_NUMBER}次构建</li>  
                    <li>触发原因: ${CAUSE}</li>  
                    <li>构建分支: ${GIT_BRANCH}</li>
                    <li>构建状态: <span style="font-weight:bold; color:#FF0000">${BUILD_STATUS}</span></li>  
                    <li>构建日志: <a href="${BUILD_URL}console">${BUILD_URL}console</a></li>  
                    <li>构建  Url : <a href="${BUILD_URL}">${BUILD_URL}</a></li>  
                    <li>工作目录 : <a href="${PROJECT_URL}ws">${PROJECT_URL}ws</a></li>  
                    <li>项目  Url : <a href="${PROJECT_URL}">${PROJECT_URL}</a></li>  
                </ul>  
            </td>  
        </tr>  
    </table>  
</body>  
</html>

修改JenkinFile

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:${BUILD_NUMBER} ."
                sh "docker push ${DOCKER_USERNAME}/dev_ops:${BUILD_NUMBER}"
            }
        }
        stage('Service Docker Run'){
            steps {
                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:${BUILD_NUMBER}"
                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"
            }
         
        }
        stage('Delete Publish Release') {
            steps {
                echo "Delete Files..."
                sh "rm -rf ${RELEASE_PATH}"
            }
        }
    }
    post {
      success{
                emailext (
                subject: '${DEFAULT_SUBJECT}',
                body: '${DEFAULT_CONTENT}',
                to: "915589928@qq.com")
        }
        failure {
            emailext (
                subject: '${DEFAULT_SUBJECT}',
                body: '${DEFAULT_CONTENT}',
                to: "915589928@qq.com")
        }
   }
}

主要修改的地方

post {
      success{
                emailext (
                subject: '${DEFAULT_SUBJECT}',
                body: '${DEFAULT_CONTENT}',
                to: "915589928@qq.com")
        }
        failure {
            emailext (
                subject: '${DEFAULT_SUBJECT}',
                body: '${DEFAULT_CONTENT}',
                to: "915589928@qq.com")
        }
   }


将laster变更为Jenkins编译的版本号
cd ${RELEASE_PATH}/DevOps && docker build -t ${DOCKER_USERNAME}/dev_ops:${BUILD_NUMBER} .

当build完成后会收到该邮件
image.png

条件提交发布

设置项目为
image.png
commit信息为[ci-build]时编译发布项目
image.png
image.png
当commit消息不是[ci-build]的话则不会构建

# CI/DI 

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×