
若要使用readJSON方法需要安装Pipeline插件,很方便解析Json数据。可以读取文件或文本。具体的用法:
def response = readJSON text: "${response.content}"
println(response[0]['name'])还可以使用原生的Groovy方法,注意添加@NonCPS。(否则出现Json序列化问题)
//原生方法
import groovy.json.*
@NonCPS
def GetJson(text){
def prettyJson = JsonOutput.prettyPrint(text)
new JsonSlurperClassic().parseText(prettyJson)
}可通过凭据ID读取Jenkins系统中配置的凭据,当在流水线的执行过程中需要用到敏感信息都可以先使用凭据存储,再使用此插件以变量的方式使用。
下面演示读取一个token:
withCredentials([string(credentialsId: "xxxxx", variable: "sonarToken")]) {
println(sonarToken)
}使用checkout检出代码。
//Git
//需要传递分支、凭据、仓库地址
checkout([$class: 'GitSCM', branches: [[name: "brnachName"]],
doGenerateSubmoduleConfigurations: false,
extensions: [], submoduleCfg: [],
userRemoteConfigs: [[credentialsId: "${credentialsId}",
url: "${srcUrl}"]]])检出SVN代码
//Svn
//需要传递分支、凭据、仓库地址
checkout([$class: 'SubversionSCM', additionalCredentials: [],
filterChangelog: false, ignoreDirPropChanges: false,
locations: [[credentialsId: "${credentialsId}",
depthOption: 'infinity', ignoreExternalsOption: true,
remote: "${svnUrl}"]], workspaceUpdater: [$class: 'CheckoutUpdater']])使用此插件可以展示html报告,例如单元测试报告、自动化测试报告。
publishHTML([allowMissing: false,
alwaysLinkToLastBuild: false,
keepAll: true,
reportDir: './report/',
reportFiles: "a.html, b.html",
reportName: 'InterfaceTestReport',
reportTitles: 'HTML'])input可以以交互的方式控制流水线。例如用过交互的方式让用户选择是否要进行部署?
其参数可以是选项参数、多项选择参数、字符参数等。
def result = input message: '选择xxxxx',
ok: '提交',
parameters: [extendedChoice( description: 'xxxxx',
descriptionPropertyValue: '',
multiSelectDelimiter: ',',
name: 'failePositiveCases',
quoteValue: false,
saveJSONParameterToFile: false,
type: 'PT_CHECKBOX',
value: "1,2,3",
visibleItemCount: 99)]
println(result)使用此方法可以获取当前构建用户的信息。
wrap([$class: 'BuildUser']){
echo "full name is $BUILD_USER"
echo "user id is $BUILD_USER_ID"
echo "user email is $BUILD_USER_EMAIL"
}在调用其他系统的接口时必备的工具。
ApiUrl = "http://xxxxxx/api/project_branches/list?project=${projectName}"
Result = httpRequest authentication: 'xxxxxxxxx',
quiet: true,
contentType: 'APPLICATION_JSON' ,
url: "${ApiUrl}"往期内容推荐
PPT:基于Jenkins实现需求与代码基线关联
新生:编写Jenkinsfile常用的Groovy语法
新生:Jenkins Pipeline基础语法篇(PPT)
新生:Jenkins入门基础篇(PPT)