Reputation: 2421
As part of post build action in jenkins, I want to delete a job if it has failed with a particular exit code. Logic here is:
Reason being not having failed jobs every 5 mins (build schedule is set to every 5 mins) I have been looking for a way to do this. So, far I have got below 2 ways working via Manage Jenkins -> Script Console. But the same code fails when I add it to current jenkins job.
Working under Manage Jenkins -> Script Console
import jenkins.model.Jenkins
import hudson.model.*
def jobName = "renum_jenkins_test"
def job = Jenkins.instance.getItem(jobName)
job.getBuilds().each { it.delete() }
job.nextBuildNumber = 26
job.save()
As well as:
import jenkins.model.Jenkins
def jobName = 'renum_jenkins_test'
Jenkins.instance.getItemByFullName(jobName).builds.findAll { it.result
== Result.FAILURE}.each { it.delete() }
Error message when it runs as Post Build Step in Jenkins Job Configure:
java.nio.file.FileSystemException: /auto/system-dir/idevtest/jenkins/conf/jobs/renum_jenkins_test/builds/.30/.nfs000000000d587f2900000049: Device or resource busy
at sun.nio.fs.UnixException.translateToIOException(UnixException.java:91)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
at sun.nio.fs.UnixFileSystemProvider.implDelete(UnixFileSystemProvider.java:243)
at sun.nio.fs.AbstractFileSystemProvider.delete(AbstractFileSystemProvider.java:103)
at java.nio.file.Files.delete(Files.java:1077)
at sun.reflect.GeneratedMethodAccessor1449.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at hudson.Util.deleteFile(Util.java:247)
at hudson.Util.deleteRecursive(Util.java:310)
and the error in Failed log says:
No such file: /auto/system-dir/idevtest/jenkins/conf/jobs/renum_jenkins_test/builds/30/log
Any pointers how can I get away with this error and also determine the status/result of current job?
Upvotes: 0
Views: 3882
Reputation: 12255
Easy way is to change build status in post build step:
pipeline {
agent any
environment {
skip = "false"
}
stages {
stage('MySQL') {
steps {
script{
echo currentBuild.currentResult
skip = "true"
echo "skip: " + skip
}
}
}
stage('Set Build Status') {
when {
equals expected: "true", actual: skip
}
steps {
script{
echo currentBuild.currentResult
currentBuild.result = 'ABORTED'
currentBuild.displayName = "#${env.BUILD_NUMBER} skipped"
currentBuild.description = "any description"
}
}
}
}
post {
aborted {
deleteDir()
}
}
}
If you want to delete, you have to create another job with buildNumber parameter, that will be triggered by MySQL job on fail in post build step and delete last build. This will not cause the Device or resource busy
error.
Upvotes: 1