首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用Jenkinsfile/pipline插件将工作区识别为目录

无法使用Jenkinsfile/pipline插件将工作区识别为目录
EN

Stack Overflow用户
提问于 2017-01-18 13:33:28
回答 3查看 1.7K关注 0票数 0

我试图在目录中递归地搜索文件,因此不能使用findFiles。我已经看到了通过手动登录到从目录,但它不能识别在下面的代码。当我使用isDirectory()时,在以后使用dir.listFiles()时,它会说false,然后返回null。

以下是代码:

代码语言:javascript
复制
def recursiveFileSearch(File dir, filename, filesPath) {

  File[] files = dir.listFiles() // It returns null here as it cannot recognize it as directory
  echo "$files" 
  for (int i=0; i < files.size(); i++) {
    if (files[i].isDirectory()) {
      recursiveFileSearch(files[i], filename, filesPath)
    } else {
      if (files[i].getAbsolutePath().contains(filename)) {
        filesPath.add(files[i].getAbsolutePath())
        return filesPath
      }
    }
  }
  return filesPath
}

node('maven') {
      git 'https://github.com/rupalibehera/t3d.git'
      sh 'mvn clean install'
      File currentDir = new File(pwd())

      def isdir = currentDir.isDirectory()
      println "isdir:${isdir}" // The output here is False
      def isexist = currentDir.exists()
      println "isexist:${isexist}" // The output here is False
      def canread = currentDir.canRead()
      println "canread:${canread}" // The output here is False
      def filesPath = []
      def openshiftYaml = recursiveFileSearch(currentDir, "openshift.yml", filesPath)
} 

我不知道这里出了什么问题。

但以下是一些观察结果:

  • 当我执行File currentDir = new File(".")时,它返回/并开始读取我不想要的完整根目录,而且它也不识别工作区为目录
  • 如果我在主节点上运行它,它就会执行得很好,但在我的用例中,它总是一个从节点。
  • 我还检查了用户具有读/写/执行权限的目录的权限。

如有任何指示/帮助,将不胜感激。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-01-19 11:03:26

我找到了答案,为了从Jenkinsfile中搜索工作区中的任何文件,您可以使用findFiles步骤,我确实尝试过这样做,但我传递的是相同的不正确的glob。现在我只做def files = findFiles(glob: '**/openshift.yml') \\ it returns the path of file

票数 0
EN

Stack Overflow用户

发布于 2017-01-19 12:17:05

通常,运行一个sh步骤来完成您需要的任何工作。您不能使用管道脚本中的java.io.File等。它不会在代理上运行,而且也是不安全的,这就是为什么在沙箱模式保持不变(默认)时,任何这样的尝试都将被拒绝。

票数 1
EN

Stack Overflow用户

发布于 2017-07-27 21:25:17

您正在遇到文件在管道描述中的应用问题。我太清楚了。文件对象和NIO可以很好地分解路径,但是它们的isDirectory、存在和其他方法作为Jenkinsfile的一部分而不是在节点上运行。因此,因为文件在工作区中,所以主人上的所有用途看起来都很好。所有在节点上的使用都失败了。

总之,不要那么做。使用fileExists()pwd()、findFiles等

如果您创建了一个shareLibrary,并且希望在Jenkins之外的代码上使用单元测试,那么您可以创建一个依赖于脚本对象(管道中的“this”)的面板。

用于共享库的类

代码语言:javascript
复制
class PipelineUtils implements Serializable {
    static def pipelineScript = null;

    /**
     * Setup this fascade with access to pipeline script methods
     * @param jenkinsPipelineScript
     * @return
     */
    static initialize(def jenkinsPipelineScript) {
        pipelineScript = jenkinsPipelineScript
    }

    /**
     * Use pipelineScript object ('this' from pipeline) to access fileExists
     * We cannot use Java File objects for detection as the pipeline script runs on master and uses delegation/serialization to
     * get to the node.  So, File.exists() will be false if the file was generated on the node and that node isn't master.
     * https://support.cloudbees.com/hc/en-us/articles/230922128-Pipeline-Using-java-io-File-in-a-Pipeline-description
     * @param target
     * @return true if path exists
     */
    static boolean exists(Path target) {
        if (!pipelineScript) {
            throw new Exception("PipelineUtils.initialize with pipeline script not called - access to pipeline 'this' required for access to file detection routines")
        }

        if (! target.parent) {
            throw new Exception('Please use absolutePaths with ${env.WORKSPACE}/path-to-file')
        }
        return pipelineScript.fileExists(target.toAbsolutePath().toString())
    }

    /**
     * Convert workspace relative path to absolute path
     * @param path relative path
     * @return node specific absolute path
     */
    static def relativeWorkspaceToAbsolutePath(String path) {
        Path pwd = Paths.get(pipelineScript.pwd())
        return pwd.resolve(path).toAbsolutePath().toString()
    }

    static void echo(def message) {
        pipelineScript.echo(message)
    }
}

用于测试的

类JenkinsStep {静态布尔值fileExists(def路径){返回新文件(路径).exists()}

代码语言:javascript
复制
static def pwd() {
    return System.getProperty("user.dir")
}

static def echo(def message) {
    println "${message}"
}

}

在jenkins中的使用

代码语言:javascript
复制
PipelineUtils.initialize(this)
println PipelineUtils.exists(".")
// calls jenkins fileExists()

在单元测试中的使用

代码语言:javascript
复制
PipelineUtils.initialize(new JenkinsStep())
println PipelineUtils.exists(".")
// calls File.exists
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41720831

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档