how to delete a particular file in Jenkins workspace via Jenkins groovy script2019 Community Moderator ElectionHow to restart Jenkins manually?Get absolute path to workspace directory in Jenkins Pipeline pluginJenkins Multipipeline Project with Groovy Pipeline ScriptAccessing Groovy class variables via Jenkins Declarative PipelineCreate new Jenkins jobs using Pipeline Job and Groovy scriptCreate a file with some content using Groovy in Jenkins pipelineGroovy shell script with a sed command in a Jenkins PipelineJenkins Multibranch Pipeline fails because it runs in Groovy SandboxHow to create jenkins pipeline job that will use jenkins file present in github, using groovy script placed inside init.groovy.d directoryExecute groovy script in a jenkins job
How would an energy-based "projectile" blow up a spaceship?
Can Witch Sight see through Mirror Image?
Is this a crown race?
“I had a flat in the centre of town, but I didn’t like living there, so …”
Can I frame a new window without adding jack studs?
Is it appropriate to ask a former professor to order a library book for me through ILL?
How to educate team mate to take screenshots for bugs with out unwanted stuff
How do you make a gun that shoots melee weapons and/or swords?
Book where society has been split into 2 with a wall down the middle where one side embraced high tech whereas other side were totally against tech
Professor forcing me to attend a conference, I can't afford even with 50% funding
What is Tony Stark injecting into himself in Iron Man 3?
Is this Paypal Github SDK reference really a dangerous site?
What is the orbit and expected lifetime of Crew Dragon trunk?
Is there a math expression equivalent to the conditional ternary operator?
Short story about cities being connected by a conveyor belt
Geological Explanation for an Unusually Temperate Northern Mountain Valley
What is the oldest European royal house?
Is the differential, dp, exact or not?
Insult for someone who "doesn't know anything"
Who has more? Ireland or Iceland?
How to write a chaotic neutral protagonist and prevent my readers from thinking they are evil?
Create chunks from an array
What does *dead* mean in *What do you mean, dead?*?
How to recover against Snake as a heavyweight character?
how to delete a particular file in Jenkins workspace via Jenkins groovy script
2019 Community Moderator ElectionHow to restart Jenkins manually?Get absolute path to workspace directory in Jenkins Pipeline pluginJenkins Multipipeline Project with Groovy Pipeline ScriptAccessing Groovy class variables via Jenkins Declarative PipelineCreate new Jenkins jobs using Pipeline Job and Groovy scriptCreate a file with some content using Groovy in Jenkins pipelineGroovy shell script with a sed command in a Jenkins PipelineJenkins Multibranch Pipeline fails because it runs in Groovy SandboxHow to create jenkins pipeline job that will use jenkins file present in github, using groovy script placed inside init.groovy.d directoryExecute groovy script in a jenkins job
i have a Jenkins pipeline, which triggers a Jenkins groovy script via SCM, this script will create a file(if the file is not present) and write else it will update the file and doing some stuffs, this file need to be deleted.
below is the code of creating, writing and updating the file.
node(node_label)
if (fileExists ( file_path+'/'+file_name ) )
def readContent = readFile file_path+'/'+file_name
writeFile file: file_path+'/'+file_name, text: readContent+'rn'+data
else
writeFile file: file_path+'/'+file_name, text:data
after doing some stuffs, i need to delete this file,
i tried deleting this as below, but its not working.
def Delfile = new File(path+'/'+file_name)
Delfile.delete()
jenkins jenkins-pipeline jenkins-groovy
add a comment |
i have a Jenkins pipeline, which triggers a Jenkins groovy script via SCM, this script will create a file(if the file is not present) and write else it will update the file and doing some stuffs, this file need to be deleted.
below is the code of creating, writing and updating the file.
node(node_label)
if (fileExists ( file_path+'/'+file_name ) )
def readContent = readFile file_path+'/'+file_name
writeFile file: file_path+'/'+file_name, text: readContent+'rn'+data
else
writeFile file: file_path+'/'+file_name, text:data
after doing some stuffs, i need to delete this file,
i tried deleting this as below, but its not working.
def Delfile = new File(path+'/'+file_name)
Delfile.delete()
jenkins jenkins-pipeline jenkins-groovy
Hi, check my answer, I am using the same, but only for all jobs which are being loaded, so it should works. So, what is not working in your case? You should to be able to printDelfile(due naming convention, camel-case, should be delFile btw.., its a groovy, "based" on java). so question is what are the values ofpathandfile_name,.. Did you getting any error, or whatever?
– xxxvodnikxxx
2 days ago
add a comment |
i have a Jenkins pipeline, which triggers a Jenkins groovy script via SCM, this script will create a file(if the file is not present) and write else it will update the file and doing some stuffs, this file need to be deleted.
below is the code of creating, writing and updating the file.
node(node_label)
if (fileExists ( file_path+'/'+file_name ) )
def readContent = readFile file_path+'/'+file_name
writeFile file: file_path+'/'+file_name, text: readContent+'rn'+data
else
writeFile file: file_path+'/'+file_name, text:data
after doing some stuffs, i need to delete this file,
i tried deleting this as below, but its not working.
def Delfile = new File(path+'/'+file_name)
Delfile.delete()
jenkins jenkins-pipeline jenkins-groovy
i have a Jenkins pipeline, which triggers a Jenkins groovy script via SCM, this script will create a file(if the file is not present) and write else it will update the file and doing some stuffs, this file need to be deleted.
below is the code of creating, writing and updating the file.
node(node_label)
if (fileExists ( file_path+'/'+file_name ) )
def readContent = readFile file_path+'/'+file_name
writeFile file: file_path+'/'+file_name, text: readContent+'rn'+data
else
writeFile file: file_path+'/'+file_name, text:data
after doing some stuffs, i need to delete this file,
i tried deleting this as below, but its not working.
def Delfile = new File(path+'/'+file_name)
Delfile.delete()
jenkins jenkins-pipeline jenkins-groovy
jenkins jenkins-pipeline jenkins-groovy
edited yesterday
Ann
asked 2 days ago
AnnAnn
83
83
Hi, check my answer, I am using the same, but only for all jobs which are being loaded, so it should works. So, what is not working in your case? You should to be able to printDelfile(due naming convention, camel-case, should be delFile btw.., its a groovy, "based" on java). so question is what are the values ofpathandfile_name,.. Did you getting any error, or whatever?
– xxxvodnikxxx
2 days ago
add a comment |
Hi, check my answer, I am using the same, but only for all jobs which are being loaded, so it should works. So, what is not working in your case? You should to be able to printDelfile(due naming convention, camel-case, should be delFile btw.., its a groovy, "based" on java). so question is what are the values ofpathandfile_name,.. Did you getting any error, or whatever?
– xxxvodnikxxx
2 days ago
Hi, check my answer, I am using the same, but only for all jobs which are being loaded, so it should works. So, what is not working in your case? You should to be able to print
Delfile (due naming convention, camel-case, should be delFile btw.., its a groovy, "based" on java). so question is what are the values of path and file_name ,.. Did you getting any error, or whatever?– xxxvodnikxxx
2 days ago
Hi, check my answer, I am using the same, but only for all jobs which are being loaded, so it should works. So, what is not working in your case? You should to be able to print
Delfile (due naming convention, camel-case, should be delFile btw.., its a groovy, "based" on java). so question is what are the values of path and file_name ,.. Did you getting any error, or whatever?– xxxvodnikxxx
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
I have following for manual workspace cleanup, so as you mentioned it should work as well, check bellow.
I am assuming then you are probably not correctly getting file path
//load jobs
def jobs = Jenkins.instance.getAllItems(Job.class)
//iterate over
for(job in jobs)
//seems like they dont have workspace
if(job instanceof hudson.model.ExternalJob)
continue
String workspace = null
//pipelines dont have workspace
if(job instanceof org.jenkinsci.plugins.workflow.job.WorkflowJob)
println ("workflow job, not cleaning")
continue
try
workspace = job.workspace
catch(Exception e)
//already clean eg.
println ("no workspace, not cleaning")
workspace = null
if(workspace != null)
//creation of the workspace and modules folder
//again not sure, but sometimes was failing due boolean ..
if(workspace instanceof java.lang.Boolean)
println "cant cleanup"
continue
File folder = new File(workspace)
//Check if the Workspace folder really exists
if(folder!=null && folder.exists())
//workspace cleanup
//get files
File[] files = null
try
files=new File(workspace).listFiles().sort()
//println it.name
if(!it.isFile())
it.deleteDir()
else
it.delete()
catch(Exception e)
println "cant clean: " + workspace
else
println "workspace is not existing, not cleaning"
So, the core of the cleanup is:
//get files
File[] files = null
try
files=new File(workspace).listFiles().sort()
//println it.name
if(!it.isFile())
it.deleteDir()
else
it.delete()
catch(Exception e)
println "cant clean: " + workspace
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55022971%2fhow-to-delete-a-particular-file-in-jenkins-workspace-via-jenkins-groovy-script%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I have following for manual workspace cleanup, so as you mentioned it should work as well, check bellow.
I am assuming then you are probably not correctly getting file path
//load jobs
def jobs = Jenkins.instance.getAllItems(Job.class)
//iterate over
for(job in jobs)
//seems like they dont have workspace
if(job instanceof hudson.model.ExternalJob)
continue
String workspace = null
//pipelines dont have workspace
if(job instanceof org.jenkinsci.plugins.workflow.job.WorkflowJob)
println ("workflow job, not cleaning")
continue
try
workspace = job.workspace
catch(Exception e)
//already clean eg.
println ("no workspace, not cleaning")
workspace = null
if(workspace != null)
//creation of the workspace and modules folder
//again not sure, but sometimes was failing due boolean ..
if(workspace instanceof java.lang.Boolean)
println "cant cleanup"
continue
File folder = new File(workspace)
//Check if the Workspace folder really exists
if(folder!=null && folder.exists())
//workspace cleanup
//get files
File[] files = null
try
files=new File(workspace).listFiles().sort()
//println it.name
if(!it.isFile())
it.deleteDir()
else
it.delete()
catch(Exception e)
println "cant clean: " + workspace
else
println "workspace is not existing, not cleaning"
So, the core of the cleanup is:
//get files
File[] files = null
try
files=new File(workspace).listFiles().sort()
//println it.name
if(!it.isFile())
it.deleteDir()
else
it.delete()
catch(Exception e)
println "cant clean: " + workspace
add a comment |
I have following for manual workspace cleanup, so as you mentioned it should work as well, check bellow.
I am assuming then you are probably not correctly getting file path
//load jobs
def jobs = Jenkins.instance.getAllItems(Job.class)
//iterate over
for(job in jobs)
//seems like they dont have workspace
if(job instanceof hudson.model.ExternalJob)
continue
String workspace = null
//pipelines dont have workspace
if(job instanceof org.jenkinsci.plugins.workflow.job.WorkflowJob)
println ("workflow job, not cleaning")
continue
try
workspace = job.workspace
catch(Exception e)
//already clean eg.
println ("no workspace, not cleaning")
workspace = null
if(workspace != null)
//creation of the workspace and modules folder
//again not sure, but sometimes was failing due boolean ..
if(workspace instanceof java.lang.Boolean)
println "cant cleanup"
continue
File folder = new File(workspace)
//Check if the Workspace folder really exists
if(folder!=null && folder.exists())
//workspace cleanup
//get files
File[] files = null
try
files=new File(workspace).listFiles().sort()
//println it.name
if(!it.isFile())
it.deleteDir()
else
it.delete()
catch(Exception e)
println "cant clean: " + workspace
else
println "workspace is not existing, not cleaning"
So, the core of the cleanup is:
//get files
File[] files = null
try
files=new File(workspace).listFiles().sort()
//println it.name
if(!it.isFile())
it.deleteDir()
else
it.delete()
catch(Exception e)
println "cant clean: " + workspace
add a comment |
I have following for manual workspace cleanup, so as you mentioned it should work as well, check bellow.
I am assuming then you are probably not correctly getting file path
//load jobs
def jobs = Jenkins.instance.getAllItems(Job.class)
//iterate over
for(job in jobs)
//seems like they dont have workspace
if(job instanceof hudson.model.ExternalJob)
continue
String workspace = null
//pipelines dont have workspace
if(job instanceof org.jenkinsci.plugins.workflow.job.WorkflowJob)
println ("workflow job, not cleaning")
continue
try
workspace = job.workspace
catch(Exception e)
//already clean eg.
println ("no workspace, not cleaning")
workspace = null
if(workspace != null)
//creation of the workspace and modules folder
//again not sure, but sometimes was failing due boolean ..
if(workspace instanceof java.lang.Boolean)
println "cant cleanup"
continue
File folder = new File(workspace)
//Check if the Workspace folder really exists
if(folder!=null && folder.exists())
//workspace cleanup
//get files
File[] files = null
try
files=new File(workspace).listFiles().sort()
//println it.name
if(!it.isFile())
it.deleteDir()
else
it.delete()
catch(Exception e)
println "cant clean: " + workspace
else
println "workspace is not existing, not cleaning"
So, the core of the cleanup is:
//get files
File[] files = null
try
files=new File(workspace).listFiles().sort()
//println it.name
if(!it.isFile())
it.deleteDir()
else
it.delete()
catch(Exception e)
println "cant clean: " + workspace
I have following for manual workspace cleanup, so as you mentioned it should work as well, check bellow.
I am assuming then you are probably not correctly getting file path
//load jobs
def jobs = Jenkins.instance.getAllItems(Job.class)
//iterate over
for(job in jobs)
//seems like they dont have workspace
if(job instanceof hudson.model.ExternalJob)
continue
String workspace = null
//pipelines dont have workspace
if(job instanceof org.jenkinsci.plugins.workflow.job.WorkflowJob)
println ("workflow job, not cleaning")
continue
try
workspace = job.workspace
catch(Exception e)
//already clean eg.
println ("no workspace, not cleaning")
workspace = null
if(workspace != null)
//creation of the workspace and modules folder
//again not sure, but sometimes was failing due boolean ..
if(workspace instanceof java.lang.Boolean)
println "cant cleanup"
continue
File folder = new File(workspace)
//Check if the Workspace folder really exists
if(folder!=null && folder.exists())
//workspace cleanup
//get files
File[] files = null
try
files=new File(workspace).listFiles().sort()
//println it.name
if(!it.isFile())
it.deleteDir()
else
it.delete()
catch(Exception e)
println "cant clean: " + workspace
else
println "workspace is not existing, not cleaning"
So, the core of the cleanup is:
//get files
File[] files = null
try
files=new File(workspace).listFiles().sort()
//println it.name
if(!it.isFile())
it.deleteDir()
else
it.delete()
catch(Exception e)
println "cant clean: " + workspace
answered 2 days ago
xxxvodnikxxxxxxvodnikxxx
92811027
92811027
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55022971%2fhow-to-delete-a-particular-file-in-jenkins-workspace-via-jenkins-groovy-script%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Hi, check my answer, I am using the same, but only for all jobs which are being loaded, so it should works. So, what is not working in your case? You should to be able to print
Delfile(due naming convention, camel-case, should be delFile btw.., its a groovy, "based" on java). so question is what are the values ofpathandfile_name,.. Did you getting any error, or whatever?– xxxvodnikxxx
2 days ago