jenkins-groovy基础

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

字符串和列表

"devopstestops".contains("ops")  // 包含 ops
true
"devopstestops".endsWith("ops)   // 已 ops 结尾
true
"devopstestops".size() // 字符串长度 length() 依然是计算长度
13
"devops".toUpperCase() // 转大写
DEVOPS
"DEVOPS".toLowerCase() // 转小写
devops

--- 字符串分割
hosts = "host01,host02,host03".split(',')
[host01,host02,host03]
for ( i in hosts ){
    println(i)
}
host01
host02
host03

--- 添加元素
result = [1,2,3,4]  
result.add(5)
println(result)

--- 去重
[2,3,4,5,5,6,6].unique()
[2,3,4,5,6]

--- 链接
[2,3,4,5,6].join("-")
2-3-4-5-6

--- 遍历
result = [1,2,3,4]
result.each{
    println it
}
1
2
3
4

字典

[key:value]
--- 获取key
[1:2,3:4,5:6].keySet()
[1,3,5]

--- 获取值
[1:2,3:4,5:6].values()
[2,4,6]

--- 追加
[1:2,3:4,5:6] + [7:8]
[1:2,3:4,5:6,7:8]

--- 减少
[1:2,3:4,5:6,7:8] - [7:8]
[1:2,3:4,5:6]

if

buildType = "maven"
if (buildType == "maven"){
    println("This is a maven project!")
} else if (){

} else {

}

switch

buildType = "gradle"
switch("${buildType}"){
	case 'maven':
		println("This is maven")
		break;
		;;
	
	case 'gradle':
		println("This is a gradle")
		break;
		;;
		
	default:
		println("Project Type Error")
		;;
}

This is a gradle

👙以上写法,如果不加 break,则始终会执行 default

for和while

langs = ['java', 'python', 'groovy']
for (lang in langs){
	if (lang == 'java'){
		println('lang error in java')
	}else {
		println("lang is ${lang}")
	}
}

lang error in java
lang is python
lang is groovy
while(1==1){
	println('true')
}

function

def PrintMes(){
	println('true')
}
PrintMes()

def PrintMes(info){
	println(info)
}
PrintMes("devops")

正则

@NonCPS // 解释器,处理重复运行情况下 json 序列化问题
String getBranch(String branchName){
	def matcher = (branchName =~ "RELEASE-[0-9]{4}") // 构建判断 branchName是否包含 RELEASE-4位数字的匹配器
	if (matcher.find()){
		newBranchName = matcher[0]
	} else {
		newBranchName = branchName
	}
	newBranchName
}

newBranchName = getBranch(branchName)
println("新分支名 --> ${newBranchName}")