[英]Groovy Script in Jenkins: dependency management
我试图让这个Groovy脚本在Jenkins中运行:
import java.lang.ProcessBuilder.Redirect
import hudson.model.*
import hudson.util.*
import hudson.scm.*
//I'm not sure that these 2 imports are correct:
import hudson.plugins.tfs.model.ChangeSet;
import hudson.plugins.tfs.model.ChangeSet.Item;
// work with current build
def build = Thread.currentThread()?.executable
// get ChangesSets with all changed items
def changeSet= build.getChangeSet()
List<Item> items = changeSet.getItems()
def affectedFiles = items.collect { it.paths }
// get file names
def fileNames = affectedFiles.flatten().findResults
// setup log files
def stdOutFile = "${build.rootDir}\\stdout_groovy.txt"
def stdErrFile = "${build.rootDir}\\stderr_groovy.txt"
//execute a command for each file
fileNames.each
{
def params = ["cmd.exe", "/C", "dir ${it}"]
def processBuilder = new ProcessBuilder(params)
// redirect stdout and stderr to log files
processBuilder.redirectOutput(new File(stdOutFile))
processBuilder.redirectError(new File(stdErrFile))
def process = processBuilder.start()
process.waitFor()
// print log files
println new File(stdOutFile).readLines()
System.err.println new File(stdErrFile).readLines()
}
我的目标是能够仅迭代已更改的文件并将这些文件复制到另一个位置。 我正在尝试在Process中创建一个版本的代码, 只更改了与TFS一起使用的文件 。 我得到2个错误:
无法解析类hudson.plugins.tfs.model.ChangeSet.Item
无法解析类hudson.plugins.tfs.model.ChangeSet
这是有道理的,因为Groovy不知道从哪里获得TFS代码。 我在https://wiki.jenkins-ci.org/display/JENKINS/Team+Foundation+Server+Plugin上使用TFS插件。 我已经读过Groovy可以使用Grape进行依赖管理。 如果TFS插件在http://mvnrepository.com/中,那么我可以使用它来让Groovy获得最新的代码,如下所示:
@Grapes(
@Grab(group='org.apache.maven.scm', module='maven-scm-provider-tfs', version='1.9.4')
)
但是,TFS插件不在http://mvnrepository.com/中 。 插件源位于https://github.com/jenkinsci/tfs-plugin 。 那么,我怎么能告诉Groovy在哪里获取代码? 理想情况下,我不必将插件代码复制到我的构建机器并处理保持最新。 (我确定我的代码中存在错误。我只是试图让依赖管理得以解决(但是可以随意指出编码错误))。 谢谢。
只要安装了插件,就可以从jenkins中运行的groovy访问插件类。 话虽这么说,听起来你可能没有安装插件!?!
理想情况下,我不必将插件代码复制到我的构建机器并处理保持最新。
如果是这种情况,我真的不明白为什么你会尝试使用未安装在jenkins中的插件代码。
正如我所看到的,你正在使用“groovy”插件在一个作业中运行Groovy脚本。
简单的“Groovy脚本”在分叉的JVM中运行,在运行构建的从属设备上运行。 它与运行“groovy”命令并传入脚本基本相同。 所以你需要添加类路径条目,如:
groovy -cp <path-to-jars> script.groovy
系统 groovy脚本在Jenkins master的JVM中运行。 因此,它可以访问Jenkins的所有内部对象,因此您可以使用它来改变Jenkins的状态。 它类似于Jenkins脚本控制台功能。
因此,您应该选择“System Groovy Script”,以便能够访问Jenkins知道的类(其插件)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.