
本篇將介紹 Jenkins 的 Pipeline Job 透過 Groovy 搜尋檔案。
1. FileNameFinder
資料夾內容
此範例我要搜尋 JENKINS_HOME 資料夾底下的檔案,內容如下:

Groovy Script
1 | new FileNameFinder().getFileNames(env.JENKINS_HOME, "*.xml", "jenkins*") |
- getFileNames
- 參數 1: 要搜尋的資料夾
- 參數 2: 符合的 Pattern(可省略)
- 參數 3: 排除的 Pattern(可省略)
執行結果

2. eachFile*
除了 FileNameFinder 以外,還有其他的搜尋方式,但我用 Jenkins 2.60.2 版本及 Pipeline: Groovy 2.38 版本,eachFile* 有 Bug 尚未修復,找到第一筆資料後就終止了。
用較早之前的版本,可以參考以下方法:
2.1. eachFile
1 | new File(env.JENKINS_HOME).eachFile() { file-> |
2.2. eachFileRecurse
1 | import groovy.io.FileType |
2.3. eachFileMatch
1 | new File(env.JENKINS_HOME).eachFileMatch (~/.*.xml/) { file -> |
2.4. traverse
1 | import groovy.io.FileType |
授權執行
執行 Pipeline Job 後,若顯示失敗訊息:
1 | org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: ***** |