多文本输入框
def userInput = input(
id: 'userInput', message: 'Let\'s promote?', parameters: [
[$class: 'TextParameterDefinition', defaultValue: 'uat', description: 'Environment', name: 'env'],
[$class: 'TextParameterDefinition', defaultValue: 'uat1', description: 'Target', name: 'target']
])
echo ("Env: "+userInput['env'])
echo ("Target: "+userInput['target'])
单文本输入框
def userInput = input(
id: 'userInput', message: 'Let\'s promote?', parameters: [
[$class: 'TextParameterDefinition', defaultValue: 'uat', description: 'Environment', name: 'env']
])
echo ("Env: "+userInput)
下拉选项输入框
def userInput = input(id: 'userInput', message: 'some message', parameters: [
[
$class: 'ChoiceParameterDefinition',
choices: "Dev\nQA\nProd",
name: 'Env'
]
])
echo "This is a deploy step to ${userInput}"
pipeline 脚本下的参数输入
properties([
parameters([
choice( choices: ['ONE', 'TWO'], name: 'PARAMETER_01'),
booleanParam(defaultValue: true, description: '', name: 'BOOLEAN_VALUE'),
text(defaultValue: '''this is a multi-line string parameter example ''', name: 'MULTI-LINE-STRING'),
string(defaultValue: 'scriptcrunch', name: 'STRING-PARAMETER', trim: true)
])
])
if(Boolean.valueOf(BOOLEAN_VALUE)){}
获取Jenkins的构建号
用Groovy Pipeline 构建时:
node {
stage('test advance script') {
//三种获取版本方法都有效
echo "current build number : ${BUILD_NUMBER}"
echo "current build number : ${env.BUILD_NUMBER}"
echo "current build number: ${currentBuild.number}"
//获取前一个构建号
echo "previous build number: ${currentBuild.previousBuild.getNumber()}"
}
}
钉钉Jenkins机器人
钉钉Jenkins机器人地址 https://jenkinsci.github.io/dingtalk-plugin/
dingtalk (
robot: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
type: 'MARKDOWN',
title: '你有新的消息,请注意查收',
text: [
"# [${env.JOB_NAME}](${env.JOB_URL})",
'',
"- 任务: [${env.BUILD_NUMBER}](${env.BUILD_URL})",
"- 分支: ${build_tag}",
"- 状态: <font color=red>失败</font>",
"- 版本: ${BUILD_VERSION}"
],
at: []
)
获取提交日志
#!groovy
pipeline {
agent any
stages {
stage('拉代码') {
steps {
//这里就不写了,用pipeline syntax生成一份checkout命令
}
}
stage('输出日志') {
steps {
script{
//调用方法得到日志 并 输出
def changeString = getChangeString()
echo "$changeString"
}
}
}
}
}
@NonCPS
def getChangeString() {
MAX_MSG_LEN = 100
def changeString = ""
echo "Gathering SCM changes"
def changeLogSets = currentBuild.changeSets
for (int i = 0; i < changeLogSets.size(); i++) {
def entries = changeLogSets[i].items
for (int j = 0; j < entries.length; j++) {
def entry = entries[j]
truncated_msg = entry.msg.take(MAX_MSG_LEN)
changeString += " - ${truncated_msg} [${entry.author}]\n"
}
}
if (!changeString) {
changeString = " - No new changes"
}
return changeString
}
获取提交信息,并且发邮件
@NonCPS
def getChangeString() {
MAX_MSG_LEN = 100
def changeString = ""
def sendMail="zhangsan@demo.com,lisi@demo.com,"
echo "Gathering SCM changes"
def changeLogSets = currentBuild.changeSets
for (int i = 0; i < changeLogSets.size(); i++) {
def entries = changeLogSets[i].items
for (int j = 0; j < entries.length; j++) {
def entry = entries[j]
truncated_msg = entry.msg.take(MAX_MSG_LEN)
sendMail = sendMail+"${entry.author}@demo.com,"
changeString += "--${truncated_msg} [${entry.author}]\n"
}
}
if (!changeString) {
changeString = " - 无"
}
return [sendMail,changeString]
}
def sendEmail(status) {
def sendObject = getChangeString()
echo "${sendObject[0]}"
echo "${sendObject[1]}"
emailext attachLog: true, body: "更新记录:\n " + "${sendObject[1]}" + "\n\n 构建日志: $BUILD_URL/console" + "\n",
subject: "${JOB_NAME} (${BUILD_NUMBER})-"+status,to: "${sendObject[0]}"
}
修改构建展示名称和描述
currentBuild.displayName = "${appVersion}"
currentBuild.description = "${appVersion}"
日期时间
def timeStamp = Calendar.getInstance().getTime().format('yyyy-MM-dd HH:mm:ss',TimeZone.getTimeZone('Asia/Shanghai'))
def time=new Date().format('yyyy-MM-dd HH:mm:ss')