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-Jsontry {
$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”
}