Read .txt file from workspace groovy script in Jenkins

こ雲淡風輕ζ 提交于 2019-12-19 05:07:05

问题


I am new to Jenkins and to groovy scripting, I want to read a .txt file which is located in the workspace of one of the jobs. I am trying to do this way:

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

But leads to the following error:

groovy.lang.MissingMethodException: No signature of method: hudson.FilePath.readFileFromWorkspace() is applicable for argument types: (java.lang.String) values: [file.txt]


回答1:


Try this:

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



回答2:


I was struggling to make it work for the pom modules for a file in the workspace, in the Extended Choice Parameter. Here is my solution with the 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
}


来源:https://stackoverflow.com/questions/48401363/read-txt-file-from-workspace-groovy-script-in-jenkins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!