How do I add environment in lifecycle using powershell script or cmd?
Hi Max,
Thanks for getting in touch! I found a sample script in our API script repo that uses the Octopus.Client (an open source .NET client library) which will add a new phase to an existing lifecycle. I’m wondering if that suits your needs nicely? You could also use that as a template and tweak as needed if you’re looking to do something slightly different, like add an environment to an already existing phase.
I hope that helps, and let us know how you go!
Best regards,
Kenny
Thank you Kenneth, but I am facing some issue in my script.
My script:
Add-Type -Path ‘’
$apikey = ‘’ # Get this from your profile
$octopusURI = ‘’ # Your Octopus Server address
$endpoint = New-Object Octopus.Client.OctopusServerEndpoint($octopusURI,$apikey)
$client = New-Object Octopus.Client.OctopusClient($endpoint)
$lifecycle = $client.Repository.Lifecycles.FindByName(“dev”)
Error:
You cannot call a method on a null-valued expression.
- $lifecycle = $client.Repository.Lifecycles.FindByName(“dev”)
-
+ CategoryInfo : InvalidOperation: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : InvokeMethodOnNull
Hi @maxjose88,
Thanks for following up! Sorry to see you hit some trouble running that sample script. I did a bit of debugging on it, and looks like it didn’t properly account for the repo within a space. I got it working as expected with some tweaks, and I’ll paste the full script below.
Add-Type -Path 'path\to\Octopus.Client.dll'
$server = ""
$apikey = ""
$SpaceName = ""
$LifecycleName = "" # Lifecycle to add the new phase to
$PhaseName = "" # Name of the new phase to create
$EnvironmentName = "" # Name of the environment to add to the phase
# Create endpoint and client
$endpoint = New-Object Octopus.Client.OctopusServerEndpoint($server, $apikey)
$client = New-Object Octopus.Client.OctopusClient($endpoint)
# Get default repository and get space by name
$repository = $client.ForSystem()
$space = $repository.Spaces.FindByName($SpaceName)
# Get space specific repository and get all projects in space
$repo = $client.ForSpace($space)
# We need to grab the entire Lifecycle object as all changes to it must be saved as an entire complete Lifecycle object.
$lifecycle = $repo.Lifecycles.FindByName($LifecycleName) # Lifecycle name to add a phase to.
# We need the Environment-id to tell the phase which environment to associate with it.
$Environment = $repo.Environments.FindByName($EnvironmentName).Id # Environment name to add to phase.
# Create new $phase object and add requisite values.
$phase = New-Object Octopus.Client.Model.PhaseResource
$phase.Name = $PhaseName # Rename what you want.
$phase.OptionalDeploymentTargets.Add($Environment)
$phase.MinimumEnvironmentsBeforePromotion = 0
# Phase's retention Policy
#$phase.ReleaseRetentionPolicy = [Octopus.Client.Model.RetentionPeriod]::new(0,[Octopus.Client.Model.RetentionUnit]::Items) #Unlimmited Releases
$phase.ReleaseRetentionPolicy = [Octopus.Client.Model.RetentionPeriod]::new(2,[Octopus.Client.Model.RetentionUnit]::Days) #2 days
#$phase.TentacleRetentionPolicy = [Octopus.Client.Model.RetentionPeriod]::new(0,[Octopus.Client.Model.RetentionUnit]::Items)
$phase.TentacleRetentionPolicy = [Octopus.Client.Model.RetentionPeriod]::new(2,[Octopus.Client.Model.RetentionUnit]::Days) #2 days
# Add this $phase object to our $lifecycle object.
$lifecycle.Phases.Add($phase)
# Modify the Lifecycle with our new $lifecycle object containing our new phase.
$client.Repository.Lifecycles.Modify($lifecycle)
I’ll make sure to update the script body in the sample I linked to. Let me know how you go or if you have any further questions.
Best regards,
Kenny
Thank you, It worked
Hi @maxjose88,
Great to hear, thanks for confirming! Don’t hesitate to get in touch if we can try to help with anything in the future.
Best regards,
Kenny
This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.