Home > Enterprise >  Remove substring from filename in jenkins groovy script
Remove substring from filename in jenkins groovy script

Time:11-25

Hi I am trying to remove substring "-unsigned" from filename in jenkins pipeline script.

where filePattern app/build/outputs/**/-release.apk".

I wrote below groovy script

        findFiles(glob: filePattern).each { file ->
            sh """
            mv ${file.path} "${file.path//-unsigned/}"
            """
        }

getting error unexpected char : 0XFFFF.

Can suggest where exactly I am missing. or suggest how to remove substring from file name in groovy.

CodePudding user response:

not sure it's the best way to rename files however:

findFiles(glob: filePattern).each { file ->
   sh """
      mv ${file.path} "${file.path - '-unsigned'}"
      """
}

issue in your code that you have // in this expression ${file.path // ...}

and compiler could take it as a single line comment

try to run this in groovy console:

"""
${'abc' //no matter what here}
"""
//comment here

^^^ compilation error: unexpected char: 0xFFFF

CodePudding user response:

See bash(1) - Linux man page:

EXPANSION

[...]

Parameter Expansion

[...]

${parameter/pattern/string}

Pattern substitution. [...] Parameter is expanded and the longest match of pattern against its value is replaced with string.

So it should be "${file.path/-unsigned//}".

  • Related