流水线开发工具

本模块主要了解在流水线中常用的方法😀

目录


片段生成器

使用片段生成器可以根据个人需要生成方法,有些方法来源于插件,则需要先安装相关的插件才能使用哦。

images

填写好参数后,点击生成,然后复制粘贴到Jenkinsfile images

声明式语法生成器

images

全局变量参考

images

env变量

  1. BUILD_NUMBER //构建号
  2. BUILD_ID //构建号
  3. BUILD_DISPLAY_NAME //构建显示名称
  4. JOB_NAME //项目名称
  5. EXECUTOR_NUMBER //执行器数量
  6. NODE_NAME //构建节点名称
  7. WORKSPACE //工作目录
  8. JENKINS_HOME //Jenkins home
  9. JENKINS_URL //Jenkins地址
  10. BUILD_URL //构建地址
  11. JOB_URL //项目地址

currentbuild变量

  1. result currentResult //构建结果
  2. displayName //构建名称 #111
  3. description //构建描述
  4. duration //持续时间

常用的方法

json数据处理

  1. def response = readJSON text: "${scanResult}"
  2. println(scanResult)
  3. //原生方法
  4. import groovy.json.*
  5. @NonCPS
  6. def GetJson(text){
  7. def prettyJson = JsonOutput.prettyPrint(text)
  8. new JsonSlurperClassic().parseText(prettyJson)
  9. }

使用凭据

  1. withCredentials([string(credentialsId: "xxxxx", variable: "sonarToken")]) {
  2. println(sonarToken)
  3. }

下载代码

  1. //Git
  2. checkout([$class: 'GitSCM', branches: [[name: "brnachName"]],
  3. doGenerateSubmoduleConfigurations: false,
  4. extensions: [], submoduleCfg: [],
  5. userRemoteConfigs: [[credentialsId: "${credentialsId}",
  6. url: "${srcUrl}"]]])
  7. //Svn
  8. checkout([$class: 'SubversionSCM', additionalCredentials: [],
  9. filterChangelog: false, ignoreDirPropChanges: false,
  10. locations: [[credentialsId: "${credentialsId}",
  11. depthOption: 'infinity', ignoreExternalsOption: true,
  12. remote: "${svnUrl}"]], workspaceUpdater: [$class: 'CheckoutUpdater']]
  13. )

展示报告

  1. publishHTML([allowMissing: false,
  2. alwaysLinkToLastBuild: false,
  3. keepAll: true,
  4. reportDir: './report/',
  5. reportFiles: "a.html, b.html",
  6. reportName: 'InterfaceTestReport',
  7. reportTitles: 'HTML'])

交互输入

  1. def result = input message: '选择xxxxx',
  2. ok: '提交',
  3. parameters: [extendedChoice( description: 'xxxxx',
  4. descriptionPropertyValue: '',
  5. multiSelectDelimiter: ',',
  6. name: 'failePositiveCases',
  7. quoteValue: false,
  8. saveJSONParameterToFile: false,
  9. type: 'PT_CHECKBOX',
  10. value: "1,2,3",
  11. visibleItemCount: 99)]
  12. println(result)

构建用户

  1. wrap([$class: 'BuildUser']){
  2. echo "full name is $BUILD_USER"
  3. echo "user id is $BUILD_USER_ID"
  4. echo "user email is $BUILD_USER_EMAIL"
  5. }

制品上传

  1. rtUpload (
  2. serverId: "artifactory01",
  3. spec:
  4. """{
  5. "files": [
  6. {
  7. "pattern": "report/a.html",
  8. "target": "${repoName}/a/b/v"
  9. }
  10. ]
  11. }"""
  12. )

发送http请求

  1. ApiUrl = "http://xxxxxx/api/project_branches/list?project=${projectName}"
  2. Result = httpRequest authentication: 'xxxxxxxxx',
  3. quiet: true,
  4. contentType: 'APPLICATION_JSON' ,
  5. url: "${ApiUrl}"