尝试在Jenkins文件中将'//'上的字符串拆分为'/'

时间:2018-05-03 20:08:44

标签: regex jenkins groovy jenkins-pipeline

  

//中以双向前斜杠Jenkinsfile标记字符串的正确方法是什么?

下面的示例导致字符串在单前向斜杠/上被标记化,这不是所需的行为。

<小时/>的 Jenkinsfile <小时/>

包含相关部分的Jenkinsfile的简略过度简化示例是:

node {
    // Clean workspace before doing anything
    deleteDir()

    try {
        stage ('Clone') {
            def theURL = "http://<ip-on-lan>:<port-num>/path/to/some.asset"
            sh "echo 'theURL is: ${theURL}'"
            def tokenizedURL = theURL.tokenize('//')
            sh "echo 'tokenizedURL is: ${tokenizedURL}'"
        }
    } catch (err) {
        currentBuild.result = 'FAILED'
        throw err
    }
}


日志:

前面的日志输出是:

echo 'theURL is: http://<ip-on-lan>:<port-num>/path/to/some.asset'— Shell Script<1s
    [ne_Branch-Name-M2X23QGNMETLDZWFK7IXVZQRCNSWYNTDFJZU54VP7DMIOD6Z4DGA] Running shell script
    + echo theURL is: http://<ip-on-lan>:<port-num>/path/to/some.asset
    theURL is: http://<ip-on-lan>:<port-num>/path/to/some.asset

echo 'tokenizedURL is: [http:, <ip-on-lan>:<port-num>, path, to, some.asset]'— Shell Script<1s  
    [ne_Branch-Name-M2X23QGNMETLDZWFK7IXVZQRCNSWYNTDFJZU54VP7DMIOD6Z4DGA] Running shell script
    + echo tokenizedURL is: [http:, <ip-on-lan>:<port-num>, path, to, some.asset]
    tokenizedURL is: [http:, <ip-on-lan>:<port-num>, path, to, some.asset]

请注意,日志显示该字符串是/而不是//上的令牌。

1 个答案:

答案 0 :(得分:1)

tokenize takes string as optional argument可能包含1个或多个字符作为分隔符。它将字符串参数中的每个字符视为单独的分隔符,因此// 实际上/相同

要拆分//,您可以使用支持正则表达式的split

theURL.split(/\/{2}/)  

Code Demo