获取Jenkinsfile(Groovy)中给定文件夹中的文件名列表 [英] Get a list of filenames in a given folder in Jenkinsfile (Groovy)
问题描述
我无法在脚本化的Jenkins管道的给定目录中创建文件名的简单列表.我尝试了在SO和其他论坛上发布的许多Groovy Script示例,但是该功能被阻止或找不到方法或其他方法.
I cannot create a simple list of filenames in a given directory in a scripted Jenkins Pipeline. I tried many Groovy Script examples posted on SO and other forums but either the feature is blocked or I get method not found or whatever.
这似乎是最简单的
def DOCKER_FILES_DIR = './dockerfiles'
// ...
def dir = new File(DOCKER_FILES_DIR);
def dockerfiles = [];
dir.traverse(type: FILES, maxDepth: 0) {
dockerfiles.add(it)
}
但这会错误地解析相对路径,所以我收到此错误:
But this resolves the relative path incorrectly, so I get this error:
java.io.FileNotFoundException: /./dockerfiles
我还尝试将其包装在dir
中:
I also tried to wrap it in dir
with:
def DOCKER_FILES_DIR = './dockerfiles'
// ...
def dockerfiles = [];
dir("${env.WORKSPACE}"){
def dir = new File(DOCKER_FILES_DIR);
dir.traverse(type: FILES, maxDepth: 0) {
dockerfiles.add(it)
}
}
但是出现了相同的错误:
But got the same error:
java.io.FileNotFoundException: /./dockerfiles
这不会产生错误,而只会在列表中添加一个文件:
This does not give an error, but only adds one file to the list:
def DOCKER_FILES_DIR = './dockerfiles'
// ...
def dockerfiles = [];
def dir = new File("${env.WORKSPACE}/${DOCKER_FILES_DIR}");
dir.traverse(type: FILES, maxDepth: 0) {
dockerfiles.add(it.getName())
}
然后dockerfiles
的内容将丢失除第一个文件以外的所有文件:
The contents of dockerfiles
is then missing all the files but the first one:
['py365']
这里是在Jenkins中重现它的最小管道:
Here is a minimal Pipeline to reproduce it in Jenkins:
#!groovy
import static groovy.io.FileType.FILES
node('master') {
FILES_DIR = './foo'
cleanWs()
sh """
mkdir foo
touch foo/bar1
touch foo/bar2
touch foo/bar3
"""
def filenames = [];
def dir = new File("${env.WORKSPACE}/${FILES_DIR}");
dir.traverse(type: FILES, maxDepth: 0) {
filenames.add(it.getName())
}
for (int i = 0; i < filenames.size(); i++) {
def filename = filenames[i]
echo "${filename}"
}
}
和显示仅打印bar1
的输出:
Started by user Tamas Gal
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/TestHome
[Pipeline] {
[Pipeline] cleanWs
[WS-CLEANUP] Deleting project workspace...[WS-CLEANUP] done
[Pipeline] sh
[TestHome] Running shell script
+ mkdir foo
+ touch foo/bar1
+ touch foo/bar2
+ touch foo/bar3
[Pipeline] echo
bar1
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
推荐答案
您不能真正使用new File
和常规的Groovy/Java方式遍历文件系统.默认情况下,对呼叫进行了安全检查(请参阅 JENKINS-38131 ),并且不会甚至由于Jenkins Pipelines如何执行您的管道代码而正常工作.
You can't really make use of the new File
and normal Groovy/Java ways to traverse file systems. The call is security checked by default (see JENKINS-38131) and won't even generally work because of how Jenkins Pipelines executes your pipeline code.
您可以使用 findFiles
步骤" rel ="noreferrer"> Pipeline Utility步骤插件.它返回一个
One way you could do this would be to use the findFiles
step from the Pipeline Utility Steps plugin. It returns a FileWrapper[]
which can be inspected/used for other purposes.
node {
// ... check out code, whatever
final foundFiles = findFiles(glob: 'dockerfiles/**/*')
// do things with FileWrapper[]
}
另一种选择是掏出外壳并捕获标准件:
Another option is to shell out and capture the standard out:
node {
// ... check out code, whatever
final foundFiles = sh(script: 'ls -1 dockerfiles', returnStdout: true).split()
// Do stuff with filenames
}
这篇关于获取Jenkinsfile(Groovy)中给定文件夹中的文件名列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!