Modify existing lifecycle

Hello There,
I am trying to find a solution to add environment to existing lifecycle. I am trying to use octopus.client dll to automate the task however I am running into some issue. Please see my script below:

Add-Type -Path ‘D:\Program Files\Octopus Deploy\Octopus\Octopus.Client.dll’

$apikey = ‘key’ # Get this from your profile
$octopusURI = ‘server_ip’ # Your Octopus Server address

$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $octopusURI,$apikey
$repository = New-Object Octopus.Client.OctopusRepository $endpoint
$lifecycle = “test”
$phases =“Limited environments”
$Environment = “test-1”
$phase.OptionalDeploymentTargets.Add("$environment")

I was trying to following the script listed below :

Could you please help
Thanks

Hi @alam.musa,

Thanks for getting in touch!

There are some important things to note when working with API objects. One of the most important is that you need to save the entire object you are working on. This would be the Lifecycle object in this case. Within this object you need to create a new phase and assign it some data. The below script is something I have made that will let you add a new phase to your Lifecycle and push it to Octopus. I have tested it on my machine and it works. :slight_smile:

Add-Type -Path 'C:\MyScripts\Octopus.Client.dll' 

# Create endpoint and client
$endpoint = New-Object Octopus.Client.OctopusServerEndpoint("YourServerURL", "API-KEY")
$client = New-Object Octopus.Client.OctopusClient($endpoint)

# We need to grab the entire Lifecycle object as all changes to it must be saved as an entire complete Lifecycle object.
$lifecycle = $client.Repository.Lifecycles.FindByName("Prod")
# We need the Environment-id to tell the phase which environment to associate with it.
$Environment = $client.Repository.Environments.FindByName(“Test").Id


# Create new $phase object and add requisite values.
$phase = New-Object Octopus.Client.Model.PhaseResource
$phase.Name = "New Phase Name" # 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(10,[Octopus.Client.Model.RetentionUnit]::Days) #10 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)

If you have any issues or questions here please don’t hesitate to let me know.

Best regards,
Daniel

Hello @Daniel_Fischer ,
Thank you for getting back to me. Is there anyway I can add the environment to the existing Phase?
I tried looking for it but I cannot see any method called Phase?

i.e. findbyname for Phase test and modify it to add the Environment?
Thanks,
Musa

Hi Musa,

To do this, the script is a bit more simple.

# We need to grab the entire Lifecycle object as all changes to it must be saved as an entire complete Lifecycle object.
$lifecycle = $client.Repository.Lifecycles.FindByName("Prod") # Lifecycle name to add a phase to.
# We need the Environment-id to tell the phase which environment to associate with it.
$Environment = $client.Repository.Environments.FindByName(“Dev").Id # Environment name to add to phase.

$phase = $lifecycle.FindPhase("Phase Name")

$phase.OptionalDeploymentTargets.Add($Environment)

$client.Repository.Lifecycles.Modify($lifecycle)

When you get the entire Lifecycle object, you should see a FindPhase option for it. This allows you to enter a string with the phases name to grab the specific phase from that Lifecycle. Then you can run the same add environment command before modifying the entire Lifecycle object again.

Let me know if this helps.

Best regards,
Daniel

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