Toggle the Retain Indefinitely flag from Release Management

Problem:

We employ continuous delivery with Release Management and our drop server has run out of disk space.

Solution:

Use the attached PowerShell scripts to toggle the Retain Indefinitely flag during your release.

Explanation:

The default behavior of Release Management is to set the Retain Indefinitely flag of a build used by Release Management.  Setting the Retain Indefinitely flag allows the build to ignore the Retention Policy set for the build definition so that it is never deleted.   I can understand the reasoning behind this decision, however, if a company is employing continuous delivery you will soon run out of disk space on your drop server.  Customers have been asking how to turn this feature off.  Release Management does not provide a way to disable this feature so I created a pair PowerShell scripts you can use to toggle the Retain Indefinitely flag.  If you are using the agent based deployment you can add the AgentBasedBuild PowerShell script as a custom tool. If you are using the vNext based deployment you can simply execute the vNextBasedBuild PowerShell using the PS/DSC action.  When using a vNext based deployment the $tfsUrl, $teamProject and $buildDefinition values are automatically passed to all PowerShell scripts. When using the agent based solution you will need to add those as parameters when you add the PowerShell as a custom tool.  You can then use the Helper Variables provided by Release Management to pass in the correct values.  You can see an example of adding an agent based custom tool here.

vNextBasedBuild.ps1

function set-RetainIndefinitely

{

    [CmdletBinding()]

       param(

    [bool] $retainIndefinitely

    )

    # The machine you use to execute this PowerShell must have Team Explorer installed

    # so that we can load the needed assemblies.

    # The [void] stops the GAC messages from being output to screen.

    Write-Verbose "Loading assemblies"

    [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")

    [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Build.Client")

    # Connect to TFS

    Write-Verbose "Connecting to TFS"

    $teamProjectCollection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($tfsUrl)

    # Get the build service

    $buildServer = $teamProjectCollection.GetService([type]"Microsoft.TeamFoundation.Build.Client.IBuildServer")

    # Find the build used for this release

    $buildSpec = $buildServer.CreateBuildDetailSpec($teamProject, $buildDefinition)

    # Don't query any additional information i.e. associated work items, associated changesets, etc.

    $buildSpec.InformationTypes  = $null

    # Use the build number to find the build

    $buildSpec.BuildNumber = $buildNumber

    # Search for the build

    $results = $buildServer.QueryBuilds($buildSpec)

    # Make sure we only found a single build

    if($results.Builds.Count -eq 1)

    {

        Write-Verbose "Build Found"

        $build = $results.Builds[0]

        $build.KeepForever = $retainIndefinitely

        $build.Save()

        Write-Verbose "Retain Indefinitely flag set to $retainIndefinitely"

    }

}


vNextBasedBuild.ps1 (1.6KB)

AgentBasedBuild.ps1 (1.9KB)

Pingbacks and trackbacks (1)+

Comments are closed