Adding a step returns 400 error, advice please

Currently when attempting to add a step to a deployment process I am receiving a 400 error. This is a cloud implementation, as such I cannot see any detailed information regarding this error.

In a previous post on this forum it said that $scriptBody was an issue, so I updated mine to a simplistic script.
$scriptBody = ‘Write-Host “SODTESTING because powershell makes me sad”’

This is the function that is failing…

function Add-Action($stepName, $stepTemplateName, $steps){
$alreadyAdded = Get-AddedAlreadyStatus $actionTemplateId $steps

if ($alreadyAdded -eq $true){
Write-Host “—> Step: $($stepTemplateName) - Already in project, will not add.” -ForegroundColor Yellow
return;
}

#convert the array to a collection
$stepCollection = {$deploymentProcess.Steps}.Invoke()

Add new step to process

$step = @{
Name = “$stepTemplateName”
Properties = @{
‘Octopus.Action.TargetRoles’ = $roles
}
Condition = “Success”
StartTrigger = “StartAfterPrevious”
PackageRequirement = “LetOctopusDecide”
Actions = @(
@{
ActionType = “Octopus.Script”
Name = “Run a script”
Environments = @()
ExcludedEnvironments = @()
Channels = @()
TenantTags = @()
Properties = @{
‘Octopus.Action.RunOnServer’ = “false”
‘Octopus.Action.EnabledFeatures’ = “”
‘Octopus.Action.Script.ScriptSource’ = “Inline”
‘Octopus.Action.Script.Syntax’ = “PowerShell”
‘Octopus.Action.Script.ScriptBody’ = $scriptBody
‘Octopus.Action.Template.Id’ =$actionTemplateID
‘Octopus.Action.Template.Version’ = $actionTemplateVersion
}
Packages = @()
IsDisabled = $false
WorkerPoolId = “”
WorkerPoolVariable = “”
Container = @{
“FeedId” = $null
“Image” = $null
}
CanBeUsedForProjectVersioning = $false
IsRequired = $false
}
)
}

$stepCollection.Insert(0, $step)

#convert it back to an array
$stepCollection | Set-Variable stepArray

$deploymentProcess.Steps = $stepArray

$jsonPayload = $deploymentProcess | ConvertTo-Json -Depth 10

Write-Host “—> JSON Payload”

Write-Host $jsonPayload

if ($previewOnly -eq $false){
Invoke-RestMethod -Method Put -Uri “$octopusURL/api/$($spaceId)/deploymentprocesses/$($project.DeploymentProcessID)” -Headers $header -Body $jsonPayload
Write-Host “Attempted to update Deployment Process”
}

}

Hey @john.king1,

Thanks for posting on our help forum, and welcome to the community! My guess would be that there is some bad data in that JSON payload. If you can give me your Cloud instance URL, I can look up the error message to see if there is some helpful info. Also, we have an API script that will add a script step to a project that you may want to try. You can compare that script with the one you’re running to help you get unstuck. You may also find other scripts in our public API repo helpful.

Best,
Brent

[redacted]

Hey @john.king1,

That is an on-premise Octopus instance. Our cloud instances follow this naming convention - chosensitename.octopus.app.

Your admin should be able to look into the Server logs and find that error for you. That API script I sent should also help you get things moving as it is adding a similar script step.

Best,
Brent

Ok so is it possible that you could help diagnose malformed JSON when we are attempting to insert a step at 0?

I started with that script. It was a great help, just at a small hurdle now with the JSON formation.

Hey @john.king1,

If you can get the error info from your server logs, we would be happy to have a look, but I’ve got a feeling that this will be resolved by debugging the script via breakpoints and inspecting what data is getting passed to each variable you’re setting. The error message will likely indicate a problem with your API Post line, which may not be very helpful for the diagnosis.

Best,
Brent

It did turn out to be a user error/json issue. I tried to get fancy and burned myself. Thank you for your help.

2 Likes

Hey @john.king1,

I’m glad you were able to figure it out! Let us know if anything else comes up.

Happy deployments!

Best,
Brent