J'essaie de créer un fichier appelé groovy1.txt avec le contenu "Travailler avec des fichiers à la manière de Groovy est facile."
Remarque: je ne souhaite pas utiliser le shell pour créer ce fichier, je souhaite plutôt utiliser Groovy pour y parvenir.
J'ai le script suivant dans mon pipeline Jenkins.
node {
def file1 = new File('groovy1.txt')
file1.write 'Working with files the Groovy way is easy.\n'
sh 'ls -l'
// Expecting the file groovy1.txt should present with the content mentioned above
}
Mais il génère une erreur FileNotFound (autorisation refusée) comme ci-dessous
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
Le pipeline Jenkins fournit une étape writeFile
qui peut être utilisée pour écrire un fichier dans l'espace de travail du travail.
Regardez l'exemple suivant:
node {
writeFile file: 'groovy1.txt', text: 'Working with files the Groovy way is easy.'
sh 'ls -l groovy1.txt'
sh 'cat groovy1.txt'
}
L'exécution de ces scripts de pipeline génère la sortie suivante:
[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
Utilisation de Java
File
Comme Jon S mentionné dans le commentaire, Java
new File("${env.WORKSPACE}/groovy1.txt")
) fonctionnera uniquement si votre étape de noeud est exécutée sur un noeud maître - si elle est exécutée sur un noeud esclave, le code de votre pipeline échouera. Vous pouvez vérifier le thread de débordement de pile suivant pour plus d'informations: