Modifying Existing Resources through Octopus client CLI


(Satish Y) #1

Hi Team,

I have created some scripts to create all Octopus resources like creating environment,projects,Tentacle addition and life-cycle creation.

Now I want to add a new environment to existing resources like adding new environment to existing target machine,role and life-cycle, I need a script to achieve this.

Also I need a script which create the process for a projects with Stepname,Execution location as Octopus server,role, get the package from Octopus built in using package id, move this package to Azure app service by using Account name,site and resources group, deployment physical path name and Substitute Variables in Files option enable and include a file name.

It will be very helpful to get script for adding new process to existing project with respect to new ENV since we have more number of projects.

Regards,
Satish


(Kenneth Bates) #3

Hi Satish,

Thanks for getting in touch! That’s awesome to hear you’ve created these scripts! We have some various scripts related to machines in our sample script repository that may help you get going. The closest one to what you’re after would probably be AddRoleToTarget.ps1 which is located in the following section.

I think the closest we have to your second question regarding creating a project in this repo is in our Octopus.Client (an open source .NET library) section.

To create the project:

And to create steps within a project’s deployment process:

We’d love to have you contribute the scripts you’ve written as well to that sample script repo if you’re willing. :slight_smile:

I hope this helps get you going! Please don’t hesitate to reach out if you have any further questions moving forward.

Best regards,

Kenny


(Satish Y) #4

Hi Kenneth,

I want a step creation script which deploy to Azure app service. Like mentioned below properties.

Execution location-Octopus server,
role-existing role
get the package from Octopus built in using package id,
move this package to Azure app service by using Account name,site and resources group, deployment physical path name and Substitute Variables in Files option enable and include a file name.

hope you got my requirement.

Regards,
Satish


(Kenneth Bates) #5

Hi Satish,

Thanks for following up. Unfortunately we’re unable to invest the considerable time that would be required in writing custom scripts like this directly. The API sample repository provides some examples that should help get started, and since Octopus is built API-first, anything you do in the UI can be done via the API. Meaning you can watch your browser’s network tab to see the API calls it makes as you perform these tasks in the UI, which may help ease the process. :slight_smile:

Let me know if you have any further questions moving forward, and we’ll be more than happy to assist in any way we can!

Best regards,

Kenny


(Satish Y) #6

Hi Kenny,

I have created a custom script to fulfill my requirement however, I want to dedicate this step to run on only specific environment.

In GUI if you edit the process step we can see “Run only for specific environments” option under conditions, so I want to add this option from my script.

Please find the attached script and let me know how can I add the environment.

Regards,
Satishstepp.ps1 (2.1 KB)


(Kenneth Bates) #7

Hi Satish,

Thanks for following up, and that’s awesome to hear you’ve created your custom script! Adding the environments in which the step will be run in should be just something like adding this line to your current script:

$scriptAction.Environments = "Environments-1"

Where Environments-1 is my Development environment’s ID that you can get via the API at /api/environments. Similarly, you can exclude environments on the step by instead adding $scriptAction.ExcludedEnvironments

I hope this helps! Let me know how you go or if you come across any further questions. :slight_smile:

Best regards,

Kenny


(Satish Y) #8

Hi Kenny,

Thanks for your reply and providing the parameter to add environment to step.

I added the same parameter to my script with my environment id, then when I run the script I got below error. Please find the attached script which included Environment property.

‘Environments’ is a ReadOnly property.
At C:\Bamboo-Home\xml-data\build-dir\OctoCLI\ProcessStep.ps1:34 char:1

  • $scriptAction.Environments = “Environments-1671”
  •   + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
      + FullyQualifiedErrorId : PropertyAssignmentException
    
    

Regards,
Satishstepp.ps1 (2.1 KB)


(Kenneth Bates) #9

Hi Satish,

Thanks for following up! Actually I think what you need to do to scope this step to an environment is to use the line:
$scriptAction.Environments.ReplaceAll($environmentId) where $environmentId = Environments-1671. This is similar to the sample script available on the thread at the following link. :slight_smile:

I hope this helps get you going! Let me know how you go or if you have any further questions moving forward.

Best regards,

Kenny


(Satish Y) #10

Hi Kenny,

I did the changes like you said, but still I am getting error message like below, please find my script as well.

Cannot convert argument “newItems”, with value: “Environments-963”, for “ReplaceAll” to type
“System.Collections.Generic.IEnumerable1[System.String]": "Cannot convert the "Environments-963" value of type "System.String" to type "System.Collections.Generic.IEnumerable1[System.String]”."
At C:\Bamboo-Home\xml-data\build-dir\OctoCLI\ProcessStep.ps1:47 char:1

  • $scriptAction.Environments.ReplaceAll($environmentToAdd)
  •   + CategoryInfo          : NotSpecified: (:) [], MethodException
      + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
    
    
    
    
    
    

$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $octopusURI,$apikey
$repository = New-Object Octopus.Client.OctopusRepository $endpoint

foreach ($pkmn in $pkdex) {
$ProjectId = $($pkmn.ProjectId) # Get this from /api/projects
$StepName = $($pkmn.StepName) # The name of the step
$Role = $($pkmn.Role) # The machine role to run this step on
$PackageId = $($pkmn.PackageId)
#$AccountId = $($pkmn.AccountId)
#$ResourceGroupName = $($pkmn.ResourceGroupName)
#$WebAppName = $($pkmn.WebAppName)
$PhysicalPath = $($pkmn.PhysicalPath)
$TargetFiles = $($pkmn.TargetFiles)
$environment = “Glss-Aqua”

$project = $repository.Projects.Get($ProjectId)
$process = $repository.DeploymentProcesses.Get($project.DeploymentProcessId)
$environmentToAdd = $repository.Environments.FindByName($environment).Id

$step = New-Object Octopus.Client.Model.DeploymentStepResource
$step.Name = $StepName
$step.Condition = [Octopus.Client.Model.DeploymentStepCondition]::Success
$step.Properties[“Octopus.Action.TargetRoles”] = $Role

$scriptAction = New-Object Octopus.Client.Model.DeploymentActionResource
$scriptAction.ActionType = “Octopus.AzureWebApp”
$scriptAction.Name = $StepName
$scriptAction.Properties.Add(“Octopus.Action.Azure.UseChecksum”, “False”)
$scriptAction.Properties.Add(“Octopus.Action.Package.FeedId”, “feeds-builtin”)
$scriptAction.Properties.Add(“Octopus.Action.Package.PackageId”, “$PackageId”)
#$scriptAction.Properties.Add(“Octopus.Action.Azure.AccountId”, “$AccountId”)
#$scriptAction.Properties.Add(“Octopus.Action.Azure.ResourceGroupName”, “$ResourceGroupName”)
#$scriptAction.Properties.Add(“Octopus.Action.Azure.WebAppName”, “$WebAppName”)
$scriptAction.Properties.Add(“Octopus.Action.Azure.PhysicalPath”, “$PhysicalPath”)
$scriptAction.Properties.Add(“Octopus.Action.SubstituteInFiles.Enabled”, “True”)
$scriptAction.Properties.Add(“Octopus.Action.EnabledFeatures”, “Octopus.Features.SubstituteInFiles”)
$scriptAction.Properties.Add(“Octopus.Action.SubstituteInFiles.TargetFiles”, “$TargetFiles”)
$scriptAction.Properties.Add(“Octopus.Action.Package.DownloadOnTentacle”, “False”)
$scriptAction.Environments.ReplaceAll($environmentToAdd)

$step.Actions.Add($scriptAction)

$process.Steps.Add($step)

$repository.DeploymentProcesses.Modify($process)
}

Regards,
Satish