多文本输入框

  1. def userInput = input(
  2. id: 'userInput', message: 'Let\'s promote?', parameters: [
  3. [$class: 'TextParameterDefinition', defaultValue: 'uat', description: 'Environment', name: 'env'],
  4. [$class: 'TextParameterDefinition', defaultValue: 'uat1', description: 'Target', name: 'target']
  5. ])
  6. echo ("Env: "+userInput['env'])
  7. echo ("Target: "+userInput['target'])

单文本输入框

  1. def userInput = input(
  2. id: 'userInput', message: 'Let\'s promote?', parameters: [
  3. [$class: 'TextParameterDefinition', defaultValue: 'uat', description: 'Environment', name: 'env']
  4. ])
  5. echo ("Env: "+userInput)

下拉选项输入框

  1. def userInput = input(id: 'userInput', message: 'some message', parameters: [
  2. [
  3. $class: 'ChoiceParameterDefinition',
  4. choices: "Dev\nQA\nProd",
  5. name: 'Env'
  6. ]
  7. ])
  8. echo "This is a deploy step to ${userInput}"

pipeline 脚本下的参数输入

  1. properties([
  2. parameters([
  3. choice( choices: ['ONE', 'TWO'], name: 'PARAMETER_01'),
  4. booleanParam(defaultValue: true, description: '', name: 'BOOLEAN_VALUE'),
  5. text(defaultValue: '''this is a multi-line string parameter example ''', name: 'MULTI-LINE-STRING'),
  6. string(defaultValue: 'scriptcrunch', name: 'STRING-PARAMETER', trim: true)
  7. ])
  8. ])
  9. if(Boolean.valueOf(BOOLEAN_VALUE)){}

获取Jenkins的构建号

用Groovy Pipeline 构建时:

  1. node {
  2. stage('test advance script') {
  3. //三种获取版本方法都有效
  4. echo "current build number : ${BUILD_NUMBER}"
  5. echo "current build number : ${env.BUILD_NUMBER}"
  6. echo "current build number: ${currentBuild.number}"
  7. //获取前一个构建号
  8. echo "previous build number: ${currentBuild.previousBuild.getNumber()}"
  9. }
  10. }

钉钉Jenkins机器人

钉钉Jenkins机器人地址 https://jenkinsci.github.io/dingtalk-plugin/
image.png

  1. dingtalk (
  2. robot: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
  3. type: 'MARKDOWN',
  4. title: '你有新的消息,请注意查收',
  5. text: [
  6. "# [${env.JOB_NAME}](${env.JOB_URL})",
  7. '',
  8. "- 任务: [${env.BUILD_NUMBER}](${env.BUILD_URL})",
  9. "- 分支: ${build_tag}",
  10. "- 状态: <font color=red>失败</font>",
  11. "- 版本: ${BUILD_VERSION}"
  12. ],
  13. at: []
  14. )

获取提交日志

  1. #!groovy
  2. pipeline {
  3. agent any
  4. stages {
  5. stage('拉代码') {
  6. steps {
  7. //这里就不写了,用pipeline syntax生成一份checkout命令
  8. }
  9. }
  10. stage('输出日志') {
  11. steps {
  12. script{
  13. //调用方法得到日志 并 输出
  14. def changeString = getChangeString()
  15. echo "$changeString"
  16. }
  17. }
  18. }
  19. }
  20. }
  21. @NonCPS
  22. def getChangeString() {
  23. MAX_MSG_LEN = 100
  24. def changeString = ""
  25. echo "Gathering SCM changes"
  26. def changeLogSets = currentBuild.changeSets
  27. for (int i = 0; i < changeLogSets.size(); i++) {
  28. def entries = changeLogSets[i].items
  29. for (int j = 0; j < entries.length; j++) {
  30. def entry = entries[j]
  31. truncated_msg = entry.msg.take(MAX_MSG_LEN)
  32. changeString += " - ${truncated_msg} [${entry.author}]\n"
  33. }
  34. }
  35. if (!changeString) {
  36. changeString = " - No new changes"
  37. }
  38. return changeString
  39. }

获取提交信息,并且发邮件

  1. @NonCPS
  2. def getChangeString() {
  3. MAX_MSG_LEN = 100
  4. def changeString = ""
  5. def sendMail="zhangsan@demo.com,lisi@demo.com,"
  6. echo "Gathering SCM changes"
  7. def changeLogSets = currentBuild.changeSets
  8. for (int i = 0; i < changeLogSets.size(); i++) {
  9. def entries = changeLogSets[i].items
  10. for (int j = 0; j < entries.length; j++) {
  11. def entry = entries[j]
  12. truncated_msg = entry.msg.take(MAX_MSG_LEN)
  13. sendMail = sendMail+"${entry.author}@demo.com,"
  14. changeString += "--${truncated_msg} [${entry.author}]\n"
  15. }
  16. }
  17. if (!changeString) {
  18. changeString = " - 无"
  19. }
  20. return [sendMail,changeString]
  21. }
  22. def sendEmail(status) {
  23. def sendObject = getChangeString()
  24. echo "${sendObject[0]}"
  25. echo "${sendObject[1]}"
  26. emailext attachLog: true, body: "更新记录:\n " + "${sendObject[1]}" + "\n\n 构建日志: $BUILD_URL/console" + "\n",
  27. subject: "${JOB_NAME} (${BUILD_NUMBER})-"+status,to: "${sendObject[0]}"
  28. }

修改构建展示名称和描述

  1. currentBuild.displayName = "${appVersion}"
  2. currentBuild.description = "${appVersion}"

日期时间

  1. def timeStamp = Calendar.getInstance().getTime().format('yyyy-MM-dd HH:mm:ss',TimeZone.getTimeZone('Asia/Shanghai'))
  2. def time=new Date().format('yyyy-MM-dd HH:mm:ss')