Hi All,
I am trying to add a variable to a variable set which already exists.
The VARIABLE_NAME I am creating already exists in the variable set with NO Scope. (i.e. This is the one I want the projects to use by default. At some point in my steps I want to add a new variable of the same name
I want to keep this VARIABLE_NAME and add another variable with the SAME NAME but a different VALUE and then add the scope to an Environment which has just been created
Ideally this wouldnt fail if re-ran several times
Does anyone have any ideas?!!?
Add-Type -Path 'C:\Program Files\Octopus Deploy\Octopus\Octopus.Client.dll’
Add-Type -Path ‘C:\Program Files\Octopus Deploy\Octopus\Newtonsoft.Json.dll’
$OctopusURL = “http://localhost”
$AccountName = “MY_ACCOUNT”
$octopusAPIKey = “MY_API”
#Retrieve Environment object with name $OctopusEnvironmentName - this bit works fine but I have hardcoded the Environment below when testing
$Environments = Invoke-WebRequest $OctopusURL/api/environments -Method Get -Headers $header | ConvertFrom-Json
$EnvironmentObject = $Environments.Items | where {$_.Name -like $AccountName}
$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $OctopusURL, $octopusAPIKey
$repository = New-Object Octopus.Client.OctopusRepository $endpoint
$libraryVariableSet = New-Object Octopus.Client.Model.LibraryVariableSetResource
$libraryVariableSet.Name = “MY_HARDCODED_VARIABLESET_NAME”
$libraryVariableSet = $repository.LibraryVariableSets.Create($libraryVariableSet);
$libraryVariableSet
$newVariable = New-Object Octopus.Client.Model.VariableResource
$newVariable.Name = “VARIABLE_NAME”
$newVariable.Value = $AccountName
#if you want to scope your variable
$newVariable.Scope.Add([Octopus.Client.Model.ScopeField]::Environment, (New-Object Octopus.Client.Model.ScopeValue(“MY_ENVIRONMENT”)))
$variableSet = $repository.VariableSets.Get($libraryVariableSet.VariableSetId)
#$variableSet
$variableSet.Variables.Add($newVariable)
#$variableSet
$variableSet = $repository.VariableSets.Modify($variableSet)
#$variableSet