Package Selection While Creating New Release Using REST API

Hi Team,

Problem Description :

I am using REST API to create release . However , below is the following scenario where I have multiple steps from Step 1 to Step 10 involved in a project where I wanted to select package version only for selective steps which is Step 5, Step 7, Step 9 and rest of them should have the greatest one or the latest package version where some steps does not have the package as well. I have wrote the some code for it, however it not returning the result as I am expecting. I need a help where how I can continue to choose default or latest version of not selective step names like Step 1, Step 2, Step 8, Step 10.This text will be hidden

REST API CODE :

#Project variable
$NonPreviewVersion = “2.13.0”
$DeploymentRelam = “CLIENTSTABLE-770”
$AssociationName = “TEST”
$PackageId = “QATEST”
$ProjectName = “QATEST”
$EnvironmentType = $($DeploymentRelam)

#Octopus Server URL
$OctopusServerURL = “”

#An API key, preferably for a Service Account
$OctopusServerApiKey = “”

#Octopus Space Name
$SpaceName = “Default”

#Octopus Defauly ChannelName
$ChannelName = “Default”

#Process
$Header = @{ “X-Octopus-ApiKey” = $OctopusServerApiKey }

#Get Default Space Name & It’s ID
$SpaceList = (Invoke-WebRequest “$OctopusServerURL/api/spaces?Name=$SpaceName” -Headers $Header -UseBasicParsing).content | ConvertFrom-Json
$SpaceId = @($SpaceList.Items | Where-Object { ($_.Name -eq “$SpaceName”) })[0].id
Write-Output "The SpaceId For Space Name $SpaceName Is : "$SpaceId

#Get Project By Name
$Projects = (Invoke-WebRequest -Uri “$OctopusServerURL/api/projects/all” -Headers $Header -UseBasicParsing).content | ConvertFrom-Json
$ProjectToExecute = $Projects | Where-Object { ($_.Name -eq $ProjectName) }
Write-Output "Project To Execute : "$ProjectToExecute.Name

#Get Channel By Name
$Channels = (Invoke-WebRequest -Uri “$OctopusServerURL/api/projects/$($ProjectToExecute.Id)/channels” -Headers $Header -UseBasicParsing).content | ConvertFrom-Json
$Channel = $Channels | Where-Object { $_.Items.Name -eq $ChannelName }
Write-Output “Using Channel Named $($Channel.Items.Name) With Id $($Channel.Items.Id)”

#Get EnvironmentType By Name
$EnvironmentTypes = (Invoke-WebRequest -Uri “$OctopusServerURL/api/Environments/all” -Headers $Header -UseBasicParsing).content | ConvertFrom-Json
$EnvironmentType = $EnvironmentTypes | Where-Object { ($_.Name -eq $EnvironmentType) }
Write-Output "Project To Execute On EnvironmentType : "$EnvironmentType.Name

#Getting Deployment Template to get Next version
$DeploymentTemplate = Invoke-WebRequest -Uri “$OctopusServerURL/api/deploymentprocesses/deploymentprocess-$($ProjectToExecute.id)/template?channel=$($Channel.Id)” -Headers $Header | ConvertFrom-Json

$DeploymentTemplate.Packages | ForEach-Object {

$URI = “$OctopusServerURL/api/feeds/$($.FeedId)/packages/versions? packageId=$($.PackageId)&take=1”
$PacakageIDVersionList = (Invoke-WebRequest -Uri $URI -Method GET -Headers $Header ->UseBasicParsing).content | ConvertFrom-Json
$DefaultVersion = $PacakageIDVersionList.Items[0].Version
}

$DeploymentTemplate.Packages | Where-Object { $_.PackageId -eq “$PackageId” } | ForEach-Object {

$URI = “$OctopusServerURL/api/feeds/$($.FeedId)/packages/versions?packageId=$($.PackageId)&includeMultipleVersions=true&includePreRelease=true&take=100000”
$PacakageIDVersionList = (Invoke-WebRequest -Uri $URI -Method GET -Headers $Header -UseBasicParsing).content | ConvertFrom-Json
$PackageIDVersion = ($PacakageIDVersionList.Items | Where-Object { ($_.ID -like “.$($NonPreviewVersion).”) } | Sort-Object LastAccessTime -Descending | Select-Object -First 1).Version
Write-Output “PackageIDVersion Is $($PackageIDVersion)”

}

#Create Release
$ReleaseBody = @{
Projectid = $ProjectToExecute.Id
version = $DeploymentTemplate.nextversionincrement
ReleaseNotes = “Setup PGO For $($AssociationName) With Version $($PackageIDVersion) On $($EnvironmentType.Name)”
SelectedPackages = @(
@{
ActionName = $_.ActionName
Version = $($DefaultVersion)
}
@{
StepName = “Step 5”
Version = “$($PackageIDVersion)”
}
@{
StepName = “Step 7”
Version = “$($PackageIDVersion)”
}
@{
StepName = “Step 9”
Version = “$($PackageIDVersion)”
}
)
} | ConvertTo-Json

try {
$Release = (Invoke-WebRequest -Uri “$OctopusServerURL/api/releases” -Method Post -Headers $Header -Body $ReleaseBody -ErrorVariable octoError -UseBasicParsing).content | ConvertFrom-Json
}
catch {
$Result = $_.Exception.Response.GetResponseStream()
$Reader = New-Object System.IO.StreamReader($Result)
$ResponseBody = $Reader.ReadToEnd();
$Response = $ResponseBody | ConvertFrom-Json
$Response.Errors
exit
}
$ReleaseID = $Release.Id
Write-Output “Release ID For $($AssociationName) Is : $ReleaseID”

Error Output

{
“ReleaseNotes”: “Setup PGO For TEST With Version On CLIENTSTABLE-770”,
“SelectedPackages”: [
{
“ActionName”: null,
“Version”: “7.7.0”
},
{
“Version”: “2.13.0.115”,
“StepName”: “Step 5”
},
{
“Version”: “2.13.0.115”,
“StepName”: “Step 7”
},
{
“Version”: “2.13.0.115”,
“StepName”: “Step 9”
}
],
“version”: “0.0.34”,
“Projectid”: “Projects-603”
}

Hi @vivek.singh,

Looking at our example here(https://github.com/OctopusDeploy/OctopusDeploy-Api/blob/master/REST/PowerShell/Releases/CreateRelease.ps1), it looks like the SelectedPackages object is a bit different than the one you’re building. Would you be able to change your SelectPackages object to mirror the logic and object format in the example and let me know if that fixes the issue for you? Each object should need the action name of the step, the version desired, and the package Id.

Please let me know if it works or if you need further help with it.

This code in particular may be useful:

    foreach ($step in $deploymentProcess.Steps)
{
    # Loop through the actions
    foreach($action in $step.Actions)
    {
        # Check for packages
        if ($null -ne $action.Packages)
        {
            # Loop through packages
            foreach ($package in $action.Packages)
            {
                # Get latest version of package
                $packageVersion = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/$($space.Id)/feeds/$($package.FeedId)/packages/versions?packageId=$($package.PackageId)&take=1" -Headers $header).Items[0].Version

                # Add package to selected packages
                $selectedPackages += @{
                    ActionName = $action.Name
                    Version = $packageVersion
                    PackageReferenceName = $package.PackageId
                }
            }
        }
    }
}
# Create json payload
$jsonPayload = @{
    ProjectId = $project.Id
    ChannelId = $channel.Id
    Version = $releaseVersion
    SelectedPackages = $selectedPackages
}

# Create the release
Invoke-RestMethod -Method Post -Uri "$octopusURL/api/$($space.Id)/releases" -Body ($jsonPayload | ConvertTo-Json -Depth 10) -Headers $header

Thanks,
Jeremy

Thanks for responding very quickly and providing the solution.

I guess something is missing I have tried the entire script to test the create release and it is returning the 404 error.

The entire process I have tried through octo.cli and things are working fine however I wanted to get this done through rest api only.

Which script did you try and failed? The example one with no modifications?

Thanks,
Jeremy

Yes, I have copied the entire script and passed the required variables to see how it is working .
however, after it so it throwed the error :

The remote server returned an error: (400) Bad Request

Hi,

I’m finding I am having issues with it on my side as well. I am tinkering with it and will update you asap.

Thanks,
Jeremy

It seems that post method is getting failed while transferring payload.
take your time , I will wait & thanks for helping so far.

Hi @vivek.singh,

I figured out what’s going on here. The PackageReferenceName part of the json needs to be just “” if the step is a deploy a package step.

I’m going to talk to the creator of the example I sent you and get that one updated, but in the meantime, this example has working code: https://github.com/OctopusDeploy/OctopusDeploy-Api/blob/a78a6f445531910aefe91ae93f86eea560ce9998/REST/PowerShell/Deployments/CreateReleaseAndDeployment.ps1

This part in particular will be helpful:

This builds the selectedpackages by looking at the deployment process (stored in $template), and references itself for the values necessary. The only thing you should need to modify is at the part where its building the object with ActionName, PackageReferenceName, and Version, you would need to implement logic for “If action name equals one of these steps I’ve defined, make it this version instead of latest”

$template = Invoke-WebRequest -Uri "$octopusSpaceUrl/deploymentprocesses/deploymentprocess-$($project.id)/template?channel=$($channel.Id)" -Headers $headers | ConvertFrom-Json

# Create the release body
$releaseBody = @{
ChannelId        = $channel.Id
ProjectId        = $project.Id
    Version          = $template.NextVersionIncrement
    SelectedPackages = @()
}

# Set the package version to the latest for each package
# If you have channel rules that dictate what versions can be used, you'll need to account for that
Write-Host "Getting step package versions"
$template.Packages | ForEach-Object {
    $uri = "$octopusSpaceUrl/feeds/$($_.FeedId)/packages/versions?packageId=$($_.PackageId)&take=1"
    $version = Invoke-WebRequest -Uri $uri -Method GET -Headers $headers -Body $releaseBody -ErrorVariable octoError | ConvertFrom-Json
    $version = $version.Items[0].Version

    $releaseBody.SelectedPackages += @{
        ActionName           = $_.ActionName
        PackageReferenceName = $_.PackageReferenceName
        Version              = $version
    }
}

# Create release
$releaseBody = $releaseBody | ConvertTo-Json
Write-Host "Creating release with these values: $releaseBody"
$release = Invoke-WebRequest -Uri $octopusSpaceUrl/releases -Method POST -Headers $headers -Body $releaseBody -ErrorVariable octoError | ConvertFrom-Json

Please let me know if that works for you.

Thanks,
Jeremy

Hi Jeremy,

Thanks for help & information. I took help from the link you have posted and was able to get the release with latest package version of requited package step. That is a perfect solution for ideal scenario.

However, the last statement you have spoke about , I was trying to implanting the if condition block outside of hash table declaration for release body. I am getting null value. I
think need help in this section of implement the logic which I am thinking of where to implement it.

If you see in my original post then , there is snippet I am trying to playing around and then when I am executing it is throwing the error that I have selected multiple package for same step.

Here is the example :

$ReleaseBody.SelectedPackages +=
@{
ActionName = $.ActionName
PackageReferenceName = $
.PackageReferenceName
Version = $DefaultVersion
}
@{
ActionName = $.ActionName -eq “Test”
PackageReferenceName = $
.PackageReferenceName
Version = $PackageIDVersion
}

Note : I am able to achieve the objective using Octo.exe. However wanted to prefer REST over command line.

Hi,

Here is the latest snippet, definitely I am doing something wrong : please see if you can find correct logic for this pattern.

Snippet

$DeploymentTemplate = Invoke-WebRequest -Uri “$OctopusServerURL/api/deploymentprocesses/deploymentprocess-$($ProjectToExecute.id)/template?channel=$($Channel.Id)” -Headers $Header | ConvertFrom-Json
$DeploymentTemplate.Packages | ForEach-Object {

$URI = “$OctopusServerURL/api/feeds/$($.FeedId)/packages/versions?packageId=$($.PackageId)&take=1”
$PacakageIDVersionList = (Invoke-WebRequest -Uri $URI -Method GET -Headers $Header -Body $ReleaseBody -ErrorVariable octoError -UseBasicParsing).content | ConvertFrom-Json
$DefaultVersion = $PacakageIDVersionList.Items[0].Version

$ReleaseBody.SelectedPackages +=
@{
ActionName = $.ActionName
PackageReferenceName = $
.PackageReferenceName
Version = $DefaultVersion
}

foreach ($ActionName in $($DeploymentTemplate.Packages.ActionName)) {

if ($ActionName -match “TEST”) {

   $ReleaseBody.SelectedPackages += @{
     StepName = "TEST"
   Version  = $PackageIDVersion         

}

   }

}
}

Error : More than one package version was specified for the step ‘TEST’

Hi,

Correct me if I’m misunderstanding your process, but to simplify, you want it to use latest package version for a package EXCEPT for for a few specific steps, then you want to use a specific version of the package?

If that’s the case, the way I would do this is by changing the way the object is created.

First, create an array with the step names in it that you want the specific package to be used. Iterate through the array to find out if it is one of the special steps. If it is, create the custom object, set a flag, if the flag is set, dont create the regular one. For instance,

$specialSteps = "Step Name 1", "Step Name 2", "Step Name 3"

#set flag before each deployment process step
$usedSpecialStep = $false
   #iterate through the array, if the current action in the deployment process is in the array, create the package reference with a static version
 foreach ($step in $specialSteps){
    if ($_.ActionName -eq $step){

        $releaseBody.SelectedPackages += @{
            ActionName           = $_.ActionName
            PackageReferenceName = $_.PackageReferenceName
            Version              = $STATICPACKAGEVERSION
        }
#set the flag if we found one of the special steps
$usedSpecialStep = $true
    }
    }
#if the step wasnt found, the flag will still be false, so create the normal one.
if ($usedSpecialStep -eq $false){

$releaseBody.SelectedPackages += @{
        ActionName           = $_.ActionName
        PackageReferenceName = $_.PackageReferenceName
        Version              = $version
    }
}

$version would be the latest you find earlier in the code example I pasted above. This SHOULD work but I wrote it without testing it, the syntax may be off as I wrote it here instead of an IDE.

Please let me know how it goes or if I misunderstood your use case. As always please test and read scripts thoroughly before using in prod.

Thanks,
Jeremy

Hi Jeremy,

BIG THANK YOU FOR YOUR CONSISTENT SUPPORT

Thanks for the information. That was helpful and I was able to construct the snippet which worked for me for what i wanted to have with custom package version of selective action name or step name in projects.

Here is the working code which might help someone in need like me :slight_smile:

# Octopus Server URL
$OctopusServerURL = ""

# Octopus Server URL API key
$OctopusServerApiKey = ""

# Octopus Space Name
$SpaceName = "Default"

# Octopus Defauly ChannelName
$ChannelName = "Default"

# Process
$Header = @{ "X-Octopus-ApiKey" = $OctopusServerApiKey }

# Get Default Space Name & It's ID
$SpaceList = (Invoke-WebRequest "$OctopusServerURL/api/spaces?Name=$SpaceName" -Headers $Header -UseBasicParsing).content | ConvertFrom-Json
$SpaceId = @($SpaceList.Items | Where-Object { ($_.Name -eq "$SpaceName") })[0].id
Write-Output "The SpaceId For Space Name $SpaceName Is : "$SpaceId

# Get Project By Name
$Projects = (Invoke-WebRequest -Uri "$OctopusServerURL/api/projects/all" -Headers $Header -UseBasicParsing).content | ConvertFrom-Json
$ProjectToExecute = $Projects | Where-Object { ($_.Name -eq $ProjectName) }
Write-Output "Project To Execute : "$ProjectToExecute.Name

# Get Channel By Name
$Channels = (Invoke-WebRequest -Uri "$OctopusServerURL/api/projects/$($ProjectToExecute.Id)/channels" -Headers $Header -UseBasicParsing).content | ConvertFrom-Json
$Channel = $Channels | Where-Object { $_.Items.Name -eq $ChannelName }
Write-Output "Using Channel Named $($Channel.Items.Name) With Id $($Channel.Items.Id)"
   
# Get EnvironmentType By Name
$EnvironmentTypes = (Invoke-WebRequest -Uri "$OctopusServerURL/api/Environments/all" -Headers $Header -UseBasicParsing).content | ConvertFrom-Json
$EnvironmentType = $EnvironmentTypes | Where-Object { ($_.Name -eq $EnvironmentType) }
Write-Output "Project To Execute On EnvironmentType : "$EnvironmentType.Name

# Getting Deployment Template to get Next version
$DeploymentTemplate = Invoke-WebRequest -Uri "$OctopusServerURL/api/deploymentprocesses/deploymentprocess-$($ProjectToExecute.id)/template?channel=$($Channel.Id)" -Headers $Header | ConvertFrom-Json

# Create the release body
$ReleaseBody = @{
    Projectid        = $ProjectToExecute.Id
    ChannelId        = $Channel.Items.Id
    Version          = $DeploymentTemplate.NextVersionIncrement
    ReleaseNotes     = ""
    SelectedPackages = @()
}
# Set the CustomPackageIDVersion for specific packages
$DeploymentTemplate.Packages  | Where-Object { $_.PackageId -eq "$PackageId" }  | ForEach-Object {
    $URI = "$OctopusServerURL/api/feeds/$($_.FeedId)/packages/versions?packageId=$($_.PackageId)&includeMultipleVersions=true&includePreRelease=true&take=100000"
    $PacakageIDVersionList = (Invoke-WebRequest -Uri $URI -Method GET -Headers $Header -UseBasicParsing).content | ConvertFrom-Json
    $CustomPackageIDVersion = ($PacakageIDVersionList.Items | Where-Object { ($_.ID -like "*.$($UserInputVersionValue).*") } | Sort-Object LastAccessTime -Descending | Select-Object -First 1).Version
    Write-Output "PackageIDVersion Is $($CustomPackageIDVersion)"

}

$DeploymentTemplate.Packages | ForEach-Object {
    $URI = "$OctopusServerURL/api/feeds/$($_.FeedId)/packages/versions?packageId=$($_.PackageId)&take=1"
    $PacakageIDVersionList = (Invoke-WebRequest -Uri $URI -Method GET -Headers $Header -Body $ReleaseBody -ErrorVariable octoError -UseBasicParsing).content | ConvertFrom-Json
    $DefaultVersion = $PacakageIDVersionList.Items[0].Version

    if (($_.ActionName) -eq "StepA") {
       
        $ReleaseBody.SelectedPackages += @{
            ActionName           = "StepA"
            PackageReferenceName = $_.PackageReferenceName
            Version              = $($CustomPackageIDVersion)
        }
    }
    elseif (($_.ActionName) -eq "StepB") {
       
        $ReleaseBody.SelectedPackages += @{
            ActionName           = "StepB"
            PackageReferenceName = $_.PackageReferenceName
            Version              = $($CustomPackageIDVersion)
        }
    }
    elseif (($_.ActionName) -eq "StepC") {
       
        $ReleaseBody.SelectedPackages += @{
            ActionName           = "StepC"
            PackageReferenceName = $_.PackageReferenceName
            Version              = $($CustomPackageIDVersion)
        }
    }
    else {
        $ReleaseBody.SelectedPackages += 
        @{
            ActionName           = $_.ActionName
            PackageReferenceName = $_.PackageReferenceName
            Version              = $DefaultVersion        
        }
        
          
    }
try {
    $ReleaseBody = $ReleaseBody | ConvertTo-Json
    $Release = (Invoke-WebRequest -Uri "$OctopusServerURL/api/releases" -Method Post -Headers $Header -Body $ReleaseBody -ErrorVariable octoError -UseBasicParsing).content | ConvertFrom-Json
}
catch {
    $Result = $_.Exception.Response.GetResponseStream()
    $Reader = New-Object System.IO.StreamReader($Result)
    $ResponseBody = $Reader.ReadToEnd();
    $Response = $ResponseBody | ConvertFrom-Json
    $Response.Errors
    exit
}
$ReleaseID = $Release.Id
Write-Output "Release ID Is : $ReleaseID"
1 Like

Hi,

You’re very welcome! I’m glad to hear you got it going and thank you for updating me and providing the final script.

I hope you have a great rest of your week.

Thanks,
Jeremy

1 Like

Hi Jeremy,

Here is latest one. I have updated line number “42” where it was earlier not able to shortlist the package on published date from built-in repo.

  1. # Octopus Server URL
    
  2. $OctopusServerURL = ""
    
  3. # Octopus Server URL API key
    
  4. $OctopusServerApiKey = ""
    
  5. # Octopus Space Name
    
  6. $SpaceName = "Default"
    
  7. # Octopus Defauly ChannelName
    
  8. $ChannelName = "Default"
    
  9. # Process
    
  10. $Header = @{ "X-Octopus-ApiKey" = $OctopusServerApiKey }
    
  11. # Get Default Space Name & It's ID
    
  12. $SpaceList = (Invoke-WebRequest "$OctopusServerURL/api/spaces?Name=$SpaceName" -Headers $Header -UseBasicParsing).content | ConvertFrom-Json
    
  13. $SpaceId = @($SpaceList.Items | Where-Object { ($_.Name -eq "$SpaceName") })[0].id
    
  14. Write-Output "The SpaceId For Space Name $SpaceName Is : "$SpaceId
    
  15. # Get Project By Name
    
  16. $Projects = (Invoke-WebRequest -Uri "$OctopusServerURL/api/projects/all" -Headers $Header -UseBasicParsing).content | ConvertFrom-Json
    
  17. $ProjectToExecute = $Projects | Where-Object { ($_.Name -eq $ProjectName) }
    
  18. Write-Output "Project To Execute : "$ProjectToExecute.Name
    
  19. # Get Channel By Name
    
  20. $Channels = (Invoke-WebRequest -Uri "$OctopusServerURL/api/projects/$($ProjectToExecute.Id)/channels" -Headers $Header -UseBasicParsing).content | ConvertFrom-Json
    
  21. $Channel = $Channels | Where-Object { $_.Items.Name -eq $ChannelName }
    
  22. Write-Output "Using Channel Named $($Channel.Items.Name) With Id $($Channel.Items.Id)"
    
  23. # Get EnvironmentType By Name
    
  24. $EnvironmentTypes = (Invoke-WebRequest -Uri "$OctopusServerURL/api/Environments/all" -Headers $Header -UseBasicParsing).content | ConvertFrom-Json
    
  25. $EnvironmentType = $EnvironmentTypes | Where-Object { ($_.Name -eq $EnvironmentType) }
    
  26. Write-Output "Project To Execute On EnvironmentType : "$EnvironmentType.Name
    
  27. # Getting Deployment Template to get Next version
    
  28. $DeploymentTemplate = Invoke-WebRequest -Uri "$OctopusServerURL/api/deploymentprocesses/deploymentprocess-$($ProjectToExecute.id)/template?channel=$($Channel.Id)" -Headers $Header | ConvertFrom-Json
    
  29. # Create the release body
    
  30. $ReleaseBody = @{
    
  31.     Projectid        = $ProjectToExecute.Id
    
  32.     ChannelId        = $Channel.Items.Id
    
  33.     Version          = $DeploymentTemplate.NextVersionIncrement
    
  34.     ReleaseNotes     = ""
    
  35.     SelectedPackages = @()
    
  36. }
    
  37. # Set the CustomPackageIDVersion for specific packages
    
  38. $DeploymentTemplate.Packages  | Where-Object { $_.PackageId -eq "$PackageId" }  | ForEach-Object {
    
  39.     $URI = "$OctopusServerURL/api/feeds/$($_.FeedId)/packages/versions?packageId=$($_.PackageId)&includeMultipleVersions=true&includePreRelease=true&take=100000"
    
  40.     $PacakageIDVersionList = (Invoke-WebRequest -Uri $URI -Method GET -Headers $Header -UseBasicParsing).content | ConvertFrom-Json
    
  41.     $CustomPackageIDVersion = ($PacakageIDVersionList.Items | Where-Object { ($_.ID -like "*.$($UserInputVersionValue).*") } | Select-Object -First 1).Version
    
  42.     Write-Output "PackageIDVersion Is $($CustomPackageIDVersion)"
    
  43. }
    
  44. $DeploymentTemplate.Packages | ForEach-Object {
    
  45.     $URI = "$OctopusServerURL/api/feeds/$($_.FeedId)/packages/versions?packageId=$($_.PackageId)&take=1"
    
  46.     $PacakageIDVersionList = (Invoke-WebRequest -Uri $URI -Method GET -Headers $Header -Body $ReleaseBody -ErrorVariable octoError -UseBasicParsing).content | ConvertFrom-Json
    
  47.     $DefaultVersion = $PacakageIDVersionList.Items[0].Version
    
  48.     if (($_.ActionName) -eq "StepA") {
    
  49.         $ReleaseBody.SelectedPackages += @{
    
  50.             ActionName           = "StepA"
    
  51.             PackageReferenceName = $_.PackageReferenceName
    
  52.             Version              = $($CustomPackageIDVersion)
    
  53.         }
    
  54.     }
    
  55.     elseif (($_.ActionName) -eq "StepB") {
    
  56.         $ReleaseBody.SelectedPackages += @{
    
  57.             ActionName           = "StepB"
    
  58.             PackageReferenceName = $_.PackageReferenceName
    
  59.             Version              = $($CustomPackageIDVersion)
    
  60.         }
    
  61.     }
    
  62.     elseif (($_.ActionName) -eq "StepC") {
    
  63.         $ReleaseBody.SelectedPackages += @{
    
  64.             ActionName           = "StepC"
    
  65.             PackageReferenceName = $_.PackageReferenceName
    
  66.             Version              = $($CustomPackageIDVersion)
    
  67.         }
    
  68.     }
    
  69.     else {
    
  70.         $ReleaseBody.SelectedPackages += 
    
  71.         @{
    
  72.             ActionName           = $_.ActionName
    
  73.             PackageReferenceName = $_.PackageReferenceName
    
  74.             Version              = $DefaultVersion        
    
  75.         }
    
  76.     }
    
  77. try {
    
  78.     $ReleaseBody = $ReleaseBody | ConvertTo-Json
    
  79.     $Release = (Invoke-WebRequest -Uri "$OctopusServerURL/api/releases" -Method Post -Headers $Header -Body $ReleaseBody -ErrorVariable octoError -UseBasicParsing).content | ConvertFrom-Json
    
  80. }
    
  81. catch {
    
  82.     $Result = $_.Exception.Response.GetResponseStream()
    
  83.     $Reader = New-Object System.IO.StreamReader($Result)
    
  84.     $ResponseBody = $Reader.ReadToEnd();
    
  85.     $Response = $ResponseBody | ConvertFrom-Json
    
  86.     $Response.Errors
    
  87.     exit
    
  88. }
    
  89. $ReleaseID = $Release.Id
    
  90. Write-Output "Release ID Is : $ReleaseID"

Thanks for updating the thread!