Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I delete files in directory using jenkins

In my system, I am downloading new build everyday in 1 folder and then use it for further causes but after running jenkins job I want to delete files in the folder (not workspace) which will delete specific folders from same directory. This will help me downloading new build every time based on different jenkins job running on same machine.

EG:

I am downloading x.x build and then running jenkins job on machine and then if I want to run other job which requires x.y build, it will just see if SOME build is already there in folder. if it is there, it will not download any kit after that. So, now simplest thing I can do is delete x.x after every jenkins run (post build ) so it will download x.y next time..

Please help. Thanks in advance

like image 644
JAVAPYTHON Avatar asked Nov 14 '14 01:11

JAVAPYTHON


People also ask

How do I delete a file in Jenkins?

Navigate to /scriptApproval/ (Manage Jenkins > In-process Script Approval) and approve the script. File. delete works on master.

How do I delete files from a directory?

Locate the item you want to delete, highlight it by left-clicking the file or folder with your mouse once, and press the Delete key.

How do I delete a folder in Jenkins?

Delete views and folders - Jenkins Tutorial To delete a view, you would start by entering the view. And let's look at the build view for example. On the left-hand side, there is an option to delete view. So I'll click that and I'm prompted to confirm the deletion.

How do I delete multiple builds in Jenkins?

There's a lot you can break using the script console. Delete the builds' folders from disk (by default in $JENKINS_HOME/jobs/JobName/builds/ , using the start time stamp as folder name) and restart Jenkins, or Reload Configuration From Disk.


2 Answers

You can delete the whole folder using this syntax to delete a folder called bin:

stage('Setup') {
    steps {
        dir ('bin') {
            deleteDir()
        }
    }
}
like image 75
katrash Avatar answered Oct 19 '22 13:10

katrash


If my understanding is right, consider my below assumptions

If your jenkins is running on a Unix server, then you can configure a post build step as suggested by nerdwaller above

  1. In the job configuration, in the build step, select the option "execute unix command"
  2. In the box for the shell script, you can use rm -rf <<directoryname>>

Else, if your jenkins is running on a windows server, then select "execute batch command" from the build step and give the appropriate command like rmdir /Q /S nonemptydir

However, my best approach would be to use a platform independent tool like ant to delete the folders using Ant Delete Task and it can be configured similarly like the above two approaches instead selecting "invoke ant" in the build step/ post build step.

This will help you to achieve what you need.

like image 33
Raghav Vaidhyanathan Avatar answered Oct 19 '22 14:10

Raghav Vaidhyanathan