简体   繁体   English

使用 System groovy 脚本从 Jenkins 工作区读取文件

[英]Read file from Jenkins workspace with System groovy script

I have a question very similar to this: Reading file from Workspace in Jenkins with Groovy script我有一个与此非常相似的问题: Reading file from Workspace in Jenkins with Groovy script

However I need to read the file from a System Groovy script so the solution of using Text-finder or the Groovy PostBuild plugin will not work.但是,我需要从 System Groovy 脚本中读取文件,因此使用 Text-finder 或 Groovy PostBuild 插件的解决方案将不起作用。

How can I get the workspace path from a system groovy script?如何从系统 groovy 脚本中获取工作区路径? I have tried the following:我尝试了以下方法:

System.getenv('WORKSPACE')
System.getProperty("WORKSPACE")
build.buildVariableResolver.resolve("WORKSPACE")

Thanks!谢谢!

If you have a file called "a.txt" in your workspace , along with a script called "sysgvy.groovy" that you want to execute as a system groovy script.如果您的工作区中有一个名为“a.txt”的文件,以及您希望作为系统 groovy 脚本执行的名为“sysgvy.groovy”的脚本。 Suppose your "sysgvy.groovy" script needs to read the file "a.txt".假设您的“sysgvy.groovy”脚本需要读取文件“a.txt”。

The issue of this topic is that if your script read "a.txt" directly without providing any path, "sysgvy.groovy" executes and will throw an error saying cannot find "a.txt".本主题的问题是,如果您的脚本在不提供任何路径的情况下直接读取“a.txt”,则“sysgvy.groovy”会执行并抛出错误,提示找不到“a.txt”。

I have tested and found that the following method works good.我已经测试并发现以下方法效果很好。

def build = Thread.currentThread().executable

Then use然后使用

build.workspace.toString()+"\\a.txt"

as the full location string to replace "a.txt".作为替换“a.txt”的完整位置字符串。

It's also important to run on the Jenkins master machine by placing "a.txt" and "sysgvy.groovy" onto Jenkins master machine's workspace.通过将“a.txt”和“sysgvy.groovy”放置到 Jenkins 主机的工作区,在 Jenkins 主机上运行也很重要。 Executing on slave machine does not work.在从机上执行不起作用。

Try it, the file should be found and get read in the script without any problem.尝试一下,应该可以找到该文件并在脚本中读取该文件,没有任何问题。

If there is problem with variable Thread, it is just that some modules need to be imported.如果变量Thread有问题,只是需要导入一些模块。 So add these lines to the start of code:因此,将这些行添加到代码的开头:

import jenkins.*
import jenkins.model.*
import hudson.*
import hudson.model.*

Each build has a workspace , so you need to find the desired project first.每个构建都有一个工作空间,因此您需要先找到所需的项目。 (The terms "job" and "project" are used rather interchangeable in Jenkins - also in the API.) (术语“工作”和“项目”在 Jenkins 中可以互换使用——在 API 中也是如此。)

After that, you can either cross your fingers and just call getWorkspace() , which is deprecated (see JavaDoc for details).之后,您可以交叉手指并调用getWorkspace() ,它已被弃用(有关详细信息,请参阅JavaDoc )。

Or you can find a specific build (eg the last), which can give you the workspace used for that specific build via the getWorkspace() method as it is defined in the AbstractBuild class.或者您可以找到一个特定的构建(例如最后一个),它可以通过在AbstractBuild类中定义的getWorkspace()方法为您提供用于该特定构建的工作空间

Example code:示例代码:

Jenkins.instance.getJob('<job-name>').lastBuild.workspace;

Just use只需使用

build.workspace

The "build" variable is available keyword in System Groovy Script . “build”变量是System Groovy Script 中的可用关键字。

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

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