Unable to modify process step using Octopus.Client.dll

(Sasank Kolli) #1

We are trying to update the step using Octopus.Client.dll.
There are no errors however the step is not updated. Our script is as below:

$projectId = “Projects-873”
$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $octopusURI,$apikey
$repository = New-Object Octopus.Client.OctopusRepository $endpoint
$project = $repository.Projects.Get($projectId)
$stepToModify = “test step”

$step = $repository.DeploymentProcesses.Get(“deploymentprocess-$projectId”).FindStep("$stepToModify")
$deployProcess = $repository.DeploymentProcesses.Get($project.DeploymentProcessId)

$step.Actions.Remove($step.Actions[0])

$scriptAction = New-Object Octopus.Client.Model.DeploymentActionResource
$scriptAction.ActionType = “Octopus.Script”
$scriptAction.Name = $step.Name
$scriptAction.Properties.Add(“Octopus.Action.Script.ScriptBody”, $scriptBody)
$scriptAction.ExcludedEnvironments.Add(“Environments-3”)

$step.Actions.Add($scriptAction)

#$step.Actions[0].ExcludedEnvironments = “Environments-3”

$repository.DeploymentProcesses.Modify($deployProcess)

Initially we tried $step.Actions[0].ExcludedEnvironments = “Environments-3” but it said that ExcludedEnvironments is readonly property.
Let us know how to update the deployment step using Octopus.Client.dll

(Daniel Fischer) #4

HI @sasank_kolli,

Thanks for getting in touch! It looks like you are mostly there with your script. However, in your final line:

$repository.DeploymentProcesses.Modify($deployProcess)

It looks like you are modifying the wrong object here. In this step, you should be passing through the $step object and not the $deployProcess object.

So the following should get this through:

$repository.DeploymentProcesses.Modify($step)

Let me know if this helps you get this script working. :slight_smile:

Best regards,
Daniel

(Sasank Kolli) #5

Hi Daniel,

Thanks for your response, however we are getting an error when we pass the step to DeploymentProcesses.Modify Method. The error message is

Cannot find an overload for “Modify” and the argument count: “1”.
At C:\Users\sasank\Desktop\skipbeta.ps1:29 char:1

  • $repository.DeploymentProcesses.Modify($step)
  •   + CategoryInfo          : NotSpecified: (:) [], MethodException
      + FullyQualifiedErrorId : MethodCountCouldNotFindBest
    
    
    

We also tried $step.AddOrUpdateAction($step)
This did not throw any errors, however it did not update the step either.

Please help us to fix the issue here.

Thanks
Sasank

(Daniel Fischer) #6

Hi Sasank,

Thanks for getting back here! It looks like your script may need some further adjustment for it to work in this scenario. I did some testing to confirm and Octopus.Client does not seem to like sending back the $step Object on its own and expects the entire DeploymetProcess object sent back with the modified step. So I made some adjustments to the script for this to work.

I have just tested on my end and seems to work just fine.

$project = $repository.Projects.Get($projectId)


#$step = $repository.DeploymentProcesses.Get(“deploymentprocess-$projectId”).FindStep("$stepToModify")
$deployProcess = $repository.DeploymentProcesses.Get($project.DeploymentProcessId)

foreach ($step in $deployProcess.Steps){
    if ($step.Name -match $stepToModify){

        $step.Actions.Remove($step.Actions[0])
        $scriptAction = New-Object Octopus.Client.Model.DeploymentActionResource
        $scriptAction.ActionType = “Octopus.Script”
        $scriptAction.Name = $step.Name
        $scriptAction.Properties.Add(“Octopus.Action.Script.ScriptBody”, $scriptBody)
        $scriptAction.ExcludedEnvironments.Add(“Environments-3”)
        $step.Actions.Add($scriptAction)
    }
}
$repository.DeploymentProcesses.Modify($deployProcess)

Instead of using the $step object, I use the $deployProcess object and a foreach to find the matching step I want to edit. Then make my changes to $deploymentProcess.Step and put the entire deployment process back at the end.

You may be able to make some edits to better fit your needs, but this should work as a basic example.

It’s also worth noting that we have an open source repo with example scripts you can use for referenced when working with the Octopus.Client.

If you have any further questions here, please let me know.

Best regards,

(Sasank Kolli) #7

Thanks, It’s Working :smiley:

(Daniel Fischer) #8

Hi Sasank,

Thanks for the update! I’m glad to hear this is working now.

Feel free to get in touch at any time. :slight_smile:

Best regards,
Daniel