How can I download a package from the built in Feed programmatically?

Hi All,

I’m looking for a way to Deploy a package a bit more dynamically than how the out of the box step template for Deploying a package works.

The package name and version will not be known until deployment time, so I would like to create a custom template to Deploy a Package using the Octopus Client powershell code.

Can anyone share a code snippet of how this can be accomplished?

Hi Jason,

Thanks for getting in touch!

I haven’t been able to get this to work so far using either the Octopus.Client or direct REST calls.
I can use the Octopus.Client to get specific package details $package = $repositoryForSpace.BuiltInPackageRepository.GetPackage($packageID,$packageVersion), but can’t see any mechanism to download it.

The easiest way I can think of to achieve this would be to have the script copy the package from the repository folder location e.g. C:\Octopus\Packages\Spaces-1\feeds-builtin\<packageName>\<packageName.<version>

Regards,
Paul

Thanks very much paul for the prompt reply.

That is a good idea, but I think the dilemma I face is the packages are stored in a storage account in Azure and not on the Octopus server itself.

I guess I can target the storage account and try to download it, that way…But ideally i think the path of least resistance is mimicking what Octopus does in that step. Any idea if the code is available for ‘Deploy Package’ step template?

Hi Jason,

Continued to work on this, and have got the following script working using the REST API.

# Define working variables
$octopusURL = "https://your.octopus.app"
$octopusAPIKey = "API-YOURAPIKEY"
$header = @{ "X-Octopus-ApiKey" = $octopusAPIKey }
$spaceName = "Default"
$packageName = "packageName"
$packageVersion = "1.0.0.0"
$outputFolder = "/path/to/output/folder"
try
{
    # Get space
    $space = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/spaces/all" -Headers $header) | Where-Object {$_.Name -eq $spaceName}
    # Get package details
    $package = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/$($space.Id)/packages/packages-$packageName.$packageVersion" -Headers $header)
    # Get package
    $filePath = [System.IO.Path]::Combine($outputFolder, "$($package.PackageId).$($package.Version)$($package.FileExtension)")
    Invoke-RestMethod -Method Get -Uri "$octopusURL/api/$($space.Id)/packages/$packageName.$packageVersion/raw" -Headers $header -OutFile $filePath
    Write-Host "Downloaded file to $filePath"
}
catch
{
    Write-Host $_.Exception.Message
}

Regards,
Paul

Wow, thanks! I’ll give this a shot

1 Like

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