Jenkins - kill process before delete workspace action starts - jenkins

I am having a Jenkins job that runs Nunit tests on remote machine.
I am using Jenkins's Workspace Cleanup Plugin pluggin (https://wiki.jenkins-ci.org/display/JENKINS/Workspace+Cleanup+Plugin) to clean my workspace.
the problem is that I want to task kill some process on my machine (because otherwise I could not delete the workspace - some files will be in use and threfore could not be deleted) and I want to do it before the delete action takes place (it is always the first action on the job).
I know that there is an option in the pluggin- "External Deletion Command" - but this runs the command on all the files in the workspace where as I need it to run only once (not on a the sepsific workspace files - i.e. only this command: "c:/workspace/taskill nunit")
is there a way to do so?
Thanks

If I can suggest a different approach to use an app called LockHunter which has an API to unlock and delete your workspace. It's much more "sergical" than removing a random task and hope it's the one you meant to.
You can trigger it from command line using "run before SCM" and it'll handle the deletion and unblocking of your specific workspace.
You can also use:
"cmd /c wmic /INTERACTIVE:OFF Path win32_process Where \\"CommandLine Like '%workspace%'\\" call terminate"
Where %workspace% is your current workspace. This will go over all the tasks that are currently running and check the command line path, then it'll call terminate for anything it found.
Good luck!

Related

How to delete a build from Jenkins job workspace

I wonder if it is possible to remove only one build (including artifacts) from job workspace.
I tried to "Delete Build" in Build History but all it does is remove build reference from Build History table. I know I can ssh to a server and delete files from the command line but I am looking for a way to do it from Jenkins web interface.
After installing Workspace Cleanup Plugin I am able to wipe out current workspace but I want to keep my other builds in the workspace.
In your Jenkins instance, to be able to have folder/per build - set flag "Use custom workspace" in your job's settings. Here is a brief help info from the setting description:
For each job on Jenkins, Jenkins allocates a unique "workspace directory."
This is the directory where the code is checked out and builds happen.
Normally you should let Jenkins allocate and clean up workspace directories,
but in several situations this is problematic, and in such case, this option
lets you specify the workspace location manually.
One such situation is where paths are hard-coded and the code needs to be
built on a specific location. While there's no doubt that such a build is
not ideal, this option allows you to get going in such a situation.
...
And your custom directory path would look like this:
workspace\$JOB_NAME\$BUILD_NUMBER ~> workspace\my-job-name\123
where $JOB_NAME will be "my-job-name" and $BUILD_NUMBER is the build number, eq. "123".
There is one nasty problem with this approach and this is why I wouldn't recommend to use it - Jenkins will not be able to reclaim disk space for outdated builds. You would have to handle cleanup of outdated builds manually and it is a lot of hassle.
Alternative approach, that gives you more control, tools and is able to keep disk space usage under control (without your supervision) is to use default workspace settings and archive your build output (files, original source code, libraries and etc.) as a post-build action. Very-very handy and gives you access to a whole bunch of great tools like, Copy Artifact Plugin or ArtifactDeployer Plugin in other jobs.
Hope that info helps you make a decision that fits your needs best.
I also use "General/Advanced/Use custom workspace" (as in #pabloduo's answer) on a Windows machine with something like:
C:\${JOB_NAME}\${BUILD_NUMBER}
Just wanted to add a solution for getting rid of the build job's workspaces.
I use Groovy Events Listener Plugin for this.
Using the plug-in's standard configuration I just use the following Groovy script:
if (event == Event.JOB_DELETED){
new File(env.WORKSPACE).deleteDir()
}
And now the custom workspace is deleted when the build job is deleted.
Just be aware that this would also delete non-custom workspaces (because the event is triggered for all jobs on your Jenkins server).

Jenkins doesn't allow file moving in post build task?

Using Jenkins, in a post-build task, I execute batch commands. I had some file to move there using command move in the script. The files to be moved is the whole workspace, I loop through the files.
However, when Jenkins execute the move command in post-build task, it says access is denied when trying to move the file from command line. I guess Jenkins probably protects the files at this point. This code execution really needs to be there for a special task.
My assumption is that in post build task Jenkins locks the workspace, but that prevents from doing some things. Is there a way to still move file in the post-build task?
I realized that my problem was the way I was moving file, they were locked by another command. It had nothing to do with Jenkins.
See how to move folders with a loop over folders (in batch)?

How can I use Jenkins to detect the presence of a file on an SFTP server?

I want to use Jenkins to monitor an SFTP site, and fire off a build when a given file type appears on that site.
How can I do this?
Ok, in that case you should have 2 jobs.
First job - running every N minutes with a bash script as a build step:
`wget ftp://login:password#ftp.example.org/file.txt
Then you should have https://wiki.jenkins-ci.org/display/JENKINS/Run+Condition+Plugin , which runs on condition when file "file.txt" (downloaded or not downloaded previously) exist
Afte that you can trigger your next job in case if file exist (or do anything else)
Like in the previous answer I would use two jobs but instead of bash scripts I would use python and and sftp since it makes dealing with ssh a bit easier.
https://pypi.python.org/pypi/pysftp

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
You can delete the whole folder using this syntax to delete a folder called bin:
stage('Setup') {
steps {
dir ('bin') {
deleteDir()
}
}
}
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
In the job configuration, in the build step, select the option "execute unix command"
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.

Jenkins: how can I clean the workspace except the vagrant directory?

I have the following setup:
I use the workspace cleanup plugin in Jenkins to clean my workspace before each build. During my build-process I also trigger a vagrant up to setup a VM for phpunit tests:
$ vagrant up
$ ./runtest.sh
$ vagrant suspend
Now when I re-build the project, the VM gets build as a new one instead of just resuming the previous one. I guess this is because of the cleanup plugin removing the .vagrant-directory, therefore making Vagrant think it should build a new machine instead of just resuming the previous one.
Now I have configured the plugin to exclude the following patterns and I have the 'Apply pattern also on directories'-checkbox also checked:
**/*.vagrant
.vagrant
.vagrant/
./.vagrant
./.vagrant/
But still the .vagrant-directory gets deleted from the workspace on each new build, spawning a brand new VM each time...
Does anyone know how I can exclude the .vagrant-directory from the workspace cleanup plugin?
Turned out I was having a logical error. What I did as first task was checkout and hard-reset a specific branch to the master. So it was this step that actually deleted the .vagrant-directory after the cleanup-task did what it was expected to do.
According to the plugin documentation, it provides include and exclude patterns. You need to change the dropdown to exclude pattern (marked with arrow #2 in the diagram on plugin page), and type .vagrant in the pattern. Should probably also click "apply to directories"

Resources