jenkins-pipeline dsl 常用方法

阅读量: zyh 2020-04-23 11:12:11
Categories: > Tags:

前言

这些方法都在jenkins里内置了,因此可以直接在pipeline里使用

图形化的代码配置可以访问:http:///job/prod_saas_cms/pipeline-syntax/

👙如果你找不到某个DSL,则说明需要相关插件

json 读取 readJSON

(需要额外插件) Pipeline Utility Steps

处理json数据格式化,处理json数据

def response = readJSON text: "${scanResult}"
println(scanResult)

def response = readJSON file: 'dir/input.json'

调用凭据 withCredentials

调用jenkins里存储的凭据

withCredentials([usernamePassword(credentialsId: 'registry-jenkins_harbor-rw', passwordVariable: 'password', usernameVariable: 'username')]) {
    println(username)
    println(password)
}

上面代码的意思是调用 usernamePassword 类型的凭据 registry-jenkins_harbor-rw,并将用户名存储在username变量里,密码存储在password变量里。

下载代码 checkout

下载代码

checkout([$class: 'GitSCM', branches: [[name: 'master']], extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'docker']], userRemoteConfigs: [[credentialsId: 'gitlab-jenkins-pull-token', url: 'http://git.xxx.com/cms/mapi-agent.git']]])

上面代码的意思是通过 jenkins 凭据 gitlab-jenkins-pull-token 下载 http://git.xxx.com/cms/mapi-agent.git 的 master 分支代码(不包含分支名目录)到 jenkins 工作目录下的 docker 目录中。

推送HTML报告 publishHTML

(需要额外插件) HTML Publisher

publishHTML([allowMissing: false,
	alwaysLinkToLastBuild: false,
	keepAll: true,
	reportDir: './report/',
	reportFiles: 'a.html, b.html',
	reportName: 'InterfaceTestReport',
	reportTitles: 'HTML'])

交互方式 input

交互方式

构建用户信息 BuildUser

(需要额外插件)

获取构建用户信息

wrap([$class: 'BuildUser']){
	echo "full name: ${BUILD_USER}"
	echo "user id: ${BUILD_USER_ID}"
	echo "user email: ${BUILD_USER_EMAIL}"
}

web请求 httpRequest

发起http请求

ApiUrl = "http://xxx/api/project_branches/list?project=${projectName}"
Result = httpRequest authentication: 'xxx',
						quiet: true,
						contentType: 'APPLICATION_JSON',
						url: "ApiUrl"

邮件 email

def EmailUser(userEmail,status){
 	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://192.168.1.200:8080/static/0eef74bf/images/headshot.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: userEmail

}

清理空间 cleanWS

cleanWs cleanWhenSuccess: false

表示仅当工程执行成功的时候不清理,其余工程状态都清理。

java单元测试报告 junit