1

I have a pipeline roughly below (largely borrowed from this) and I need to stop and remove it from the history if it aborts. I'm trying to avoid a plugin. Is there an easy way to delete it from the history?

node {
  checkout scm
  result = sh (script: "git log -1 | grep '\\[release\\]'", returnStatus: true) 
  if (result == 0) {
    currentBuild.result = 'ABORTED'
  }
}
Hardrock302
  • 391
  • 5
  • 20
  • There's an obvious problem .. your build is actually still running while evaluating the condition, logging to the build log, so can't be self-deleted. You could write another job that is post-build triggered, parameter the original build log, which it then deletes. – Ian W Nov 24 '20 at 09:03

2 Answers2

2

you can add BUILD HISTORY MANAGER(https://plugins.jenkins.io/build-history-manager/) plugin and do it . then you add this code in pipeline . by this code the history was deleted from 1 to countBuildRemain .

def buildNum = BUILD_ID as Integer
def num = countBuildRemain as Integer
def result = (buildNum) - (num)

options {
  buildDiscarder BuildHistoryManager([[actions: [DeleteBuild()],
  conditions: [BuildResult(matchAborted: true),
  BuildNumberRange(maxBuildNumber: "${result}", minBuildNumber: 1) ]]])
}
0

Use this script, the current job will be deleted. (use with caution, not a best practice)

node {
  checkout scm
  result = sh (script: "git log -1 | grep '\\[release\\]'", returnStatus: true) 
  if (result == 0) {
    currentBuild.result = 'ABORTED'
    Run.fromExternalizableId(currentBuild.externalizableId).delete()
  }
}

or declarative syntax

post {
        success {
        }

      aborted{
          script{
              Run.fromExternalizableId(currentBuild.externalizableId).delete()
          }
      }
}
Avihay Tsayeg
  • 444
  • 4
  • 10