How to set workerpool variable from API?

Hi,

We would like to modify a step in all our deployment processes.
Currently they are running on a specific workerpool.
We now want to change this to use a worker pool variable instead.
This works fine from the ui but can’t find in which property the variable name is stored in?
Can see when set from the ui that step.Actions[0].WorkerPoolId is set to null.

Hi Anders,

I have written up some example code but you will need to modify it to fit your use case. It uses the Octopus.Client.dll. If you use this exact script, you may need to modify the Spaces ID in the first Invoke-RestMethod to match which space you are working in. You will also need to match your current WorkerPoolId and WorkerPoolVariable name lower in the script for your purposes. As always, please do testing with scripts before using them in production. Please let me know if this works for you or if you need any other assistance.

Thanks,
Jeremy

$Apikey = ""#Octopus API Key

$OctopusServerURL = ""            #Octopus URL

$ProjectName = ""

$StepName = ""

Add-Type -Path 'C:\Program Files\Octopus Deploy\Octopus\Octopus.Client.dll' 

$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $OctopusServerURL, $Apikey 

$repository = New-Object Octopus.Client.OctopusRepository $endpoint

$Projects = Invoke-RestMethod -Method "get" -Uri "$OctopusServerURL/api/Spaces-1/projects/all" -Headers @{"X-Octopus-ApiKey" = "$ApiKey" }

$Project = $Projects | Where { $_.Name -eq $ProjectName } | Select -First 1

$Project = $repository.Projects.Get($Project.Id)

$Process = $repository.DeploymentProcesses.Get($Project.DeploymentProcessId)

ForEach ($step in $Process.Steps) {

    If ($step.Name -eq $StepName) {

        if ($step.Actions[0].WorkerPoolId -eq "WorkerPools-1") {

            $step.Actions[0].WorkerPoolId = $null

            $step.Actions[0].WorkerPoolVariable = "Worker Pool"

        }

    }

}

$repository.DeploymentProcesses.Modify($Process)

Hi Jeremy!

Everything worked out well using your example.

Thanks!

Regards

Anders

Hey Anders,

Awesome, glad to hear it! I appreciate you letting me know it worked.

Sincerely,
Jeremy

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.