简体   繁体   English

Jenkins中的Groovy脚本:依赖管理

[英]Groovy Script in Jenkins: dependency management

I'm trying to get this Groovy script to run in Jenkins: 我试图让这个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()
}

My goal is to be able to iterate over only the files that have changed and copy those files to another location. 我的目标是能够仅迭代已更改的文件并将这些文件复制到另一个位置。 I'm trying to create a version of the code in Process only changed files that works with TFS. 我正在尝试在Process中创建一个版本的代码, 只更改了与TFS一起使用的文件 I get 2 errors: 我得到2个错误:

unable to resolve class hudson.plugins.tfs.model.ChangeSet.Item 无法解析类hudson.plugins.tfs.model.ChangeSet.Item

unable to resolve class hudson.plugins.tfs.model.ChangeSet 无法解析类hudson.plugins.tfs.model.ChangeSet

This makes sense because Groovy doesn't know where to get the TFS code. 这是有道理的,因为Groovy不知道从哪里获得TFS代码。 I'm using the TFS plugin at https://wiki.jenkins-ci.org/display/JENKINS/Team+Foundation+Server+Plugin . 我在https://wiki.jenkins-ci.org/display/JENKINS/Team+Foundation+Server+Plugin上使用TFS插件。 I've read that Groovy can use Grape for dependency management. 我已经读过Groovy可以使用Grape进行依赖管理。 If the TFS plugin was in http://mvnrepository.com/ then I could use it to let Groovy get the latest code which would look something like this: 如果TFS插件在http://mvnrepository.com/中,那么我可以使用它来让Groovy获得最新的代码,如下所示:

@Grapes(
    @Grab(group='org.apache.maven.scm', module='maven-scm-provider-tfs', version='1.9.4')
)

However, the TFS plugin is not in http://mvnrepository.com/ . 但是,TFS插件不在http://mvnrepository.com/中 The plugin source is in https://github.com/jenkinsci/tfs-plugin . 插件源位于https://github.com/jenkinsci/tfs-plugin So, how can I tell Groovy where to get the code? 那么,我怎么能告诉Groovy在哪里获取代码? Ideally, I wouldn't have to copy the plugin code to my build machine and deal with keeping it up to date. 理想情况下,我不必将插件代码复制到我的构建机器并处理保持最新。 (I'm sure there are errors in my code. I'm just trying to get the dependency management worked out (but feel free to point out coding errors)). (我确定我的代码中存在错误。我只是试图让依赖管理得以解决(但是可以随意指出编码错误))。 Thanks. 谢谢。

As long as you have the plugin installed, you can access plugin classes from groovy running in jenkins. 只要安装了插件,就可以从jenkins中运行的groovy访问插件类。 That being said, it sounds like you may not have the plugin installed!?! 话虽这么说,听起来你可能没有安装插件!?!

Ideally, I wouldn't have to copy the plugin code to my build machine and deal with keeping it up to date. 理想情况下,我不必将插件代码复制到我的构建机器并处理保持最新。

If this is the case, I really don't understand why you would try to use plugin code that isn't installed in jenkins. 如果是这种情况,我真的不明白为什么你会尝试使用未安装在jenkins中的插件代码。

As I see, you're running Groovy script inside a job with "groovy" plugin. 正如我所看到的,你正在使用“groovy”插件在一个作业中运行Groovy脚本。

The plain "Groovy Script" is run in a forked JVM, on the slave where the build is run. 简单的“Groovy脚本”在分叉的JVM中运行,在运行构建的从属设备上运行。 It's the basically the same as running the "groovy" command and pass in the script. 它与运行“groovy”命令并传入脚本基本相同。 So you would need to add classpath entries like: 所以你需要添加类路径条目,如:

groovy -cp <path-to-jars> script.groovy

The system groovy script, runs inside the Jenkins master's JVM. 系统 groovy脚本在Jenkins master的JVM中运行。 Thus it will have access to all the internal objects of Jenkins, so you can use this to alter the state of Jenkins. 因此,它可以访问Jenkins的所有内部对象,因此您可以使用它来改变Jenkins的状态。 It is similar to the Jenkins Script Console functionality. 它类似于Jenkins脚本控制台功能。

So, you should choose "System Groovy Script", in order to have access to classes that Jenkins knows about (its plugins). 因此,您应该选择“System Groovy Script”,以便能够访问Jenkins知道的类(其插件)。

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

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