繁体   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