Reputation: 7729
I am having a sequence of jobs in Jenkins in which few directories are created, in job 1. If any of the subsequent jobs fail, the directories still remain up.
Is there anyway I can trigger a cleanup job(I will be creating this job to make sure it wipes out all directories created in step 1) in Jenkins when any of the subsequent jobs in Jenkins fails?
Upvotes: 1
Views: 2127
Reputation: 21
The ParameterizedPlugin plugin will help to call another job on failure: ParameterizedPlugin
You can add this in Post-Build Actions by follwing the below steps: Post-Build actions=> add post-build action => trigger parameterized build on other projects, Then set the 'Trigger when build is' option to 'Failed' and mention the job you need to call in 'Projects to build' option.
Upvotes: 1
Reputation: 25451
If you use the Workflow feature this becomes straightforward:
try {
stage 'One'
node {
sh 'mkdir /tmp/stuff'
…
}
stage 'Two'
node {
…
}
…
} finally {
stage 'Cleanup'
node {
sh 'rm -rf /tmp/stuff'
}
}
Upvotes: 1
Reputation: 3744
Here is the idea ..!! Configure a trigger for Failure from you subsequent jobs to a new job say "Delete job".
Now from the data point we have there are 10 jobs. if any other intermediate jobs fail then we need to delete the folder created by the jobs for which build is completed.Jobs higher in the the sequence. Now the logic would be each job will have a unique value , set to variable [Job_No] in the order of their execution. i.e say job one variable value would be 1 so on job 10 variable value set to 10. [Job_No=10].
Now say job 6 has failed , we need to delete the all folders created by jobs 1,2,3,4 and 5. We will be using a parameterized trigger plugin to Transfer variable set in jobs to the "Delete Job" to identify which job has invoked the "Delete Job".
Now the logic would be , we have a deletion script in "Delete Job " containing all folders list that should be deleted. Now if Job_NO=6 then the deletion logic would be applicable to N-1 order , which means it will be deleting the folders created by job 1,2,3,4 and 5 in sequence. Here I have considered the folder location is constant. so that the script contains hard coded value.
Upvotes: 0