问题描述
我正在尝试创建一个名为 groovy1.txt 的文件,其内容为以 Groovy 方式处理文件很容易."
注意:我不想用shell来创建这个文件,而是想用Groovy来实现.
我的 Jenkins 管道中有以下脚本.
节点{def file1 = new File('groovy1.txt')file1.write '以 Groovy 方式处理文件很容易.
'sh 'ls -l'//期望文件 groovy1.txt 应该与上面提到的内容一起出现}
但它抛出如下 FileNotFound(权限被拒绝)错误
java.io.FileNotFoundException: groovy1.txt(权限被拒绝)在 java.io.FileOutputStream.open0(本机方法)在 java.io.FileOutputStream.open(FileOutputStream.java:270)在 java.io.FileOutputStream.(FileOutputStream.java:213)在 java.io.FileOutputStream.(FileOutputStream.java:162)在 java.io.FileWriter.(FileWriter.java:90)在 org.codehaus.groovy.runtime.ResourceGroovyMethods.write(ResourceGroovyMethods.java:740)在 org.codehaus.groovy.runtime.dgm$1035.doMethodInvoke(来源不明)在 groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213)在 groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)在 org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:47)在 org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)在 org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)在 org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:157)在 org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)在 org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:104)在 org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:155)在 org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:159)在 com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)在 WorkflowScript.run(WorkflowScript:3)在 ___cps.transform___(本机方法)在 com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:57)在 com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:109)在 com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:82)在 sun.reflect.GeneratedMethodAccessor257.invoke(来源不明)在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)在 java.lang.reflect.Method.invoke(Method.java:498)在 com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)在 com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)在 com.cloudbees.groovy.cps.Next.step(Next.java:83)在 com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)在 com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)在 org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:122)在 org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:261)在 com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)在 org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$101(SandboxContinuable.java:34)在 org.jenkinsci.plugins.workflow.cps.SandboxContinuable.lambda$run0$0(SandboxContinuable.java:59)在 org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108)在 org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:58)在 org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:174)在 org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:332)在 org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$200(CpsThreadGroup.java:83)在 org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:244)在 org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:232)在 org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64)在 java.util.concurrent.FutureTask.run(FutureTask.java:266)在 hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:131)在 jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)在 jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:59)在 java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)在 java.util.concurrent.FutureTask.run(FutureTask.java:266)在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)在 java.lang.Thread.run(Thread.java:748)完成:失败
Jenkins Pipeline 提供 writeFile
步骤,可用于在作业的工作空间内写入文件.
看看下面的例子:
节点{writeFile file: 'groovy1.txt', text: '以 Groovy 方式处理文件很容易.sh 'ls -l groovy1.txt'sh 'cat groovy1.txt'}
运行此管道脚本会生成以下输出:
[Pipeline] 节点在/var/jenkins_home/workspace/test-pipeline 中的 Jenkins 上运行[管道] {[管道] writeFile[管道] sh[test-pipeline] 运行 shell 脚本+ ls -l groovy1.txt-rw-r--r-- 1 jenkins jenkins 42 Jul 8 16:38 groovy1.txt[管道] sh[test-pipeline] 运行 shell 脚本+ 猫 groovy1.txt使用 Groovy 方式处理文件很容易.[Pipeline] }[管道]//节点[管道] 管道结束完成:成功
使用Java 文件
正如评论中提到的 Jon S,Java new File("${env.WORKSPACE}/groovy1.txt")
仅,如果您的节点步骤在主节点上执行 - 如果它在从节点上执行,那么您的管道代码将失败.您可以查看以下 Stack Overflow 线程以获取更多信息:
在詹金斯工作, 在当前工作区使用系统 groovy 创建文件
I am trying to create a file called groovy1.txt with the content "Working with files the Groovy way is easy."
Note: I don't want to use the shell to create this file, instead I want to use Groovy to achieve this.
I have following script in my Jenkins pipeline.
node {
def file1 = new File('groovy1.txt')
file1.write 'Working with files the Groovy way is easy.
'
sh 'ls -l'
// Expecting the file groovy1.txt should present with the content mentioned above
}
But it is throwing FileNotFound (permission denied) error as below
java.io.FileNotFoundException: groovy1.txt (Permission denied)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(FileOutputStream.java:270)
at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
at java.io.FileOutputStream.<init>(FileOutputStream.java:162)
at java.io.FileWriter.<init>(FileWriter.java:90)
at org.codehaus.groovy.runtime.ResourceGroovyMethods.write(ResourceGroovyMethods.java:740)
at org.codehaus.groovy.runtime.dgm$1035.doMethodInvoke(Unknown Source)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:47)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:157)
at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:104)
at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:155)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:159)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)
at WorkflowScript.run(WorkflowScript:3)
at ___cps.transform___(Native Method)
at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:57)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:109)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:82)
at sun.reflect.GeneratedMethodAccessor257.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
at com.cloudbees.groovy.cps.Next.step(Next.java:83)
at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:122)
at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:261)
at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$101(SandboxContinuable.java:34)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.lambda$run0$0(SandboxContinuable.java:59)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:58)
at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:174)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:332)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$200(CpsThreadGroup.java:83)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:244)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:232)
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:131)
at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:59)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Finished: FAILURE
Jenkins Pipeline provides writeFile
step that can be used to write a file inside job's workspace.
Take a look at following example:
node {
writeFile file: 'groovy1.txt', text: 'Working with files the Groovy way is easy.'
sh 'ls -l groovy1.txt'
sh 'cat groovy1.txt'
}
Running this pipeline scripts generates following output:
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/test-pipeline
[Pipeline] {
[Pipeline] writeFile
[Pipeline] sh
[test-pipeline] Running shell script
+ ls -l groovy1.txt
-rw-r--r-- 1 jenkins jenkins 42 Jul 8 16:38 groovy1.txt
[Pipeline] sh
[test-pipeline] Running shell script
+ cat groovy1.txt
Working with files the Groovy way is easy.[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
这篇关于在 Jenkins 管道中使用 Groovy 创建一个包含一些内容的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!