slobdell
(slobdell)
November 7, 2019, 4:27pm
1
I don’t see a way to do this. We started with one project and set up a ton of variables in it. We now would like to share those variables with other projects, but I don’t see a way to copy or duplicate a variable from a specific project to a variable set that can be shared. Is this possible? Thanks
Steve
Hi Steve,
Thanks for getting in touch regarding Variable duplication.
While there’s no built-in way to copy a project’s variables to a Library Variable Set in the UI, it is possible to script this with PowerShell.
Below is a couple of pre-written scripts to help get you started.
Using this first script, you should be able to capture your existing Project Variables to provide to the second script, adding the variables to a Library Set.
cls
function UpdateVarInProject {
Param (
[Parameter(Mandatory=$true)][string] $UserApiKey,
[Parameter(Mandatory=$true)][string] $OctopusUrl,
[Parameter(Mandatory=$true)][string] $ProjectName,
[Parameter(Mandatory=$true)][string] $VariableToModify,
[Parameter(Mandatory=$true)][string] $VariableValue,
[Parameter()][string] $EnvironmentScope,
[Parameter()][string] $RoleScope,
[Parameter()][string] $MachineScope,
[Parameter()][string] $ActionScope
)
Process {
Set-Location "C:\Program Files\Octopus Deploy\Tentacle"
Add-Type -Path 'Newtonsoft.Json.dll'
Add-Type -Path 'Octopus.Client.dll'
$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $OctopusUrl,$UserApiKey
$repository = New-Object Octopus.Client.OctopusRepository $endpoint
$project = $repository.Projects.FindByName($ProjectName)
This file has been truncated. show original
# 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-MCPLE1AQM2VKTRFDLIBMORQHBXA' # Get this from your profile
$octopusURI = 'http://localhost' # Your server address
$libraryVariableSetId = "LibraryVariableSets-1" # Get this from /api/libraryvariablesets
$variableName = "Variable name" # Name of the new variable
$variableValue = "Variable value" # Value of the new variable
$endpoint = new-object Octopus.Client.OctopusServerEndpoint $octopusURI,$apikey
$repository = new-object Octopus.Client.OctopusRepository $endpoint
$libraryVariableSet = $repository.LibraryVariableSets.Get($libraryVariableSetId);
$variables = $repository.VariableSets.Get($libraryVariableSet.VariableSetId);
$myNewVariable = new-object Octopus.Client.Model.VariableResource
$myNewVariable.Name = $variableName
$myNewVariable.Value = $variableValue
This file has been truncated. show original
I hope this helps. Please don’t hesitate to let us know if you have further questions.
slobdell
(slobdell)
November 11, 2019, 3:38pm
4
Thank you Tina! I was able to modify the scripts to copy my variables over! Here is my final script in case anyone else is interested.
$OctopusUrl = ""
$project = ""
$APIKey = "" # Get this from your profile
$variableSetID = "LibraryVariableSets-41" # Get this from /api/libraryvariablesets
TransferVarsToVS -UserApiKey $APIKey -OctopusUrl $OctopusUrl -ProjectName $project -variableSet $variableSetID
function TransferVarsToVS
{
Param (
[Parameter(Mandatory=$true)][string] $UserApiKey,
[Parameter(Mandatory=$true)][string] $OctopusUrl,
[Parameter(Mandatory=$true)][string] $ProjectName,
[Parameter(Mandatory=$true)][string] $variableSet
)
Process
{
#Location of DLLs
Set-Location "C:\DLLs"
# You can Octopus.Client.dll from your Octopus Server/Tentacle installation directory or from
# https://www.nuget.org/packages/Octopus.Client/
Add-Type -Path 'Newtonsoft.Json.dll'
Add-Type -Path 'Octopus.Client.dll'
$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $OctopusUrl,$UserApiKey
$repository = New-Object Octopus.Client.OctopusRepository $endpoint
$libraryVariableSet = $repository.LibraryVariableSets.Get($variableSet);
$libraryVariables = $repository.VariableSets.Get($libraryVariableSet.VariableSetId);
$project = $repository.Projects.FindByName($ProjectName)
$projectVariables = $repository.VariableSets.Get($project.links.variables)
foreach($variable in $projectVariables.Variables)
{
$myNewVariable = new-object Octopus.Client.Model.VariableResource
$myNewVariable.Name = $variable.Name
$myNewVariable.Value = $variable.Value
$myNewVariable.Description = $variable.Description
$myNewVariable.Type = $variable.Type
$myNewVariable.Scope = $variable.Scope
$myNewVariable.IsEditable = $variable.IsEditable
$myNewVariable.Prompt = $variable.Prompt
$myNewVariable.IsSensitive = $variable.IsSensitive
$libraryVariables.Variables.Add($myNewVariable)
$repository.VariableSets.Modify($libraryVariables)
}
}
}
Hi Steve,
That’s great! Thanks for posting your final script. I’m sure it will be helpful to others.
Happy Deployments!
Tina