Using Octopus.Client and PowerShell to change Builtin Package Repository Retention Policy

Hi,

I’m trying to use Octopus.Client.dll with PowerShell to change the builtin package repository retention policy of multiple octopus instances but I can find the right way. I’ve been digging through the documentation and the dll but can’t find anything. I can see the model BuiltInFeedResource but the Octopus.Client.Repositories.BuiltInPackageRepositoryRepository does not have the option to modify. Is there any way I can do this with PowerShell?

Hi Alex,

Thanks for getting in touch. I had to dig around for it as well. You can do this by editing the feed:

var feed = (BuiltInFeedResource) repository.Feeds.Get("feeds-builtin");
feed.DeleteUnreleasedPackagesAfterDays = 100;
repository.Feeds.Modify(feed);

Note if you are on 2019.x or later and are using Spaces, the built in feeds for spaces other than the first will have a different id.

Hi Robert,

Thanks for your response. However, I wasn’t able to make it work. I’m getting an invalid cast exception.

System.InvalidCastException: ‘Unable to cast object of type ‘Octopus.Client.Model.NuGetFeedResource’ to type ‘Octopus.Client.Model.BuiltInFeedResource’.’

Which version of Octopus Server and Octopus.Client do you have?

Could you also share the code?

Here’s my code. I’m using Octopus.Client 6.1.1.0

using Octopus.Client;
using Octopus.Client.Model;

namespace TestOctopus1
{
    class Program
    {
        static void Main()
        {
            string octopusUri = "https://octopus.redactedinfo.com/redactedinfo";
            string octopusUser = "redactedinfo";
            string octopusPass = "redactedinfo";
            var endpoint = new OctopusServerEndpoint(octopusUri);
            var repository = new OctopusRepository(endpoint);
            var loginCommand = new LoginCommand();
            loginCommand.Username = octopusUser;
            loginCommand.Password = octopusPass;
            repository.Users.SignIn(loginCommand);
            var feed = (BuiltInFeedResource) repository.Feeds.Get("feeds-builtin");
            feed.DeleteUnreleasedPackagesAfterDays = 1;
            repository.Feeds.Modify(feed);
        }
    }
}

Which version of Octopus Server?

Its Octopus v2018.6.5.

Ah right. In that case you will need to use Octopus.Client 4.47.0 (see our compatibility page).

The code to make the change is:

var config = repository.Configuration.Get<BuiltInRepositoryConfigurationResource>();
config.DeleteUnreleasedPackagesAfterDays = 100;
repository.Configuration.Modify(config);

Thanks for providing the link for the compatibility page. I have changed the Octopus.Client version to 4.47.0.

I have also tried your suggestion but unfortunately it did not work for me. I am getting this error:

System.Exception: ‘The document does not define a link for ‘Values’’

However, I found another way to make it work.

var link = repository.Client.RootDocument.Links["RepositoryConfiguration"];
var config = repository.Client.Get<BuiltInRepositoryConfigurationResource>(link);
config.DeleteUnreleasedPackagesAfterDays = 10;
repository.Client.Put<BuiltInRepositoryConfigurationResource>(link, config);

I have also converted my code to a PowerShell script since it’s what I needed originally. (I will be running the script in Octopus.) Here’s how I did it in PowerShell.

$link = $repository.Client.RootDocument.Links["RepositoryConfiguration"]
$config = $repository.Client.GetType().GetMethod("Get").MakeGenericMethod([Octopus.Client.Model.BuiltInRepositoryConfigurationResource]).Invoke($repository.Client, @($link.ToString(), $null))
$config.DeleteUnreleasedPackagesAfterDays = 15
[type]$TResourcetype = $repository.Client.GetType().GetMethods() | Where {$_.Name -eq "Put"} | foreach-object {$_.GetParameters() | Select Name, ParameterType, Position} | Where {$_.ParameterType -like "TResource"} | Select -ExpandProperty ParameterType
$repository.Client.GetType().GetMethod("Put", [type[]]@('string', $TResourcetype)).MakeGenericMethod([Octopus.Client.Model.BuiltInRepositoryConfigurationResource]).Invoke($repository.Client, @($link.ToString(), $config))

Awesome you got it to work. If you wanted to keep it in C#, here are the instructions on how to load Octopus.Client in a C# script step.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.