【问题标题】:Groovy Script in Jenkins: dependency managementJenkins 中的 Groovy 脚本:依赖管理
【发布时间】:2016-09-21 23:17:39
【问题描述】:

我正在尝试让这个 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 only changed files 中创建与 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 tfs


    【解决方案1】:

    只要你安装了插件,你就可以从运行在 jenkins 中的 groovy 访问插件类。话虽如此,听起来您可能没有安装插件!?!

    理想情况下,我不必将插件代码复制到我的构建机器并处理使其保持最新状态。

    如果是这样的话,我真的不明白你为什么要尝试使用 jenkins 中没有安装的插件代码。

    【讨论】:

    • 我确实安装了插件。也许我的导入语句是错误的?导入 hudson.plugins.tfs.model.ChangeSet;导入 hudson.plugins.tfs.model.ChangeSet.Item;
    • 我检查了 tfs api,这些导入看起来是正确的。必须是类路径问题。你是如何运行脚本的?来自工作?您是否作为系统 groovy 脚本运行?当您尝试从脚本控制台运行时会发生什么?从脚本控制台进行测试时,您可以获得类似Jenkins.instance.getJob("jobName").getBuild("1") 的构建,其中getBuild 中的数字参数是该作业的有效构建号。
    【解决方案2】:

    如我所见,您正在使用“groovy”插件在作业中运行 Groovy 脚本。

    普通的“Groovy 脚本”在一个分叉的 JVM 中运行,在运行构建的从属服务器上。这与运行“groovy”命令并传入脚本基本相同。因此,您需要添加类路径条目,例如:

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

    system groovy 脚本,在 Jenkins master 的 JVM 中运行。因此它将可以访问 Jenkins 的所有内部对象,因此您可以使用它来更改 Jenkins 的状态。它类似于 Jenkins 脚本控制台功能。

    因此,您应该选择“System Groovy Script”,以便访问 Jenkins 知道的类(其插件)。

    【讨论】:

      猜你喜欢
      • 2018-02-26
      • 1970-01-01
      • 1970-01-01
      • 2015-05-11
      • 2015-10-13
      • 2016-11-02
      • 2018-10-12
      • 2018-04-01
      • 1970-01-01
      相关资源
      最近更新 更多