Clean up stale releases

You can automate the cleanup of old releases by setting up a template with a cleanup script that will be periodically triggered by criteria that you provide. You can use this procedure in cases where many releases are in a failed state that are older than a specified period of time.

Here is an example configuration that will create a release every 10 minutes that will try to abort all releases with the provided tags that are older than the provided age. You can copy and extend the provided script to suit your situation:

  1. Create a folder named System.
  2. In the System folder, create a template called Cleanup stale releases. Create folder and a template
  3. In the Cleanup stale releases template, select Show > Variables and add the following variables:

    • pageSize of type Number (integer) with a default value of 10
    • tags of type Set
    • age of type Number (integer) with a default value of 1 Create variables
  4. Got to Show > Release flow and add a task Abort stale releases of type Script > Groovy script/Releasefile
  5. Paste following as the groovy script task content:

    import com.xebialabs.xlrelease.api.v1.forms.ReleasesFilters
    import com.xebialabs.xlrelease.api.v1.forms.ReleaseOrderMode
    import com.xebialabs.xlrelease.api.v1.forms.TimeFrame
    import java.util.Date
    import java.time.LocalDate
    
    def age = Integer.valueOf(releaseVariables["age"])
    
    def filter = new ReleasesFilters()
    filter.active = true
    filter.tags = releaseVariables["tags"].asList()
    
    filter.orderBy = ReleaseOrderMode.start_date
    
    if (age > 0) {
        filter.timeFrame = TimeFrame.RANGE
        filter.from = new Date(LocalDate.now().minusYears(10).toEpochDay())
        filter.to = new Date(LocalDate.now().minusMonths(age).toEpochDay())
    }
    
    def releases = []
    def page = 0L
    def pageSize = Long.valueOf(releaseVariables["pageSize"])
    def hasMore = true
    
    def cleanCount = 0
    
    def totalReleases = releaseApi.searchReleases(filter).size()
    
    while (hasMore) {
        releases = releaseApi.searchReleases(filter, page, pageSize)
        hasMore = !releases.isEmpty()
        releases.each {
            releaseApi.abort(it.id, "Aborted by cleanup script")
            cleanCount++
            println("Aborted release ${it.id}")
        }
    }
    
    println ("Cleaned up ${cleanCount} out of total ${totalReleases}")

    Create groovy script task

  6. Go to Show > Properties and complete the Run automated tasks as user and Password properties.

    Run-as properties

  7. Go to Show > Triggers.
  8. Uncheck Allow concurrent triggered releases.

    Triggers

  9. Create a new trigger of type Time: Schedule.

    • Provide a trigger title. For example, Stale releases cleanup
    • Provide a release title. For example, Release cleanup
    • Select Schedule Type to REPEAT
    • Set Schedule to 600 seconds
    • Add the tag cleanup
    • Provide meaningful values for template variables: Page size, Release tags, and Release age

    Triggers Triggers

  10. You can now manage this trigger from Triggers management page.

    Trigger management page