enkins学习笔记之十一:优化Gitlab提交流水线

简介: enkins学习笔记之十一:优化Gitlab提交流水线

完整jenkinsfile 、sharelibrary 及jenkins配置见最后

一、gitlab push分支自动匹配

1.添加Generic Webhook插件参数,获取本次提交的分支信息

2.jenkinsfile添加判断,并切换到push的分支

3.新建分支并提交测试

二、增加build描述信息

1.新增webhook post参数

2.修改jenkinsfile

3. currentBuild.description参考

 

4.验证

三、添加gitlba commit状态

1.相关参考

https://docs.gitlab.com/ee/api/commits.html#commit-status  #查看gitlab pipeline执行状态

2.新增jenkisn sharelibrary

3.修改jenkinsfile

// 1.引入sharelibrary
def gitlab = new org.devops.gitlab()  
// 2.开始默认运行状态为running
if("${runOpts}" == "GitlabPush"){
    branchName = branch - "refs/heads/"
    currentBuild.description = "Trigger by ${userName} ${branch}"
    gitlab.ChangeCommitStatus(projectId,commitSha,"running")
}
       //3. 在post中,在对应状态下添加gitlab commit状态 ,以success为例
        success{
            script{
                println("成功")
                gitlab.ChangeCommitStatus(projectId,commitSha,"success")
            }
        }

3.创建 gitlab token

4.jenkins配置凭据,注意凭据名称和sharelibrary中保持一致

6.新增webhook post参数

7.验证配置

四、过滤新增分支特殊的push请求

有一些特殊的push请求如新建分支,这样也执行pipeline流水线显然不合适,所有需要过滤这一类特殊的push请求

https://github.com/jenkinsci/generic-webhook-trigger-plugin/tree/master/src/test/resources/org/jenkinsci/plugins/gwt/bdd/gitlab  #官方示例

1.新增webhook post参数

2.添加过滤规则

^push\s(?!0{40}).{40}\s(?!0{40}).{40}$


$object_kind $before $after

3.自行测试新建分支是否会触发push请求执行流水线

五、添加构邮件通知

1.安装邮件插件

2.配置邮件插件

3.修改jenkins管理员邮箱地址

4.编写sharelibrary

5.修改jenkinsfile

def toemail = new org.devops.toemail()
     // 调用发送右键
        success{
            script{
                println("成功")
                gitlab.ChangeCommitStatus(projectId,commitSha,"success")
                toemail.Email("流水线成功",userEmail)
            }
        }

6.设置gitlab用户邮箱地址

7.添加webhook post参数

8.验证

六、配置分支只有在commit成功时才能合并

即如果一个分支最后一次提交执行流水线的状态必须成功才能合并分支

七、相关jenkinsfile 、sharelibrary 及jenkins配置

1.jenkinsfile(ci.jenkinsfile)

#!groovy
@Library('jenkinslibrary@master') _
// 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()
// jenkins 配置参数
String srcUrl = "${env.srcUrl}"
String branchName = "${env.branchName}"
String buildType = "${env.buildType}"
String buildShell = "${env.buildShell}"
// 判断本次job是gitlab自动提交触发还是jenkins手动触发 branch是eneric Webhook Trigger 定义的参数,值$.ref是 refs/heads/ + 本次提交的分支,所以最后需要删除refs/heads/
// currentBuild是全局环境变量,流水线语法可以查看
if("${runOpts}" == "GitlabPush"){
    branchName = branch - "refs/heads/"
    currentBuild.description = "Trigger by ${userName} ${branch}"
    gitlab.ChangeCommitStatus(projectId,commitSha,"running")
}
pipeline {
    agent any
    stages {
        stage("CheckOut"){
            steps{
                script{
                    println("${branchName}")
                    tools.PrintMes("获取代码","green")
                    checkout scmGit(branches: [[name: "${branchName}"]], extensions: [], userRemoteConfigs: [[credentialsId: 'e7054d0e-275e-48ca-8188-e69da2faffb8', url: "${srcUrl}"]])
                }
            }
        }
        stage("Build"){
            steps{
                script {
                    tools.PrintMes("编译打包","blue")
                    build.Build(buildType,buildShell)
                }
            }
        }
        
    }
    
    post {
        always{
            script{
               println("一直执行")
            }
        }
        
        success{
            script{
                println("成功")
                gitlab.ChangeCommitStatus(projectId,commitSha,"success")
                toemail.Email("流水线成功",userEmail)
            }
        }
        
        aborted{
            script{
                println("取消")
                gitlab.ChangeCommitStatus(projectId,commitSha,"canceled")
            }
        }
        
        failure{
            script{
                println("失败")
                gitlab.ChangeCommitStatus(projectId,commitSha,"failed")
                toemail.Email("流水线失败",userEmail)
            }
        }
    }
}

2.sharelibrary

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="https://www.xqual.com/documentation/continuous_integration/images/jenkins-logo.png">
                <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
        
}

gitlab.groovy

package org.devops
//封装HTTP请求
def HttpReq(reqType,reqUrl,reqBody){
    def gitServer = "http://192.168.1.128/api/v4"
    withCredentials([string(credentialsId: 'gitlab-token', variable: 'gitlabToken')]) {
      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
}
//更改提交状态
def ChangeCommitStatus(projectId,commitSha,status){
    commitApi = "projects/${projectId}/statuses/${commitSha}?state=${status}"
    response = HttpReq('POST',commitApi,'')
    println(response)
    return response
}

3.jenkisn配置

流水线配置

 

 

相关文章
|
16天前
|
jenkins 持续交付 开发工具
jenkins学习笔记之一:流水线基础语法
jenkins学习笔记之一:流水线基础语法
|
16天前
|
Shell Docker 容器
GitlabCI学习笔记之一:安装Gitlab和GitLabRunner
GitlabCI学习笔记之一:安装Gitlab和GitLabRunner
|
16天前
|
jenkins Devops Java
jenkins学习笔记之十:配置Gitlab提交流水线
jenkins学习笔记之十:配置Gitlab提交流水线
|
16天前
|
安全 jenkins 持续交付
jenkins学习笔记之八:jenkins认证集成gitlab
jenkins学习笔记之八:jenkins认证集成gitlab
|
4月前
|
JavaScript jenkins 持续交付
Jenkins自动构建 CI/CD流水线学习笔记(从入门到入土,理论+示例)
Jenkins自动构建 CI/CD流水线学习笔记(从入门到入土,理论+示例)
163 0
|
4月前
|
存储 达摩院 调度
「达摩院MindOpt」优化FlowShop流水线作业排班问题
在企业在面临大量多样化的生产任务时,如何合理地安排流水线作业以提高生产效率及确保交货期成为了一个重要的问题。
「达摩院MindOpt」优化FlowShop流水线作业排班问题
|
4月前
|
存储 缓存 算法
龚大视频学习笔记:上帝视角看GPU(1)-图形流水线基础
龚大视频学习笔记:上帝视角看GPU(1)-图形流水线基础
184 0
龚大视频学习笔记:上帝视角看GPU(1)-图形流水线基础
|
存储
gitlab--运行流水线、设置 tags、设置 pipeline 状态、添加徽章
gitlab--运行流水线、设置 tags、设置 pipeline 状态、添加徽章
|
域名解析 Cloud Native jenkins
【Drone-初识篇】Drone借助GitLab构建CICD环境、以及编写 .drone.yaml 流水线
【Drone-初识篇】Drone借助GitLab构建CICD环境、以及编写 .drone.yaml 流水线
817 0
|
19天前
|
弹性计算 运维 Serverless
项目管理和持续集成系统搭建问题之云效流水线支持阿里云产品的企业用户如何解决
项目管理和持续集成系统搭建问题之云效流水线支持阿里云产品的企业用户如何解决
37 1
项目管理和持续集成系统搭建问题之云效流水线支持阿里云产品的企业用户如何解决

热门文章

最新文章