Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. import hudson.model.FreeStyleBuild
  2. import hudson.model.Result
  3. import hudson.model.Run
  4. import jenkins.model.Jenkins
  5. import org.jenkinsci.plugins.workflow.job.WorkflowRun
  6. import org.jenkinsci.plugins.workflow.support.steps.StageStepExecution
  7.  
  8. if(!binding.hasVariable('dryRun')) {
  9. dryRun = true
  10. }
  11. if(!binding.hasVariable('projectFullName')) {
  12. projectFullName = 'project/full/name'
  13. }
  14.  
  15. //type check user defined parameters/bindings
  16. if(!(dryRun instanceof Boolean)) {
  17. throw new Exception('PARAMETER ERROR: dryRun must be a boolean.')
  18. }
  19. if(!(projectFullName instanceof String)) {
  20. throw new Exception('PARAMETER ERROR: projectFullName must be a string.')
  21. }
  22.  
  23. Jenkins.instance.getItemByFullName(projectFullName).builds.each { Run item ->
  24. if(item.isBuilding()) {
  25. if(item instanceof WorkflowRun) {
  26. WorkflowRun run = (WorkflowRun) item
  27. if(!dryRun) {
  28. //hard kill
  29. run.doKill()
  30. //release pipeline concurrency locks
  31. StageStepExecution.exit(run)
  32. }
  33. println "Killed ${run}"
  34. } else if(item instanceof FreeStyleBuild) {
  35. FreeStyleBuild run = (FreeStyleBuild) item
  36. if(!dryRun) {
  37. run.executor.interrupt(Result.ABORTED)
  38. }
  39. println "Killed ${run}"
  40. } else {
  41. println "WARNING: Don't know how to handle ${item.class}"
  42. }
  43. }
  44. }.each{ build ->
  45. if(build.isBuilding()) {
  46. build.doKill()
  47. println "killed ${build}"
  48. }
  49. }
  50.  
  51. //null so no result shows up
  52. null
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement