0

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]

Diado
  • 2,229
  • 3
  • 18
  • 21
  • looks similar to https://stackoverflow.com/questions/22917491/reading-file-from-workspace-in-jenkins-with-groovy-script – madteapot Jan 23 '18 at 12:09
  • yes, I saw this post as well but the solutions proposed there did not solve my issue. –  Jan 23 '18 at 12:15
  • are you in jenkins-pipeline or usual jenkins groovy script? the class `hudson.FilePath` definitely has no method readFileFromWorkspace : http://javadoc.jenkins.io/hudson/FilePath.html – daggett Jan 23 '18 at 12:43
  • I working on usual groovy script. As far as I am searching I am not finding any other way to read the file from workspace. My goal is to fetch the data from a file which contain the list of job names and parameters and to automate to trigger builds for these jobs. –  Jan 23 '18 at 12:51

2 Answers2

1

Try this:

file = new File("${Jenkins.instance.getJob('JobName').workspace}/file.txt").text
Evgeny Smirnov
  • 2,886
  • 1
  • 12
  • 22
1

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
}
coz
  • 445
  • 8
  • 14