1.直接定义
def x="abc"
2.从脚本执行结果赋值变量 branch = “/jen_script/return-branch.sh $group $job”.execute().text
#将结果通过逗号分隔,写入数组里 branch = “one, two, three” branch_list = branch[1..-2].tokenize(‘,’)
3.引号
```python
def x="abc"
print '${x}' //输出${x},不支持变量
print "${x}" //输出abc,支持变量
print ''' //输出${X},不支持变量
${X}
'''
print """ //输出abc,支持变量
${X}
"""
1.判断变量
if
(x='abc')
{
echo "abc"
}
else
(x='bcd')
{
echo "bcd"
}
1.定义方法并调用
/String是声明这个变量应该类型是字符串,可以省略,类型则根据传入类型而变
def createName(String givenName,
String familyName){
return givenName +
""
+ familyName
}
//调用,可省略括号
createName(familyName =
"Lee", givenName =
"Bruce")
2.方法添加默认参数
def sayHello(String name =
"zhangsan"){
print
"hello ${name}"
}
//不传参时括号不能省略了
sayHello()
3.闭包
//定义闭包
def codeBlock =
{print
"hello closure"}
//闭包还可以直接当成函数调用
codeBlock()
//输出hello closure
4.闭包作为参数传递给另一个方法
//定义闭包
def codeBlock =
{print
"hello closure"}
//定义一个方法,它接收一个闭包参数
def sayHello(closure)
{
closure()
}
//在调用sayHello方法时可以这样
sayHello(codeBlock)
//如果把闭包定义的语句去掉
sayHello(
{print
"hello closure"}
)
//由于括号是非必需的,所以
sayHello {
print
"hello closure"
}
//如果sayHello改成名字为pipeine就是,是不是很像jenkins的pipeline
pipeline {
print
"hello closure"
}
5.闭包另类用法,定义一个stage方法
//定义方法,传一个正常变量和一个闭包
def stage(String name, closue)
{
print name
closue()
}
//在正常情况下,这样使用stage函数
stage("stage name",
{print
"closure"})
//执行打印
//stage name
//closure
//可以用另一种写法
stage("stage name")
{
print
"closure"
}
1.定义数组,然后判断是否在数组中。比如判断two是否在one这个数组里,需要先定义字符串,后面切割。
ipeline {
agent any
environment {
one =
"xxx,ddd,lll"
two =
"ddd"
}
stages {
stage('pull')
{
steps {
script {
list = one.split(',')
for
( i in list )
{
echo "$i"
echo "$two"
if
(i == two)
{
echo "ok two"
}
else
{
echo "no two"
}
}
}
}
}
}
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。