簡體   English   中英

在jenkins作業中,使用當前工作空間中的系統groovy創建文件

[英]In jenkins job, create file using system groovy in current workspace

我的任務是收集節點詳細信息並以certail格式列出。 我需要將數據寫入文件並將其保存為csv文件並將其作為工件附加。 但是我無法使用插件“Execute System Groovy”作為構建步驟在jenkins中使用groovy腳本創建文件

import jenkins.model.Jenkins
import hudson.model.User
import hudson.security.Permission
import hudson.EnvVars

EnvVars envVars = build.getEnvironment(listener);

filename = envVars.get('WORKSPACE') + "\\node_details.txt";
//filename = "${manager.build.workspace.remote}" + "\\node_details.txt"
targetFile = new File(filename);
println "attempting to create file: $targetFile"

if (targetFile.createNewFile()) {
    println "Successfully created file $targetFile"
} else {
    println "Failed to create file $targetFile"
}
print "Deleting ${targetFile.getAbsolutePath()} : "
println targetFile.delete()

獲得輸出

attempting to create file: /home/jenkins/server-name/workspace/GET_NODE_DETAILS\node_details.txt
FATAL: No such file or directory
java.io.IOException: No such file or directory
    at java.io.UnixFileSystem.createFileExclusively(Native Method)
    at java.io.File.createNewFile(File.java:947)
    at java_io_File$createNewFile.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
    at Script1.run(Script1.groovy:13)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:682)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:666)
    at hudson.plugins.groovy.SystemGroovy.perform(SystemGroovy.java:81)
    at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
    at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:772)
    at hudson.model.Build$BuildExecution.build(Build.java:199)
    at hudson.model.Build$BuildExecution.doRun(Build.java:160)
    at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:535)
    at hudson.model.Run.execute(Run.java:1732)
    at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
    at hudson.model.ResourceController.execute(ResourceController.java:88)
    at hudson.model.Executor.run(Executor.java:234)

有一段時間我看到人們使用“經理”對象,我怎樣才能訪問它? Alos關於如何完成任務的任何想法?

問題 Groovy系統腳本始終在jenkins主節點中運行,而工作空間是jenkins從節點中的文件路徑,主節點中不存在該路徑。

您可以通過代碼進行驗證

theDir = new File(envVars.get('WORKSPACE'))
println theDir.exists()

它會返回false

如果不使用從屬節點,則返回true

解決方案由於我們無法使用普通File ,因此我們必須使用FilePath http://javadoc.jenkins-ci.org/hudson/FilePath.html

if(build.workspace.isRemote())
{
    channel = build.workspace.channel;
    fp = new FilePath(channel, build.workspace.toString() + "/node_details.txt")
} else {
    fp = new FilePath(new File(build.workspace.toString() + "/node_details.txt"))
}

if(fp != null)
{
    fp.write("test data", null); //writing to file
} 

然后它適用於這兩種情況。

我懷疑錯誤是由路徑格式引起的,你可以嘗試下面:

更改

filename = envVars.get('WORKSPACE') + "\\node_details.txt";

filename = envVars.get('WORKSPACE') + "/node_details.txt";

因為當我在我的本地jenkins服務器上嘗試時,我成功執行了它。

在此輸入圖像描述

在此輸入圖像描述

回答@Larry Cai介紹了一個部分,用於從System Groovy Script(因為它在主節點上運行)向從節點寫入文件。

我回答的部分是“有時我看到人們使用”manager“對象,我怎樣才能訪問它”這是Post Build Groovy Script中已經可用的對象,用於訪問環境變量,Build Status等很多內容,構建顯示名稱等

引用自https://wiki.jenkins-ci.org/display/JENKINS/Groovy+Postbuild+Plugin

“groovy腳本可以使用變量管理器,它提供了各種方法來裝飾你的構建。這些方法可以分為白名單方法和非白名單方法。”

要訪問它,我們可以在post build groovy腳本中直接調用它。 例如

manager.build.setDescription(“custom description”)manager.addShortText(“在這里添加你的消息”)

此處記錄了管理器對象上可用的所有方法。

https://wiki.jenkins-ci.org/display/JENKINS/Groovy+Postbuild+Plugin#GroovyPostbuildPlugin-Whitelistedmethods

管理器對象不可用,具體取決於如何調用groovy。 例如在“執行系統groovy腳本”中。

你可以在jenkins GroovyPostBuild插件API中找到BadgeManager類: https ://javadoc.jenkins.io/plugin/groovy-postbuild/org/jvnet/hudson/plugins/groovypostbuild/GroovyPostbuildRecorder.BadgeManager.html#addShortText-java.lang 。串-

解答:導入GroovyPostBuild插件並創建一個新的管理器對象。 例如,這里有一個“Execute System Groovy Script”的作業創建一個manager對象並調用addShortText方法:

// java.lang.Object
// org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildRecorder.BadgeManager
// Constructor and Description
// BadgeManager(hudson.model.Run<?,?> build, hudson.model.TaskListener listener, hudson.model.Result scriptFailureResult) 

import hudson.model.*
import org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildAction

def build = Thread.currentThread().executable

manager = new org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildRecorder.BadgeManager(build, null, null)
manager.addShortText("MANAGER TEST", "black", "limegreen", "0px", "white")

這個問題提供了一個提示:在這里看到一個近乎可行的答案: 在jenkins工作中,使用當前工作空間中的系統groovy創建文件 org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildActionbuild.getActions().add(GroovyPostbuildAction.createShortText(text, "black", "limegreen", "0px", "white"));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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