简体   繁体   English

使用Groovy从Jenkins master访问节点从属文件

[英]Access files on a node slave from Jenkins master using Groovy

I am using the Jenkins Build Flow plugin to achieve parallelization. 我正在使用Jenkins Build Flow插件来实现并行化。 The Groovy DSL does certain file operations. Groovy DSL执行某些文件操作。 Even though the option Restrict where this project can be run is set to run the job on a specific slave, the DSL runs on master. 即使Restrict where this project can be run选项设置为在特定从站上运行作业,DSL也在主站上运行。 This is not intended. 这不是预期的。

Could someone tell me how I can restrict the DSL to run on the specified slave? 有人能告诉我如何限制DSL在指定的奴隶上运行吗? Even if there is a way we can access the slave file system via the DSL, that should work. 即使有一种方法我们可以通过DSL访问从属文件系统,这应该工作。

In general, how can we access files on a node slave from Jenkins master using Groovy? 一般来说,我们如何使用Groovy从Jenkins master访问节点slave上的文件?

def fp = new hudson.FilePath(build.workspace.channel, "/srv/jenkins/workspace/myworkspace_on_slave_node")
assert fp.exists()      // returns true :)

def ant = new AntBuilder()

if (fp != null) {
  def scanner = ant.fileScanner {    // fails here :(, says /srv/jenkins/workspace/myworkspace_on_slave_node not found
    // grab ALL files requested to be run
    fileset(dir: "$fp", includes: "**/*.java")
  }

  // now lets iterate over - print - and count test files
  int numFiles = 0
  for (f in scanner) {
    println("Found file $f")    
    numFiles++
  }
  println("Total files $numFiles")
}

The workspace is there on the slave node, but the above code is failing when I am trying to open the FileSet to the remote FilePath. 工作空间位于从属节点上,但是当我尝试将FileSet打开到远程FilePath时,上面的代码失败了。

The Groovy DSL is always executed on master (in tomcats directory). Groovy DSL总是在master上执行(在tomcats目录中)。 Even if you install Node Label Parameter plugin and set build job to be executed on some specific slave. 即使您安装节点标签参数插件并设置要在某些特定从站上执行的构建作业。 If you want to get access from Groovy DSL to job workspace on slave you can use channel. 如果您想从Groovy DSL访问奴隶上的作业工作区,您可以使用频道。 There's my example of creating a file in build flow job workspace: 这是我在构建流作业工作区中创建文件的示例:

if(build.workspace.isRemote()){
channel = build.workspace.channel
}
String fp = build.workspace.toString() + "\\" + "newfile.txt"
newFile = new hudson.FilePath(channel, fp)
newFile.write("xyz", null)

An easier way is executing file operations in downstream jobs in Execute Groovy script (not in build flow job) running on specific slave. 更简单的方法是在特定从属服务器上运行的Execute Groovy脚本(不在构建流程作业)中的下游作业中执行文件操作。 You must have node plugin installed and pass slave name as a parameter in DSL script: build("jobA", paramNode: "nodename") 您必须安装节点插件并将slave名称作为DSL脚本中的参数传递:build(“jobA”,paramNode:“nodename”)

The Workflow Plugin "Originally inspired by the Build Flow Plugin " has the following section in its tutorial : Workflow Plugin “最初受Build Flow Plugin的启发”其教程中有以下部分:

Using slaves 使用奴隶

The parameter may be a slave name, or a single label, or even a label expression such as: 参数可以是从属名称,也可以是单个标签,甚至可以是标签表达式,例如:

  node('unix && 64bit') { // as before } 

The following question in the Build Flow Plugin's comments is unanswered since Jan 27, 2014: 自2014年1月27日以来,Build Flow Plugin的评论中的以下问题无法回答:

Alexander Uvizhev says: Alexander Uvizhev说:

Is there a way to specify where particular build should run? 有没有办法指定特定构建应该在哪里运行? By providing a label or a node name. 通过提供标签或节点名称。

Install NodeLabel parameter plugin . 安装NodeLabel参数插件 It Provides parameter option Label . 它提供参数选项Label

Then you can use this parameter in your DSL script to pass node name or value. 然后,您可以在DSL脚本中使用此参数来传递节点名称或值。

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

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