DevOps-jenkins-流水线优化

DevOps-jenkins-流水线优化_第1张图片

GITLAB-SSO用户认证

安装GitLab Authentication插件

为了避免权限上的报错先设置权限
DevOps-jenkins-流水线优化_第2张图片
gitlab创建app
DevOps-jenkins-流水线优化_第3张图片
jenkins设置
DevOps-jenkins-流水线优化_第4张图片

验证

这时候也可以在 gitlab 上注册账户直接登入到 jenkins
DevOps-jenkins-流水线优化_第5张图片

项目准备和流水线调试

准备代码在github中下载地址: https:/github.com/zeyangli/simple-java-maven-app.git
DevOps-jenkins-流水线优化_第6张图片

jenkinsfile

#!groovy
@Library('jenkinslib@master') _ 

//func from sharelibrary
def build = new org.devops.build()
def deploy = new org.devops.deploy()
def tools = new org.devops.tools()           //引用共享库设置的函数

//env
String buildType = "${env.buildType}"
String buildShell = "${env.buildShell}"
String deployHosts = "${env.deployHosts}"
//String deployHosts = "server1.server2"
String srcUrl = "${env.srcUrl}"            //设置变量
String branchName = "${branchName}"

//pipeline
pipeline {
    agent { node { label "master"}}

    stages{
        stage("CheckOut"){
            steps{
                script{
                	//使用引入的字体修改
                    tools.PrintMes("获取代码","green")
                    //用jenkins 流水线语法生成
                    checkout([$class: 'GitSCM', branches: [[name: "${branchName}"]], extensions: [], userRemoteConfigs: [[credentialsId: '9f0c031f-924e-4a84-a9ea-4957d27a9d54', url: "${srcUrl}"]]])
                }
            }
        }
        stage("Build"){
            steps{
                script{
                    tools.PrintMes("执行打包","green")
                    build.Build(buildType,buildShell)
                    deploy.AnsibleDeploy("${deployHosts}","-m  ping")
                }
            }
        }
    }
}

已生成jar包
DevOps-jenkins-流水线优化_第7张图片

WebHook集成

Jenkins配置-安装Generic Webhook Trigger和GitLab插件

DevOps-jenkins-流水线优化_第8张图片

设置webhook

Jenkins配置
DevOps-jenkins-流水线优化_第9张图片

DevOps-jenkins-流水线优化_第10张图片

gitlab 进行 webhook设置

DevOps-jenkins-流水线优化_第11张图片

DevOps-jenkins-流水线优化_第12张图片
DevOps-jenkins-流水线优化_第13张图片
Hook executed successfully: HTTP 200

提交流水线优化一获取提交分支名称

目的:通过Jenkins 参数、shell、git实现单job自动切换不同的分支
DevOps-jenkins-流水线优化_第14张图片

增加获取hook参数 分支名称
DevOps-jenkins-流水线优化_第15张图片
jenkinsfile

#!groovy
@Library('jenkinslib@master') _ 
//func from sharelibrary
def build = new org.devops.build()
def deploy = new org.devops.deploy()
def tools = new org.devops.tools()
def runOpts
//env
String buildType = "${env.buildType}"
String buildShell = "${env.buildShell}"
String deployHosts = "${env.deployHosts}"
//String deployHosts = "server1.server2"
String srcUrl = "${env.srcUrl}"
String branchName = "${branchName}"
//pipeline
pipeline {
    agent { node { label "master"}}
    stages{
        stage("CheckOut"){
            steps{
                script{
                	//判断如果获取到gitpush的信息,重新定义分支名
                    if ("${runOpts}" == "GitlabPush"){
                        branchName = branch - "refs/heads/"
                    }
                    println("${branchName}")
                    tools.PrintMes("获取代码","green")
                    checkout([$class: 'GitSCM', branches: [[name: "${branchName}"]], extensions: [], userRemoteConfigs: [[credentialsId: '9f0c031f-924e-4a84-a9ea-4957d27a9d54', url: "${srcUrl}"]]])
                }
            }
        }
    }
}

这样设置,就可以实现不同分支提交代码,jenkins获取到gitlab推送的json信息,打印对应的分支代码

提交流水线优化一增加构建描述信息

目的: 区分信息
DevOps-jenkins-流水线优化_第16张图片

流水线添加描述 流水线语法—全局变量参考—currentBuild—description
DevOps-jenkins-流水线优化_第17张图片
jenkins设置
DevOps-jenkins-流水线优化_第18张图片
变量名称: currentBuild.description

jenkinsfile
在pipeline外增加 currentBuild.description 描述信息

if ("${runOpts}" == "GitlabPush"){
    branchName = branch - "refs/heads/"
    println("${branchName}")
    //
    currentBuild.description = "构建用户:${userName} 分支:${branchName}"
}

//pipeline

检查
DevOps-jenkins-流水线优化_第19张图片

提交流水线优化一变更commit状态

目标:在gitlab中可以看到jenkins的运行状态

DevOps-jenkins-流水线优化_第20张图片
DevOps-jenkins-流水线优化_第21张图片

方案:通过POST gitlabAPI接口,jenkins构建结束后返回相应的状态到gitlab

官方文档地址: https://docs.gitlab.com/ee/api/commits.html
DevOps-jenkins-流水线优化_第22张图片

curl --request POST --header "PRIVATE-TOKEN: " "https://gitlab.example.com/api/v4/projects/17/statuses/18f3e63d05582537db6d183d9d557be09e1f90c8?state=success"
# 编辑:更改提交状态,然后HTTP发送请求
# 解释:在gitlab上创建了一个token,然后存在jenkins中,把凭据的数值赋值给这个变量,最后请求的时候加到head里面!

引用共享仓库(gitlab.groovy)

package  org.devops
 
//(1)封装HTTP请求
def HttpReq(reqType,reqUrl,reqBody){
    //1)  Gitlab Api地址
    def gitServer = "http://121.40.207.133:8010/api/v4"
    //2)  gitlab上创建一个凭据,存在给jenkins上,凭据名字(gitlab-token),赋值给gitlabToken变量
    withCredentials([string(credentialsId: 'gitlab-token', variable: 'gitlabToken')]) {
      //  发送HTTP请求-->请求类型、等等信息-->请求的时候加到Header里面
      result = httpRequest customHeaders: [[maskValue: true, name: 'PRIVATE-TOKEN', value: "${gitlabToken}"]],
                httpMode: reqType,
                contentType: "APPLICATION_JSON",
                consoleLogResponseBody: true,
                ignoreSslErrors: true,
                requestBody: reqBody,
                url: "${gitServer}/${reqUrl}"
                //quiet: true
    }
    return result
}

//(2)更改提交状态-->利用HTTP的请求
 
def ChangeCommitStatus(projectId,commitSha,status){
    //参照:curl --request POST --header "PRIVATE-TOKEN: "
    //"https://gitlab.example.com/api/v4/projects/17/statuses/18f3e63d05582537db6d183d9d557be09e1f90c8?state=success"
    //1)  定义CommitApi
    commitApi = "projects/${projectId}/statuses/${commitSha}?state=${status}"
    //2)  发送HTTP的POST请求
    //response = HttpReq("POST",commitApi,'').content
    response = HttpReq("POST",commitApi,'')
    println("${response}")
}

Jenkinsfile

#!groovy
@Library('jenkinslib@master') _ 

//func from sharelibrary
def build = new org.devops.build()
def deploy = new org.devops.deploy()
def tools = new org.devops.tools()
def gitlab = new org.devops.gitlab()

//env
String buildType = "${env.buildType}"
String buildShell = "${env.buildShell}"
String deployHosts = "${env.deployHosts}"
String srcUrl = "${env.srcUrl}"
String branchName = "${branchName}"

if ("${runOpts}" == "GitlabPush"){
    branchName = branch - "refs/heads/"
    println("${branchName}")
    currentBuild.description = "构建用户:${userName} 分支:${branchName}"
    //函数格式为ChangeCommitStatus(projectId,commitSha.status) 
    gitlab.ChangeCommitStatus(projectId,commitSha,"running")
}

//pipeline
pipeline {
    agent { node { label "master"}}

    stages{
        stage("CheckOut"){
            steps{
                script{


                    tools.PrintMes("获取代码","green")
                    checkout([$class: 'GitSCM', branches: [[name: "${branchName}"]], extensions: [], userRemoteConfigs: [[credentialsId: '9f0c031f-924e-4a84-a9ea-4957d27a9d54', url: "${srcUrl}"]]])
                }
            }
        }
        stage("Build"){
            steps{
                script{
                    tools.PrintMes("执行打包","green")
                    build.Build(buildType,buildShell)
                    deploy.AnsibleDeploy("${deployHosts}","-m  ping")
                }
            }
        }
    }
    post {
        always {
            script{
                println("always")
            }
        }
        aborted {
            script{
                println("aborted")
                gitlab.ChangeCommitStatus(projectId,commitSha,"canceled")
            }
        }
        success {
            script{
                println("success")
                gitlab.ChangeCommitStatus(projectId,commitSha,"success")
            }
        }
        failure {
            script{
                println("failure")
                gitlab.ChangeCommitStatus(projectId,commitSha,"failure")
            }
        }
    }
}

根据Jenkinsfile 添加变量

DevOps-jenkins-流水线优化_第23张图片

测试

Contributing variables:

    branch = refs/heads/master
    commitSha = a8797285168116c5954275e6264c1bfc1966f595
    projectId = 7
    runOpts = GitlabPush
    runOpts_0 = GitlabPush
    userName = bianmc
    ...

提交流水线优化一过滤特殊push请求(创建branch)

目的:gitlab创建支线后会自动进行push,随即触发了jenkins构建,通过gitlab请求变量过滤这个操作
DevOps-jenkins-流水线优化_第24张图片
Generic Webhook Trigger 触发构建插件代码中有过滤的正则
DevOps-jenkins-流水线优化_第25张图片
根据给定的过滤器配置为文本和过滤器配置为表达式来配置jenkins

    Given the following generic variables are configured:
      | variable        | expression               | expressionType  | defaultValue | regexpFilter  |
      | object_kind     | $.object_kind            | JSONPath        |              |               |
      | before          | $.before                 | JSONPath        |              |               |
      | after           | $.after                  | JSONPath        |              |               |
      | ref             | $.ref                    | JSONPath        |              |               |
      | git_ssh_url     | $.repository.git_ssh_url | JSONPath        |              |               |

    Given filter is configured with text: $object_kind $before $after
    Given filter is configured with expression: ^push\s(?!0{40}).{40}\s(?!0{40}).{40}$

DevOps-jenkins-流水线优化_第26张图片
DevOps-jenkins-流水线优化_第27张图片

提交流水线优化一构建失败邮件通知

jenkins 插件 Email Extension

DevOps-jenkins-流水线优化_第28张图片
DevOps-jenkins-流水线优化_第29张图片

gitlab设置
DevOps-jenkins-流水线优化_第30张图片
创建邮件共享库 toemail.groovy

package org.devops

//定义邮件内容
def Email(status,emailUser){
    emailext body: """
            <!DOCTYPE html> 
            <html> 
            <head> 
            <meta charset="UTF-8"> 
            </head> 
            <body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4" offset="0"> 
                <img src="http://****:28080/static/45c24286/images/headshot.jpeg">
                <table width="95%" cellpadding="0" cellspacing="0" style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">   
                    <tr> 
                        <td><br /> 
                            <b><font color="#0B610B">构建信息</font></b> 
                        </td> 
                    </tr> 
                    <tr> 
                        <td> 
                            <ul> 
                                <li>项目名称:${JOB_NAME}</li>         
                                <li>构建编号:${BUILD_ID}</li> 
                                <li>构建状态: ${status} </li>                         
                                <li>项目地址:<a href="${BUILD_URL}">${BUILD_URL}</a></li>    
                                <li>构建日志:<a href="${BUILD_URL}console">${BUILD_URL}console</a></li> 
                            </ul> 
                        </td> 
                    </tr> 
                    <tr>  
                </table> 
            </body> 
            </html>  """,
            subject: "Jenkins-${JOB_NAME}项目构建信息 ",
            to: emailUser
        
}

jenkinsfile

#!groovy

@Library('jenkinslib@master') _ 

//func from sharelibrary
def build = new org.devops.build()
def deploy = new org.devops.deploy()
def tools = new org.devops.tools()
def gitlab = new org.devops.gitlab()
def toemail = new org.devops.toemail()

//env
String buildType = "${env.buildType}"
String buildShell = "${env.buildShell}"
String deployHosts = "${env.deployHosts}"
//String deployHosts = "server1.server2"
String srcUrl = "${env.srcUrl}"
String branchName = "${branchName}"

if ("${runOpts}" == "GitlabPush"){
    branchName = branch - "refs/heads/"
    println("${branchName}")
    currentBuild.description = "构建用户:${userName} 分支:${branchName}"
    //函数格式为ChangeCommitStatus(projectId,commitSha.status) 
    gitlab.ChangeCommitStatus(projectId,commitSha,"running")
    env.runOpts = "GitlabPush"    
} else {
   userEmail = "[email protected]"
}

//pipeline
pipeline {
    agent { node { label "master"}}

    stages{
        stage("CheckOut"){
            steps{
                script{


                    tools.PrintMes("获取代码","green")
                    checkout([$class: 'GitSCM', branches: [[name: "${branchName}"]], extensions: [], userRemoteConfigs: [[credentialsId: '9f0c031f-924e-4a84-a9ea-4957d27a9d54', url: "${srcUrl}"]]])
                }
            }
        }
        stage("Build"){
            steps{
                script{
                    tools.PrintMes("执行打包","green")
                    build.Build(buildType,buildShell)
                    deploy.AnsibleDeploy("${deployHosts}","-m  ping")
                }
            }
        }
    }
    post {
        always {
            script{
                println("always")
            }
        }
        aborted {
            script{
                println("aborted")
                gitlab.ChangeCommitStatus(projectId,commitSha,"canceled")
                toemail.Email("流水线被取消了!!!",userEmail)
            }
        }
        success {
            script{
                println("success")
                gitlab.ChangeCommitStatus(projectId,commitSha,"success")
                toemail.Email("流水线成功",userEmail)
            }
        }
        failure {
            script{
                println("failure")
                gitlab.ChangeCommitStatus(projectId,commitSha,"failure")
                //邮件提醒
                toemail.Email("流水线失败了!!!",userEmail)
            }
        }
    }
}

测试
DevOps-jenkins-流水线优化_第31张图片

流水线优化-gitlab合并策略优化

目的:分支合并后不再出发jenkins构建(如果最新流水线未成功或仍在运行,则无法合并合并请求。)
DevOps-jenkins-流水线优化_第32张图片

你可能感兴趣的:(jenkins,devops,运维)