有效选择参数中的Groovy

时间:2019-03-22 09:17:34

标签: jenkins groovy

我有以下代码编码在Active Choice Parameter中显示一些文件夹名称,如果该文件夹包含“ .a7”文件,则显示该文件,如果没有,则应发布错误消息。 我的问题是,如果缺少文件夹“ a7.nativ”并隐含了“ .a7”文件的路径(/mnt/a7/v5.5/a7.nativ/),我的鳕鱼将不会显示错误消息v5.5 / 55.a7)被中断。 有人可以帮我吗? 这是鳕鱼:

 Build=[]
path2 = "/mnt/cc7/v5.5/a7.nativ/v5.5/"
new File(path2).eachFileMatch(~/.*.a7/) {
              Build.add(it.getName())                
}  
if(Build){
return Build
} else {
return ["There is no file to display"]
}

1 个答案:

答案 0 :(得分:1)

您需要执行其他步骤来检查给定路径是否存在。否则,您将隐式假定给定文件夹始终存在。考虑以下修改:

def build = []
def path2 = "/mnt/cc7/v5.5/a7.nativ/v5.5/"
def file = new File(path2)

if (!file.exists()) {
    return ["There is no file to display"]
}

file.eachFileMatch(~/.*.a7/) {
    build.add(it.getName())
}

return build ?: ["There is no file to display"]