Hi,
I am creating a new project in octopus using powershell script and I am having an issue with adding a Library Variable Set.
I find the LibraryVariableSet below and I add it to the $Project and then save it.
But the LibraryVariableSet is not added to my project. It does not throw an error the script runs fine and the $LibraryVariableSet is returning the correct Library Variable Set. Cant figure out why the Library Variable Set is not adding to the project. Any ideas why this may be happening??
$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $octopusURI,$apikey
$client = new-object Octopus.Client.OctopusClient $endpoint
$space = $client.ForSystem().Spaces.FindByName($SpaceName)
$spaceRepository = $client.ForSpace($space)
$projectName = "My Project"
$lifecycleName = "Default Lifecycle"
$projectGroupName = "Default Project Group"
$VariableSet = "nuget-variableset"
$projectGroup = $spaceRepository.ProjectGroups.FindByName($projectGroupName)
$lifecycle = $spaceRepository.Lifecycles.FindByName($lifecycleName)
$LibraryVariableSet = $spaceRepository.LibraryVariableSets.FindByName($VariableSet)
$project = $spaceRepository.Projects.CreateOrModify($ProjectName, $ProjectGroup, $Lifecycle,
$LibraryVariableSet)
$project.Save()
Hi @mikepower79 ,
Thanks for getting in touch!
Sorry to see you are having issues getting this working.
We have example scripts of how to create a project and add a library variable set to projects in our GitHub repo, and I’ve attached them below:
# You can this dll from your Octopus Server/Tentacle installation directory or from
# https://www.nuget.org/packages/Octopus.Client/
Add-Type -Path 'Octopus.Client.dll'
$apikey = 'API-MYAPIKEY' # Get this from your profile
$octopusURI = 'http://MY-OCTOPUS' # Your server address
$projectName = "My project via the api" # Name of the new project
$projectGroupName = "All projects" # Name of the existing project group the new project will be added to
$lifecycleName = "Default Lifecycle" # Name of the existing lifecycle the new project will use
$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $octopusURI,$apikey
$repository = New-Object Octopus.Client.OctopusRepository $endpoint
$projectGroup = $repository.ProjectGroups.FindByName($projectGroupName)
$lifecycle = $repository.Lifecycles.FindByName($lifecycleName)
$project = $repository.Projects.CreateOrModify($projectName, $projectGroup, $lifecycle)
$project.Save()
# You can this dll from your Octopus Server/Tentacle installation directory or from
# https://www.nuget.org/packages/Octopus.Client/
Add-Type -Path 'Octopus.Client.dll'
$octopusProjectName = @('') # Name of the project(s) you want to add/remove the variable set
$octopusVariableSetName = '' # !! CASE SENSATIVE !! Which variable set do you want to add/remove?
$apikey = 'API-Key' # Get this from your profile
$octopusURI = 'https://localhost' # Your server address
$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $octopusURI, $apikey
$repository = New-Object Octopus.Client.OctopusRepository $endpoint
# Gets all existing variable sets from Octopus
$allVariableSets = $repository.LibraryVariableSets.FindAll()
# Gets the index of the variable set that we want to add
$indexOfVariableSetIAmUsing = $allVariableSets.name.IndexOf($octopusVariableSetName)
# Gets the object for the one variable set that we want
$libraryVariableSet = New-Object Octopus.Client.Model.LibraryVariableSetResource
This file has been truncated. show original
If you have any issues moving forward, please do get in touch.
Kind regards,
Ziaul
Hi Ziaul,
That worked great. I want to add a single variable to the project. Not part of the variable set but just one single variable. I am getting an error saying:
“You cannot call a method on a null-valued expression.”
Would this work:
#Add a new variable to the project
$newVariable = new-object Octopus.Client.Model.VariableResource
$newVariable.Name = "PackageName"
$newVariable.Value = "Package.Common.DataConcerns"
# Find the project you want and add the variable to
foreach ($name in $ProjectName) {
$project = $spaceRepository.Projects.FindByName($name)
$project.Variables.AddOrUpdateVariableValue($newVariable)
$spaceRepository.Projects.Modify($project)
}
Hi Mike,
Apologies in the delay getting back to you.
the majority of the code is there, I would adjust it a little and you will be good to go:
$Varname = "name"
$varvalue = "value"
$space = $client.ForSystem().Spaces.FindByName($SpaceName)
$spaceRepository = $client.ForSpace($space)
$newVariable = New-Object Octopus.Client.Model.VariableResource
$project = $spaceRepository.Projects.FindByName($ProjectName)
$variableset = $spaceRepository.VariableSets.Get($Project.links.variables)
$newVariable.Name = $Varname
$newVariable.Value = $varvalue
$variableset.Variables.Add($newVariable)
$spaceRepository.VariableSets.Modify($variableset)
I hope this helps.
Kind regards,
Ziaul
Hi Ziaul,
Thats perfect thanks alot for your help.
Mike
1 Like
system
(system)
Closed
August 16, 2020, 10:00am
7
This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.