In jenkins job, create file using system groovy in current workspace -


my task collect node details , list them in certail format. need write data file , save csv file , attach artifacts. not able create file using groovy scripts in jenkins using plugin "execute system groovy" 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 create file: $targetfile"  if (targetfile.createnewfile()) {     println "successfully created file $targetfile" } else {     println "failed create file $targetfile" } print "deleting ${targetfile.getabsolutepath()} : " println targetfile.delete() 

output obtained

attempting 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     @ java.io.unixfilesystem.createfileexclusively(native method)     @ java.io.file.createnewfile(file.java:947)     @ java_io_file$createnewfile.call(unknown source)     @ org.codehaus.groovy.runtime.callsite.callsitearray.defaultcall(callsitearray.java:42)     @ org.codehaus.groovy.runtime.callsite.abstractcallsite.call(abstractcallsite.java:108)     @ org.codehaus.groovy.runtime.callsite.abstractcallsite.call(abstractcallsite.java:112)     @ script1.run(script1.groovy:13)     @ groovy.lang.groovyshell.evaluate(groovyshell.java:682)     @ groovy.lang.groovyshell.evaluate(groovyshell.java:666)     @ hudson.plugins.groovy.systemgroovy.perform(systemgroovy.java:81)     @ hudson.tasks.buildstepmonitor$1.perform(buildstepmonitor.java:20)     @ hudson.model.abstractbuild$abstractbuildexecution.perform(abstractbuild.java:772)     @ hudson.model.build$buildexecution.build(build.java:199)     @ hudson.model.build$buildexecution.dorun(build.java:160)     @ hudson.model.abstractbuild$abstractbuildexecution.run(abstractbuild.java:535)     @ hudson.model.run.execute(run.java:1732)     @ hudson.model.freestylebuild.run(freestylebuild.java:43)     @ hudson.model.resourcecontroller.execute(resourcecontroller.java:88)     @ hudson.model.executor.run(executor.java:234) 

some time see people use "manager" object, how can access ? alos ideas on how accomplish task ?

problem groovy system script run in jenkins master node, while workspace file path in jenkins slave node, doesn't exist in master node.

you can verify code

thedir = new file(envvars.get('workspace')) println thedir.exists() 

it return false

if don't use slave node, return true

solution can't use normal file, have 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 file }  

then works in both case.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -