Copy variables from project to variable set?

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.

I hope this helps. Please don’t hesitate to let us know if you have further questions.

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