在 jenkins 工作中,使用当前工作区中的系统 groovy 创建文件 [英] In jenkins job, create file using system groovy in current workspace

查看:22
本文介绍了在 jenkins 工作中,使用当前工作区中的系统 groovy 创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是收集节点详细信息并以特定格式列出它们.我需要将数据写入文件并将其另存为 csv 文件并将其附加为工件.但是我无法使用插件执行系统 ​​Groovy"作为构建步骤在 jenkins 中使用 groovy 脚本创建文件

my task is to collect node details and list them in certail format. I need to write data to a file and save it as csv file and attach it as artifacts. But i am not able to create a file using groovy scripts in the jenkins using plugin "Execute System Groovy" as build step

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
ode_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)

有时我看到人们使用经理"对象,我如何才能访问它?还有关于如何完成任务的任何想法吗?

Some time i see people use "manager" object, how can i get access to it ? Alos any ideas on how to accomplish the task ?

推荐答案

问题Groovy 系统脚本始终运行在 jenkins 主节点中,而工作空间是您的 jenkins 从节点中的文件路径,在您的主节点中不存在.

Problem Groovy system script is always run in jenkins master node, while the workspace is the file path in your jenkins slave node, which doesn't exist in your master node.

您可以通过代码验证

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

它会返回false

如果你不使用slave节点,它会返回true

If you don't use slave node, it will return true

解决方案 由于我们不能使用普通的File,我们必须使用FilePath http://javadoc.jenkins-ci.org/hudson/FilePath.html

Solution As we can't use normal File, we have to use 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
} 

那么它在两种情况下都有效.

Then it works in both case.

这篇关于在 jenkins 工作中,使用当前工作区中的系统 groovy 创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆