Jenkins Groovy管道搜索和比较

时间:2018-06-25 20:39:54

标签: shell jenkins groovy jenkins-pipeline jenkins-groovy

我需要以下解决方案才能起作用-在BRANCHVERSION中搜索值变量ver_list,进行比较并使用适当的变量。

BRANCHVERSION的格式为"6.200.01"

​ver_list看起来像这样

[Pipeline] echo
[6.000.02, 6.000.10, 6.000.12, 6.000.15, 6.000.20, 6.000.25, 6.000.30, 6.100.00, 6.100.01, 6.100.10, 6.100.20, 6.100.25, 6.100.30, 6.200.00, 6.300.00]

如果BRANCHVERSION不等于ver_list中的任何值,则必须选取降序排列的列表中的下一个最接近的值(如果BRANCHVERSION = 6.200.01,然后必须领取6.200.00) 如果BRANCHVERSION等于ver_list中的任何值,则该值将被提取。

我的管道如下所述:

if (ENVIRONMENT == "") {
   error("You should choose at least one environment")
}
node {   
   deleteDir()
    checkout([$class: 'SubversionSCM', locations: [[credentialsId: 'XXXXXXX',local: './env_prop_files', remote:'http://FQDN/repos/src/cm/env_prop_files']]])
    checkout([$class: 'SubversionSCM', locations: [[credentialsId: 'XXXXXXX',local: './SoapUITestSuites', remote:'http://FQDN/repos/src/tools/SoapUI/branches/']]])
    def  FILES_LIST = sh (script: "ls   './SoapUITestSuites'", returnStdout:true).trim()
    def GS_LIST = sh (script: "ls 'path_to_file'", returnStdout: true).trim()
echo "GS_Version : ${GS_LIST}"
echo "FILES_LIST : ${FILES_LIST}"
//PARSING TestSuite Version
   def ver_list = []
     for(String ele : FILES_LIST.split("\\r?\\n")){ 
     ver = ele.split("_")
     println ">>>${ver[1]}<<<"     
     ver_list.add(ver[1])
}
println(ver_list)
def VERSION = ver_list
//PARSING GS Version
   def gs_list = []
    for(String elegs : GS_LIST.split("\\r?\\n")){ 
    println ">>>${elegs}<<<"     
    gs_list.add(elegs)
}  
println(gs_list) 
properties = readProperties file:"./env_prop_files/${ENVIRONMENT}.properties"
BRANCHVERSION = properties.SVN_LOCATION_VERSION
}

输出

No changes for http://FQDN/repos/src/tools/SoapUI/branches since the previous 
build
[Pipeline] sh
[ClientRunSoapUITest] Running shell script
+ ls ./SoapUITestSuites
[Pipeline] sh
[ClientRunSoapUITest] Running shell script
+ ls /pkg/flexprod/oracle/flexprodfiles/archive/goldstand
[Pipeline] echo
GS_Version : 5.900.00
6.000.00
6.000.01
6.000.02
6.000.20
6.000.30
6.100.00
6.100.01
6.200.00
[Pipeline] echo
FILES_LIST : rel_6.000.02
rel_6.000.10
rel_6.000.12
rel_6.000.15
rel_6.000.20
rel_6.000.25
rel_6.000.30
rel_6.100.00
rel_6.100.01
rel_6.100.10
rel_6.100.20
rel_6.100.25
rel_6.100.30
rel_6.200.00
rel_6.200.01
rel_6.300.00
[Pipeline] echo
>>>6.000.02<<<
[Pipeline] echo
>>>6.000.10<<<
[Pipeline] echo
>>>6.000.12<<<
[Pipeline] echo
>>>6.000.15<<<
[Pipeline] echo
>>>6.000.20<<<
[Pipeline] echo
>>>6.000.25<<<
[Pipeline] echo
>>>6.000.30<<<
[Pipeline] echo
>>>6.100.00<<<
[Pipeline] echo
>>>6.100.01<<<
[Pipeline] echo
>>>6.100.10<<<
[Pipeline] echo
>>>6.100.20<<<
[Pipeline] echo
>>>6.100.25<<<
[Pipeline] echo
>>>6.100.30<<<
[Pipeline] echo
>>>6.200.00<<<
[Pipeline] echo
>>>6.200.01<<<
[Pipeline] echo
>>>6.300.00<<<
[Pipeline] echo
[6.000.02, 6.000.10, 6.000.12, 6.000.15, 6.000.20, 6.000.25, 6.000.30, 6.100.00, 6.100.01, 6.100.10, 6.100.20, 6.100.25, 6.100.30, 6.200.00, 6.200.01, 6.300.00]
[Pipeline] echo
>>>5.900.00<<<
[Pipeline] echo
>>>6.000.00<<<
[Pipeline] echo
>>>6.000.01<<<
[Pipeline] echo
>>>6.000.02<<<
[Pipeline] echo
>>>6.000.20<<<
[Pipeline] echo
>>>6.000.30<<<
[Pipeline] echo
>>>6.100.00<<<
[Pipeline] echo
>>>6.100.01<<<
[Pipeline] echo
>>>6.200.00<<<
[Pipeline] echo
[5.900.00, 6.000.00, 6.000.01, 6.000.02, 6.000.20, 6.000.30, 6.100.00, 6.100.01, 6.200.00]
[Pipeline] readProperties
[Pipeline] }
[Pipeline] // node
[Pipeline] echo
"6.200.01"
[Pipeline] lock

1 个答案:

答案 0 :(得分:0)

首先,这些是字符串,因此您应该c 将它们挂起来以加倍。比较它们容易得多: 6.200.21变为6200,21 (或6200.21,具体取决于您的本地化)

将它们保留在列表中,对其进行处理要容易得多 剩下的就是简单的Groovy List操作

BRANCH_VERSION = ver_list.contains(BRANCH_VERSION) ? BRANCH_VERSION : ver_list.max()

当然,如果您需要它作为字符串,请使用DecimalFormat对象将其转换回并用点替换逗号。