Jenkinsfile clear gradle cache and retry on failure
Current scenario: We have configured CI pipeline and running a jenkinsfile.groovy script for a gradle build. The pipeline is working fine and now we are planning to enable an error handling functionality into the groovy script.
Reason of this functionality: Sometimes, the gradle build is unable to download all the dependencies due to the network glitch or could be any reason. So, as a part of this functionality, we wants to add the following steps in the existing pipeline:- throw an error message of the failure
- clear the cache from the gradle home directory
- rebuild failed jenkins job
- stop the existing failed build Following code is included in the pipeline to enable 开发者_开发技巧the functionality:
catch (err) {
echo "Build failed for ${env.JOB_NAME}, clearing cache from the $GRADLE_HOME and retrying"
sh "rm -rf $GRADLE_HOME"
build "${env.JOB_NAME}"
throw err
}
Testing of new functionality: We are now in testing phase of the above code which is working fine, and three steps of the functionality is achieved, however, throw err (last step) doesn't stop the existing failed build after triggering same build again and keeps waiting for the build to complete.
Success of the new functionality in the existing pipeline:
- throw an error message of the failure : ACHIEVED
- clear the cache from the gradle home directory : ACHIEVED
- rebuild failed jenkins job : ACHIEVED
- stop the existing failed build : NOT ACHIEVED this fourth and the last step is not working as expected and doesn't stop the failed build. The failed build keeps waiting for the next job to complete. We have tried adding exit 1 as well but no luck. Any help would be really appreciable. TIA
精彩评论