Move Library variable set to project

Is there any way to move all parameters defined in a library variable set to a project? There are a lot of variables I defined to share between multiple projects but because of new features in v 3 I merged all projected into one and the last thing left is moving variable sets.

Best,
Ali

Hi Ali,

Thanks for getting in touch!

The easiest way to achieve this would be to use the API and it just so happens that I have a PowerShell script prepared to achieve just this! :slight_smile:

$baseUri = "http://url.to.octopus" # <-- Update this to the base URL to your Octopus server(i.e. not including 'app' or 'api'
$apiKey = "API-xxxxxxxxxxxxxxxxxxxxxxxxxx" # <-- Update this to your API key
$headers = @{"X-Octopus-ApiKey" = $apiKey}
$libraryVariableSetId = "LibraryVariableSets-1" # <-- Update this to the Id of your variable set
$projectName = "test" # <-- Update this to the name of your project

function Get-OctopusResource([string]$uri) {
    Write-Host "[GET]: $uri"
    return Invoke-RestMethod -Method Get -Uri "$baseUri/$uri" -Headers $headers
}

function Put-OctopusResource([string]$uri, [object]$resource) {
    Write-Host "[PUT]: $uri"
    #Write-Output $resource | ConvertTo-Json -Depth 10
    Invoke-RestMethod -Method Put -Uri "$baseUri/$uri" -Body $($resource | ConvertTo-Json -Depth 10) -Headers $headers
}

$libVarSet = Get-OctopusResource "api/libraryvariablesets/$libraryVariableSetId"
#Write-Output $libVarSet | ConvertTo-Json -Depth 10
$varSet = Get-OctopusResource "api/variables/$($libVarSet.VariableSetId)"
#Write-Output $varSet | ConvertTo-Json -Depth 10

$project = Get-OctopusResource "api/projects/$projectName"
#Write-Output $project | ConvertTo-Json -Depth 10
$projVar = Get-OctopusResource "api/variables/$($project.VariableSetId)"
#Write-Output $projVar | ConvertTo-Json -Depth 10

$varSet.Variables | % {
    if($_.IsSensitive) {
        $_.Value = ""
    }
    $projVar.Variables += $_
}

#Write-Host "Library Variable Set variables"
#Write-Output $varSet.Variables | Format-List
#Write-Host "Variables"
#Write-Output $projVar.Variables | Format-List

Put-OctopusResource "api/variables/$($projVar.Id)" $projVar

Hope that helps!

Warm regards,
Henrik

Worked like a charm. Thanks a lot!

Henrik,

I am evaluating Octopus Deploy and I am trying to figure out how to do the opposite. I have a very large number of variables that I want to import to a Library Variable Set. I am using the Octopus.Client and it appears that for Library Variable Sets the list of variables is actually not exposed, only the description, name, Id, etc. fields are exposed in that case.

How can I Import variables in a Library Variable Set via the API? Do you have sample code for that?

Thanks in advance.

Answering my own question:

Library Variable Sets are not actually variable sets at the name may lead us to believe, instead, those are the parent or container objects, which expose a VariableSetId property in Octopus.Client. The value of VariableSetId can be used to retrieve the actual variable set so we can work with it.

For sample code see: http://help.octopusdeploy.com/discussions/questions/1806-is-there-a-way-to-bulk-load-library-variable-sets