使用FilePath访问Jenkins管道中的从属工作区 [英] Using FilePath to access workspace on slave in Jenkins pipeline

查看:339
本文介绍了使用FilePath访问Jenkins管道中的从属工作区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

新文件(pwd())。eachFileRecurse(FILES){it - >
if(it.name =='jacoco.exec'|| it.name =='Jacoco.exec')
isJacocoEnabled = true
}
}

如何使用Jenkinsfile中的Groovy访问slave上的文件系统?

我也尝试了下面的代码。但是我得到没有这样的属性:构建类:groovy.lang.Binding 错误。我也尝试使用管理器对象。但是得到同样的错误。

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ def $ j $($ @ $ $ $ $ $ $ $

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


解决方案

同样的问题,发现这个解决方案:

$ p $ import hudson.FilePath;
导入jenkins.model.Jenkins;

node(aSlave){
writeFile file:'a.txt',text:'Hello World!';
listFiles(createFilePath(pwd()));
}
$ b $ def def createFilePath(path){
if(env ['NODE_NAME'] == null){
errorenvvar NODE_NAME未设置,可能不会在节点{}内运行或运行旧版本的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$ {rootPath}中的文件:;
for(subPath in rootPath.list()){
echo$ {subPath.getName()};




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


I need to check for the existence of a certain .exe file in my workspace as part of my pipeline build job. I tried to use the below Groovy script from my Jenkinsfile to do the same. But I think the File class by default tries to look for the workspace directory on jenkins master and fails.

@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
    }
}

How to access the file system on slave using Groovy from inside the Jenkinsfile?

I also tried the below code. But I am getting No such property: build for class: groovy.lang.Binding error. I also tried to use the manager object instead. But get the same error.

@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 
    } 
}

解决方案

Had the same problem, found this solution:

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()}";
    }
}

The important thing here is that createFilePath() ins't annotated with @NonCPS since it needs access to the env variable. Using @NonCPS removes access to the "Pipeline goodness", but on the other hand it doesn't require that all local variables are serializable. You should then be able to do the search for the file inside the listFiles() method.

这篇关于使用FilePath访问Jenkins管道中的从属工作区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆