1

I'm using groovy in my Jenkinsfiles. The gradle-build creates a jar looking like this: appName-1.0.0-ua67435.jar and is copied to /opt/appName

In the Jenkinsfile (Groovy) I now like to get this part of the string 1.0.0-ua67435 in a variable. I hope this regex should do the job: -(.*?).jar$

I know where my file is, but I don't know its name. Its the only .jar in the /opt/appName/-folder.

How can I get the substring of my filename when I only know its place and name-pattern?

SWiggels
  • 2,159
  • 1
  • 21
  • 35

3 Answers3

2

Try this:

new File("/opt/appName/").eachFileMatch(FileType.FILES, ~/^.*-.*?.jar$/, { println it.name })

It uses eachFileMatch to match the name with specific pattern, you can see more examples from the doc link.


Using the full qualified name like this, if import is not allowed.

...eachFileMatch(groovy.io.FileType.FILES,...

aristotll
  • 8,694
  • 6
  • 33
  • 53
  • How can i get that name in a variable? – SWiggels Aug 25 '17 at 09:21
  • @SWiggels `it.name` is the name you want. – aristotll Aug 25 '17 at 09:22
  • I got import problems with FileType.FILES. It seems jenkins doesn't even like the import. – SWiggels Aug 25 '17 at 10:30
  • `import static groovy.io.FileType.FILES new File("$destDir").eachFileMatch(FileType.FILES, ~/^.*-.*?.jar$/, { println it.name })` results in `groovy.lang.MissingPropertyException: No such property: FileType for class: groovy.lang.Binding` – SWiggels Aug 25 '17 at 10:41
  • Now i got the same error as for Wiktors idea. ?org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod ' and yes I approved it in the script-security of jenkins. – SWiggels Aug 25 '17 at 10:50
  • 1
    Ok i got the solution. Many thanks also to @Wiktor! The problem was that when running the script it asked for approval. After it said it has no rights i had to approve another time for other methods. I had to do this 3 times in a row. Thank you guys! – SWiggels Aug 25 '17 at 10:56
2

You may use

def names = []
new File("/opt/appName/").eachFileMatch groovy.io.FileType.FILES, ~/(?<=appName-).*(?=[.]jar$)/, { names << it.name }

The (?<=appName-).*(?=[.]jar$) regex will fetch just the variable part between appName- and .jar at the end of string. If you add ^ before appName, it will be matched only when at the start of the file name.

See the regex demo.

Details

  • (?<=appName-) - make sure there is appName immediately to the left of the current location
  • .* - any 0+ chars other than line break chars as many as possible
  • (?=[.]jar$) - make sure there is .jar substring followed with an end of string position immediately to the right of the current position.

More details about eachFileMatch here.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Jenkins does not allow static imports. Additionally `baseDir` is not known. – SWiggels Aug 25 '17 at 10:29
  • @SWiggels This is an example snippet from the docs. I just changed the regex there. The `baseDir` should be initialized with `"/opt/appName/"`, sure. – Wiktor Stribiżew Aug 25 '17 at 10:33
  • `org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod` is what I get from running it. – SWiggels Aug 25 '17 at 10:34
  • Try it the way aristotll suggests. – Wiktor Stribiżew Aug 25 '17 at 10:35
  • aristotlls version needs the import as well. Hence it doens't work either. :( – SWiggels Aug 25 '17 at 10:36
  • There is one question on SO suggesting: *Navigate to `jenkins` > `Manage jenkins` > `In-process Script Approval`. There was a pending command, which I had to approve.*. [There are other hints](https://stackoverflow.com/questions/38276341/jenkins-ci-pipeline-scripts-not-permitted-to-use-method-groovy-lang-groovyobject). – Wiktor Stribiżew Aug 25 '17 at 10:40
  • Thanks for the hint. I already approved it. If you do not approve it it says that your method is not allowed in scripts. After accepting I got the static import is generally not allowed... – SWiggels Aug 25 '17 at 10:43
0

You can FileNameFinder class to find it easily:

println new FileNameFinder().getFileNames(folderPath, '**/appName*.jar').collect{ it.substring(it.lastIndexOf('/')+1, it.size())}
Rao
  • 20,781
  • 11
  • 57
  • 77
  • Using `FileNameFinder` in pipelines is dangerous if you are using slaves. Everything with files gets executed on the master only. See https://groups.google.com/forum/#!topic/jenkinsci-users/yBiYbwWjg-I – SWiggels Oct 29 '18 at 10:18