簡體   English   中英

Jenkins中的Groovy腳本:依賴管理

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM