Using API to create release with prompted variable

So we are trying to create a release with a prompted api and were following this template: https://gist.github.com/Dalmirog/b6264c8b7d61a2e1398a

Unfortunately I think things have changed since there. One thing I noticed is that the FormValues are stored using a GUID and not their name, so the only way I could properly set the value using powershell was to specify the GUID in the Deployment Body.

Here is the example:

$DeploymentBody = @{ 
            ReleaseID = $r.Id #mandatory
            EnvironmentID = $Environment.id #mandatory
            FormValues = @{ # Here is where you declare all your Prompt variables.
                            "02dc8a2f-c71e-b7c9-40a5-adcacc0df83c" = "My Variable Value!"
            ForcePackageDownload="False"
            ForcePackageRedeployment="False"
            UseGuidedFailure="False"
                         }                             
          } | ConvertTo-Json
          
Invoke-WebRequest $OctopusURL/api/deployments -Method Post -Headers $Header -UseBasicParsing -Body $DeploymentBody

The actual name of the variable is “prompted”

"Form": {
    "Values": {
      "02dc8a2f-c71e-b7c9-40a5-adcacc0df83c": ""
    },
    "Elements": [
      {
        "Name": "02dc8a2f-c71e-b7c9-40a5-adcacc0df83c",
        "Control": {
          "Type": "VariableValue",
          "Name": "prompted",
          "Label": "Prompted",
          "Description": null,
          "IsSecure": false
        },
        "IsValueRequired": true
      }
    ]
  },

Hi Corey,

Thanks for getting in touch.

Yes this is correct. You will need to specify the GUID of the variable in the deployment body. The template shows an example of how to do this by querying the deployment preview api.

From what we can tell, that template is still working correctly. We had to make one minor change to this original template (to strip some new placeholder data that exists in the preview url), but once you do that you can query the deployment preview API, which contains the prompted variables information, including their GUIDs.

Eg. See #NOTE comments inline from your JSON:

        "Name": "02dc8a2f-c71e-b7c9-40a5-adcacc0df83c", #NOTE: This is the GUID we assign to identify the variable, do not confuse this "Name" with the "Name" field included below.
        "Control": {
          "Type": "VariableValue",
          "Name": "prompted", #NOTE this is the name you set in the UI

Once you know the deployment preview URL, it will give you back the GUIDs you need to complete the deployment request, looking something like this:

...
    "Form":  {
                 "Values":  {
                                "baa2a787-f81f-aa3e-6b2d-81bb2d9d4593":  "some default value"
                            },
                 "Elements":  [
                                  "@{Name=baa2a787-f81f-aa3e-6b2d-81bb2d9d4593; Control=; IsValueRequired=True}"
                              ]
...

So you can then address the GUID of your prompted variables. Eg: $($p.Form.Elements.name) (Note Dalmiro’s comment about this PowerShell example being limited to one prompted variable only).

Here’s a slightly modified example including some #NOTE comments to hopefully make this more clear:

$apiKey = "API-YOUR_API_KEY_HERE"
$OctopusURL = "http://YOUR_INSTANCE_HERE"

$Header =  @{ "X-Octopus-ApiKey" = $apiKey }

$ProjectName = "YOUR_PROJECT"
$EnvironmentName = "YOUR_ENVIRONMENT"

#Getting Environment and Project By Name
$Project = Invoke-WebRequest -Uri "$OctopusURL/api/projects/$ProjectName" -Headers $Header| ConvertFrom-Json

$Environment = Invoke-WebRequest -Uri "$OctopusURL/api/Environments/all" -Headers $Header| ConvertFrom-Json

$Environment = $Environment | ?{$_.name -eq $EnvironmentName}

#Getting Deployment Template to get Next version 
$dt = Invoke-WebRequest -Uri "$OctopusURL/api/deploymentprocesses/deploymentprocess-$($project.id)/template" -Headers $Header | ConvertFrom-Json 

#Creating Release
$ReleaseBody =  @{ Projectid = $Project.Id
            version = $dt.nextversionincrement } | ConvertTo-Json

$r = Invoke-WebRequest -Uri $OctopusURL/api/releases -Method Post -Headers $Header -Body $ReleaseBody | ConvertFrom-Json

#Getting task to get preview
$t = Invoke-WebRequest -Uri ($OctopusURL + $r.links.DeploymentTemplate) -Headers $Header | ConvertFrom-Json

#Preview holds the variables labels and names.
$previewEndpoint = $($t.PromoteTo.Links.Preview) -replace "\{\?includeDisabledSteps\}", "" # We need to strip this template parameter out.
Write-Host "My deployment preview endpoint is: " + $previewEndpoint
$p = Invoke-WebRequest -Uri ($OctopusURL + $previewEndpoint) -Headers $Header | ConvertFrom-Json
#NOTE: The response from the preview endpoint gives you a Form "Values" and "Elements", that you can then use in the subsequent deployment request..."
#NOTE: For example, look at the following JSON response:
$p | ConvertTo-Json

#NOTE: This next section will only work if you have a single prompted variable. See Dalmiro's comment on the next line if you have multiple prompted variables...
#Creating deployment and setting value to the only prompt variable I have on $p.Form.Elements. You're gonna have to do some digging if you have more variables
$DeploymentBody = @{ 
            ReleaseID = $r.Id #mandatory
            EnvironmentID = $Environment.id #mandatory
            FormValues = @{ # Here is where you declare all your Prompt variables.
                            "$($p.Form.Elements.name)" = "My Variable Value!"
            ForcePackageDownload="False"
            ForcePackageRedeployment="False"
            UseGuidedFailure="False"
                         }                             
          } | ConvertTo-Json

#NOTE: This shows you the request body for a new deployment, including our prompted variable GUID that we got from the Form.Elements...
$DeploymentBody

# Uncomment this line when you want to create the deployment
#Invoke-WebRequest -Uri $OctopusURL/api/deployments -Method Post -Headers $Header -Body $DeploymentBody

We were able to run this against the latest version of Octopus and it created our deployment after setting our prompted variable’s value to “My Variable Value!”, as seen above. Again, if you have multiple prompted variables, you will need to make changes to the script to support that.

Hope this helps

Cheers
Mark