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

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

问题描述

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

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

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

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

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

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

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

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天全站免登陆