Unable to select 'latest' version of package from REST api

I’m running into an issue where I am unable to create a release from the REST api for the latest package available from my teamcity nuget feed. I am able to select the last version, but that is no good. Is there some special flag I need to send in the json post to the REST api in order for it to grab the latest nuget package?

Thanks,
Rob

Hi Rob,

If you don’t pass a package version to the POST call to create the release, It’ll automatically use the latest one. This code snipped creates and deploys a release without passing a Package version for example: https://github.com/OctopusDeploy/OctopusDeploy-Api/blob/master/REST/PowerShell/Deployments/CreateReleaseAndDeployment.ps1#L24-L25

From where are you executing this REST API call? Is it from a step on your TeamCity build process?

Thanks,
Dalmiro

I’m calling the REST api as part of a step in another project within octopus deploy. Below is the snippet that I was using:

    #Getting Deployment Template to get Next version 
    $dt = Invoke-WebRequest -Uri "$baseUri/api/deploymentprocesses/deploymentprocess-$($project.Id)/template" -Headers $reqheaders -UseBasicParsing | ConvertFrom-Json 
    
    $releaseBody =  @{
        Projectid = $project.Id
        version = $Chain_ReleaseNum
        SelectedPackages = @( @{ StepName = $dt.Packages[0].StepName; Version = $dt.Packages[0].VersionSelectedLastRelease } )
    } | ConvertTo-Json

    write-verbose $releaseBody
    try {
        Write-Host "Creating Release version '$Chain_ReleaseNum' of '$projectName'"

        $release = Invoke-WebRequest -Uri "$baseUri/api/releases" -Method Post -Headers $reqheaders -Body $releaseBody -UseBasicParsing | ConvertFrom-Json
    } catch {
        Output-Exception -section "Create Release" -exception $_.Exception
    }

I tried what you suggested but i get an error when I do:

19:32:08 Verbose | Deploying myProject
19:32:09 Verbose | {
19:32:09 Verbose | “Projectid”: “Projects-82”,
19:32:09 Verbose | “version”: "0.1.334.99"
19:32:09 Verbose | }
19:32:09 Info | Creating Release version ‘0.1.334.99’ of 'myProject’
19:32:09 Info | [ERROR] : Create Release
19:32:09 Info | Status : ProtocolError
19:32:09 Info | Response : System.Net.HttpWebResponse
19:32:09 Info | Message : The remote server returned an error: (400) Bad Request.
19:32:09 Info | Data : {}
19:32:09 Info | InnerException :
19:32:09 Info | TargetSite : System.Net.WebResponse GetResponse(System.Net.WebRequest)
19:32:09 Info | StackTrace : at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.GetResp
19:32:09 Info | onse(WebRequest request)
19:32:09 Info | at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.Process
19:32:09 Info | Record()
19:32:09 Info | HelpLink :
19:32:09 Info | Source : Microsoft.PowerShell.Commands.Utility
19:32:09 Info | HResult : -2146233079
19:32:09 Error | Release 0.1.334.99 of myProject not found. Could not create deployment.
19:32:09 Error | At F:\Octopus\Work\20160628193200-543\Script.ps1:103 char:9
19:32:09 Error | + throw "Release $Chain_ReleaseNum of $projectName not found. C …
19:32:09 Error | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19:32:09 Error | + CategoryInfo : OperationStopped: (Release 0.1.334…ate deploym
19:32:09 Error | ent.:String) [], RuntimeException
19:32:09 Error | + FullyQualifiedErrorId : Release 0.1.334.99 of myProject not found.
19:32:09 Error | Could not create deployment.
19:32:09 Fatal | The remote script failed with exit code 1

Hi Robert,

If all you want to do is deploy the latest package, then you don’t need to define SelectedPackages. It should be something like this:

$releaseBody = @{ 
    Projectid = $project.Id 
    version = $Chain_ReleaseNum 
    } ) 
} | ConvertTo-Json

Have you tried using the snippet I sent you instead? I know that one works for sure.

If possible also let me know which version of Octopus are you running.

So i post
{
“Projectid”: “Projects-82”,
“version”: “0.1.334.102”
}

to hostname/api/releases

with a reply:
{“ErrorMessage”:“There was a problem with your request.”,“Errors”:[“No package version was specified for the step ‘Deploy myProject’”]}

i’m using 3.3.12

So it seems that I am missing something that the request needs. This is why I added the SelectedPackages to the request, but the SelectedPackages does not have a setting for latest only last.

Any update on this?

Hi Robert,

Apologies for my previous mistake. The example I gave you on that github project was for deployment processes without package steps.

To get the latest package version, you need to do a few more REST calls. I’d recommend you to do the following to learn about this:

  1. Open your browser’s developer tools on the “network” tab to see all the http requests that are being made.

  2. Click on the “Create Release” button of your project, and check all the calls that were made to the API to build that page. You’ll notice that some of those calls will get the deployment process, then the package name, then the latest package from the feed. Take note of those to build your script.

  3. Click on “Save” and inspect the full body of the JSON that was sent to the server, which will include the latest version of the package.

Let me know how that goes,
Dalmiro

Thanks, that lead to the calls that I needed to duplicate in my script.