简体   繁体   English

从Jenkins的工作区Groovy脚本读取.txt文件

[英]Read .txt file from workspace groovy script in Jenkins

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. 我是Jenkins和groovy脚本的新手,我想读取一个.txt文件,该文件位于其中一个作业的工作空间中。 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] groovy.lang.MissingMethodException:方法的无签名:hudson.FilePath.readFileFromWorkspace()适用于参数类型:(java.lang.String)值:[file.txt]

尝试这个:

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

I was struggling to make it work for the pom modules for a file in the workspace, in the Extended Choice Parameter. 我正在努力使它适用于扩展选择参数中工作空间中文件的pom模块。 Here is my solution with the printlns: 这是我的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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM