【问题标题】:Read .txt file from workspace groovy script in Jenkins从 Jenkins 中的工作区 groovy 脚本中读取 .txt 文件
【发布时间】:2018-01-23 12:08:00
【问题描述】:

我是 Jenkins 和 groovy 脚本的新手,我想阅读一个 .txt 文件,该文件位于其中一项工作的工作区中。我正在尝试这样做:

myfile = Jenkins.instance.getJob('JobName').workspace.readFileFromWorkspace('file.txt');

但是会导致如下错误:

groovy.lang.MissingMethodException:没有方法签名: hudson.FilePath.readFileFromWorkspace() 适用于参数 类型:(java.lang.String) 值:[file.txt]

【问题讨论】:

  • 是的,我也看到了这篇文章,但那里提出的解决方案并没有解决我的问题。
  • 你是在 jenkins-pipeline 还是普通的 jenkins groovy 脚本中? hudson.FilePath 类肯定没有 readFileFromWorkspace 方法:javadoc.jenkins.io/hudson/FilePath.html
  • 我正在编写常用的 groovy 脚本。据我搜索,我没有找到任何其他方式从工作区读取文件。我的目标是从包含作业名称和参数列表的文件中获取数据,并自动触发这些作业的构建。

标签: jenkins groovy jobs workspace


【解决方案1】:

试试这个:

file = new File("${Jenkins.instance.getJob('JobName').workspace}/file.txt").text

【讨论】:

    【解决方案2】:

    我正在努力使其适用于工作区中文件的 pom 模块,在 扩展选择参数。这是我使用 printlns 的解决方案:

    import groovy.util.XmlSlurper
    import java.util.Map
    import jenkins.*
    import jenkins.model.*
    import hudson.*
    import hudson.model.*    
    
    try{
    //get Jenkins instance
        def jenkins = Jenkins.instance
    //get job Item
        def item = jenkins.getItemByFullName("The_JOB_NAME")
        println item
    // get workspacePath for the job Item
        def workspacePath = jenkins.getWorkspaceFor (item)
        println workspacePath
    
        def file = new File(workspacePath.toString()+"\\pom.xml")
        def pomFile = new XmlSlurper().parse(file)
        def pomModules = pomFile.modules.children().join(",")
        return pomModules
    } catch (Exception ex){
        println ex.message
    }
    

    【讨论】:

    • 我想使用 groovy postbuild 访问/读取所有 txt 文件(包括工作区的子目录)。该怎么做?
    猜你喜欢
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    • 2016-07-27
    • 1970-01-01
    • 2013-09-28
    • 1970-01-01
    • 2013-10-31
    • 1970-01-01
    相关资源
    最近更新 更多