gpt4 book ai didi

jenkins - 使用 FilePath 访问 Jenkins 管道中从属设备上的工作区

转载 作者:行者123 更新时间:2023-12-03 01:59:11 25 4
gpt4 key购买 nike

作为管道构建作业的一部分,我需要检查工作区中是否存在某个 .exe 文件。我尝试使用 Jenkinsfile 中的以下 Groovy 脚本来执行相同的操作。但我认为默认情况下 File 类会尝试在 jenkins master 上查找工作区目录并失败。

@com.cloudbees.groovy.cps.NonCPS
def checkJacoco(isJacocoEnabled) {

new File(pwd()).eachFileRecurse(FILES) { it ->
if (it.name == 'jacoco.exec' || it.name == 'Jacoco.exec')
isJacocoEnabled = true
}
}

如何从 Jenkinsfile 内部使用 Groovy 访问从属设备上的文件系统?

我也尝试了下面的代码。但我收到 No such property: build for class: groovy.lang.Binding 错误。我还尝试改用管理器对象。但遇到同样的错误。

@com.cloudbees.groovy.cps.NonCPS
def checkJacoco(isJacocoEnabled) {

channel = build.workspace.channel
rootDirRemote = new FilePath(channel, pwd())
println "rootDirRemote::$rootDirRemote"
rootDirRemote.eachFileRecurse(FILES) { it ->
if (it.name == 'jacoco.exec' || it.name == 'Jacoco.exec') {
println "Jacoco Exists:: ${it.path}"
isJacocoEnabled = true
}
}

最佳答案

遇到同样的问题,找到了解决方案:

import hudson.FilePath;
import jenkins.model.Jenkins;

node("aSlave") {
writeFile file: 'a.txt', text: 'Hello World!';
listFiles(createFilePath(pwd()));
}

def createFilePath(path) {
if (env['NODE_NAME'] == null) {
error "envvar NODE_NAME is not set, probably not inside an node {} or running an older version of Jenkins!";
} else if (env['NODE_NAME'].equals("master")) {
return new FilePath(path);
} else {
return new FilePath(Jenkins.getInstance().getComputer(env['NODE_NAME']).getChannel(), path);
}
}
@NonCPS
def listFiles(rootPath) {
print "Files in ${rootPath}:";
for (subPath in rootPath.list()) {
echo " ${subPath.getName()}";
}
}

这里重要的是 createFilePath() 没有用 @NonCPS 注释,因为它需要访问 env 变量。使用 @NonCPS 会删除对“管道优点”的访问,但另一方面,它并不要求所有局部变量都是可序列化的。然后,您应该能够在 listFiles() 方法中搜索文件。

关于jenkins - 使用 FilePath 访问 Jenkins 管道中从属设备上的工作区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41250567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com