从 Jenkins 中的工作区 groovy 脚本读取 .txt 文件
Read .txt file from workspace groovy script in Jenkins
我是 Jenkins 和 groovy 脚本的新手,我想读取一个位于其中一项工作的工作区中的 .txt 文件。我正在尝试这样做:
myfile = Jenkins.instance.getJob('JobName').workspace.readFileFromWorkspace('file.txt');
但会导致以下错误:
groovy.lang.MissingMethodException: No signature of method:
hudson.FilePath.readFileFromWorkspace() is applicable for argument
types: (java.lang.String) values: [file.txt]
试试这个:
file = new File("${Jenkins.instance.getJob('JobName').workspace}/file.txt").text
我正在努力使它适用于工作区中文件的 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
}
我是 Jenkins 和 groovy 脚本的新手,我想读取一个位于其中一项工作的工作区中的 .txt 文件。我正在尝试这样做:
myfile = Jenkins.instance.getJob('JobName').workspace.readFileFromWorkspace('file.txt');
但会导致以下错误:
groovy.lang.MissingMethodException: No signature of method: hudson.FilePath.readFileFromWorkspace() is applicable for argument types: (java.lang.String) values: [file.txt]
试试这个:
file = new File("${Jenkins.instance.getJob('JobName').workspace}/file.txt").text
我正在努力使它适用于工作区中文件的 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
}